Refacotring: Improving the Design of Existing Code

This post includes the notes that I went through during the reading of the book as mentioned in title. I would like to paste one sentence that totally solved one of my questions regarding the differences between refactoring and design patterns:

When my coauthors and I wrote Design Patterns, we mentioned that design patterns provide targets for refactorings. However, identifying the target is only one part of the problem; transforming your code so that you get there is another challenge.

Chapter 1 - Refactoring, A First Example

Extract Method: extract a block of complex statements as a single function, which is used to take care of the main logic. By doing this, we could simplify the original function which includes that block of codes.

Move Method: If the method inside one class does not use any information about it, then this function could be moved to another place: the current class is not the right place for the function at all.

Replace Conditional With Polymorphism: refers to the technique to replace the if-else condition to the different behavior rendered by the polymorphism.


  1. When you find you have to add a feature to a program, and the program’s code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature.

  2. Everytime when we are going to do refactoring, the first step is always the same, we have to build a sold set of tests for that section of code.

  3. Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

  4. It is a bad idea to do a switch based on an attribute of another object. If you must use a switch statement, it should be on your own data, not on someone else’s.

Chapter 2 - Principles in Refactoring

The definition of refactoring: a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.