preface

  • Refactoring in programming is like fish in water. Every breath of code is accompanied by refactoring, not at a specific time, but all the time.
  • Refactoring is to programming what testing is to refactoring.

Most of the time

  1. Methods should be placed in the object of the data being used, for example
  2. The way code is written, design => develop => test => refactor => test => Refactor => test
On/off principle => close to him rather than perfect match.

Bad smells in code – When to refactor? When there are bad smells in code, what are they?

  1. Replace switch with polymorphism (subclass, Policy, State)
  2. Parallel inheritance (inheriting a class and adding subclasses to another class)
  3. Some special case variables (generally when people understand code, it is common to think that an object needs all of its variables all of the time, and it is extremely unfriendly to guess the purpose of a variable if it is not being used. Move this special case and its associated code to the new object.)
  4. Too long message chain (a.gettb.getc.getd….)
  5. 1. Cut out the middleman and deal directly with the responsible object. 2. If the middleman has other responsibilities, give them jobs where they can actually do the work)
  6. Too close (two classes spend too much time exploring each other’s private components. 1. If the two classes are inseparable, extract the tight part into a new class and use this class. 2, or start a new class of proxy)
  7. Overcomment (not that you can’t comment, but that your code doesn’t need comments anymore)

How to reconstruct

Reorganize your approach

  1. Each temporary variable should be independent and should not have too many responsibilities => assign final
  2. Removes an assignment to a parameter
int discount(int a,int b){if(a>2) a-=2; . } = >int discount(int a,int b){
    int var1 = a;
    if(a>2) var1-=2;...}
Copy the code
  1. If you have a large method with a lot of business operations and variables in it, there is the problem of passing parameters when we need to break it down into smaller modules. This time, be able to build a class, then the original method of the parameters of the objects and methods used for field construction method to the new class, and then in the new class to create a new method as a substitute for the original method, and then decomposed to the new method (benefit is don’t have to worry about passing parameters and, because of these parameters are all new class field)

The migration

  1. If there is a method or field in the current class that has more interaction with another class, move that method to another class. If all fields of the current class should belong to another class, merge them into another class (inline class).
  2. A class does too many simple delegate procedures. Remove it and call it directly.

Reorganizing data

  1. Value objects usually refer to themselves as immutable (they are defined by the data values they contain), whereas reference objects represent some concrete object in reality (for example, usable)= =To judge). Although we mostly start with value objects in development, we should change to reference objects whenever we introduce some modifiable data.

Reduced conditional expression

  1. Using polymorphism in place of conditional expressions (such as polymorphism in place of Switch) does not eliminate the Switch completely. For example, we take a parameter of type type and need to determine what type it is, and then perform the corresponding operations (which can be solved by polymorphism).

Dealing with generalizations

  1. Perform similar operations in the same order in some subclass methods, but with different details, and move those operations into a superclass (which is essentially a template method)
  2. When subclasses use only a subset of the superclass’s methods or data that doesn’t need to be inherited at all, remove the inheritance and use delegates. Of course, if the situation is reversed, change delegation to inheritance.

Large-scale refactoring

  1. If you have two different responsibilities in an inheritance system, then you need to create another inheritance system and delegate one to call the other.

Automated refactoring tool on IDEA

  1. CTRL + Alt +shit+t lists all refactoring methods
Reference: Martin Fowler, Refactoring – Improving the Design of Existing Code