Refactoring - improving the design of existing code

 

"Refactoring - Improving the Design of Existing Code"

1.      Refactoring: Improve the internal structure of the software without changing its observable behavior. Refactoring is the key to keeping code readable and modifiable, improving its design after it's written.

2.      Adapt the name of the code - it's worth it. Good code should clearly express its function, variable names are the key to code clarity (you can use the search and replace tool to modify)

3.      Try to remove temporary variables

4.      The purpose of refactoring: to make the software easier to understand and modify, improve performance appropriately, and complement the design (refactoring can bring more simple designs without losing flexibility. It reduces the difficulty of the design process and reduces the pressure of design )

5.      Improving the design is an important direction. Improve the dimensions of code quality: improve design, improve readability, reduce errors, etc.

6.      The most common refactoring is when adding new features. Refactoring while debugging is for better readability. Code walkthroughs can also be refactored.

7.      Bad smell: Duplicate code, too long functions (the longer it is, the harder it is to understand and decompose it), too large classes, too long parameter lists, one change in many places, too manyOkay),

8.      The basics of refactoring: take small steps, test frequently

9.      Too long function: extract a piece of code from a long function and put it into a separate function, let the function name explain the purpose of the function. Comments, loops, etc. can be used as refinement points. The biggest difficulty: dealing with local variables and removing temporary variables as much as possible. If the temporary variable is read-only and not modified, it is passed to the function as a parameter. ——"The granularity of each function is very small, and the opportunity for each function to be reused is large.

10.  A temporary variable is simply assigned once, then replace the assignment code with the temporary variable

11.  Put the result of a complex expression into an interpretive temporary variable to indicate the purpose of the expression in Ming Dynasty

12.  Assignment of a temporary variable is noisy once - "Each assignment, set an independent corresponding temporary variable

13.  Remove the assignment to the parameter in the function and replace it with a temporary variable

14.  Replace the function body with an algorithm

15.  Move function: Create a new function with similar behavior in the class most often referenced by the function - "turn the old function into a simple delegate function or remove the old function completely

16.  Refinement class: a class does two classes of things - "refines related fields and functions to a new class

17.  Inline classes: a class does nothing - "merge two classes

18.  Get/set indirect access to variables benefits: subclasses can change the way to obtain data through a function, direct access benefits: code is easy to read

19.  When a data is extracted, it only makes sense when it is combined with other data and behavior, and then encapsulates it into a class

20.  Replace arrays with objects. Each element in the array is represented by a field of the object. An array holds different objects, causing trouble for users

21.  Simplify expressions - extract functions (decomposition expressions) from if ... else respectively, combine expressions with the same return value - "refine into functions, combine repeated code ( sometimes in if ... else ) - "Ported outside of expressions

22.  Replace the expression with polymorphism, put the branch of each expression into a subclass to override the function, and declare the original function as an abstract function

23.  简化函数调用——修改函数名——》解释函数用途;添加对象参数;移除不必要的参数;以一个对象取代多个参数;函数从未被其他类用到——》改为private;将构造函数改为工厂函数

24.  处理概括类关系:2个子类有同一字段——》移到超类;某函数在多个子类中有相同结果——》移到超类;

25.  某字段/函数只在某些子类中用——》移到子类;为2个类提炼新类,将共同的代码移到新类;子类和超类区别不大——》合并

26.  开发者不愿意重构原因:不知道如何重构;是一个额外的工作;有风险,可能破坏现有程序。

27.  重构是手段不是目的最大的障碍:没有工具支持

28.  小函数如同娇小可爱的人

29.  OO对象的全部要点是将数据和对数据的操作行为包装在一起

30.  代码走查:有助于在开发团队中传播知识;有助于让有经验的开发者将知识传播给欠缺经验的开发者,并帮助更多的人理解大型软件系统中更多的部分

31.  重写代码:代码基本不能工作

32.  重构代码:

(1)    改进软件设计:没有重构,程序设计会变质,再为短期目的或未完全理解整体设计前,就贸然修改代码,程序会失去自己的结构(代码结构的流失是累计的)

(2)    使软件更易理解:除了正确告诉计算机我要做什么外,还要告诉第23人看懂。重构帮助理解原本不太熟悉的代码

(3)    帮助找到BUG,写出更强健的代码

(4)    提高编程的速度(良好发的设计是快速开发的关键)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946910&siteId=291194637