24.重构

Reasons to refactor:

  1. code is duplicated;
  2. A routine is too long; A routine has a poor name;
  3. A loop is too long or too deeply nested;
  4. A class has poor cohesion; A class interface does not provide a consistent level of abstraction; One class is overly intimate with another;
  5. A parameter list has too many parameters;
  6. Data members are public;
  7. Global variables are used;

Refactorings:

  1. Data-level refactoring;
  2. statement-level refactoring;
  3. routine-level refactoring;
  4. class implementation refactoring;
  5. class interface refactoring;
  6. system-level refactoring.

TIPS:

  • replace a magic number with a named constant;
  • rename a variable with a clearer or more informative name;
  • replace an expression with a routine;
  • convert a multi-use variable to multiple single-use variables;
  • replace a traditional record with a data class;
  • change value objects to reference objects;
  • create a definitive reference source for data you can't control;
  • provide a factory method rather than a simple constructor.
上一篇:解决请求时,Status Code: 413 Request Entity Too Large,使用Nginx代理


下一篇:shell 遇到的问题