- “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
—M. Fowler (1999)
Refactoring
Refactoring is improving the design of existing code. The first step in coding is getting code that works, but do not stop there. The next step is making your code clear and maintainable, and that is what refactoring helps you do. Since refactoring is changing code, you should have unit tests for your code base before you refactor so you can make sure you are not breaking code.
Here is a list of all the posts we have for refactoring. Some refactorings are simple, some are more complex; however, refactoring is considered an essential skill for competent programmers so you should learn them and use them. You will write better code once you start using refactoring.
- Extract MethodBreak long methods into shorter methods.
- Inline MethodInline a simple method when the body is as clear as the name.
- Inline VariableUse a simple expression directly when a variable does not add any clarity or value.
- Introduce Explaining VariableStore the result of an expression in a variable with a name that explains its purpose.
- Introduce Parameter ObjectCreate a class for commonly used groups of parameters.
- Remove Dead CodeRemove code that is no longer used.
- Rename VariableRename a variable so that its name clearly indicates its purpose.
- Split Temporary VariableMake separate temporary variables for each assignment.