001 reorganize function

Reorganize function

Reconstruction of basic skills: small step forward, frequent testing

1. Extract Method (refining function)

Q: You have a piece of code that can be grouped together and independence

A: A code into a separate function, and make use of the function name of the function explained.

Function of particle size is small, it is a great opportunity to reuse; brief but well-named functions will make the function more like a series of high-level annotation; fine-grained overwrite function easier.

Semantic distance between the function name and function determines the length of the body.

practice:

  • Creating a new function, a function named intent (what to do, rather than how to do it)
  • The extract function code copied from the source to the new objective function
  • Carefully check refining code, check for references local variable or function parameter source
  • Check if there is only used temporary variables to be refined code segment, and if so, declare them as temporary variables in the objective function in the newly created
  • Check the refined code segment to see if there are any local variables that change can be considered
    • The code segment is treated as a refined query function returns the value assigned to the relevant variables
    • Use Split the Temporary Variable , create multiple temporary variables
    • Use Replace the Temp with Query , a query function instead of temporary variables
  • Local variables need to be read by the parameters passed to the objective function
  • Replace all calls

2. Inline Method (inline)

Q: the body and the name of a function equally clear and understandable

A: insert a function call point function in the body, then remove the function

Indirection may bring help, but not always necessary indirection uncomfortable.

Organization irrational function, you can to a large inline function, and then migrate or extract.

practice:

  • Check whether they have a function polymorphism
  • Find all the call-sites
  • Replace function call site body
  • Delete function definition

3. Inline Temp (temporary inline variable)

Q: You have a temporary variable is assigned only once with a simple expression, but it prevents other reconstruction techniques

A: All references to the variable action, replace expression that temp itself.

This reconstruction technique is usually to reconstruct the way other services, such Replace Temp with Query or Extract Method, .

practice:

  • Ensure that the expression has no side effects
  • Temporary variables for the final which is not assigned to check again
  • Find temporary variable reference point and replace expression
  • Statements and delete temporary variable assignment statement

4. Replace Temp with Query (query to replace the temporary variables)

Q: Your program to a temporary variable to hold the result of a calculation expression

A: this expression to extract a separate function, all the temporary variable reference point is replaced with a call to the new function

Existing temporary variables is that they are temporary and can only be used within the current function belongs. If you replace the temporary variable to a query, then the same class all the functions can access this information.

practice:

  • Only to find out is assigned a temporary variable (if assigned several times, the first use Split Temporary Variable divided into multiple categories)
  • Declare variables for the final
  • Refining the right side of the equal sign is part of a query function
  • Variable implementation Inline Temp

For temporary variable to the type of statistics, brought into circulation also requires a separate query function, the number of temporary variables lead to multiple cycles.

5. Intruduce Explaining Variable (incorporated explanatory variables)

Q: You have a complex expression

A: The results of complex expression (or part) is placed in a temporary variable expression in order to explain the use of the variable name.

This reconstruction technique commonly used in conditional logic, especially complex particularly long determination condition expression, conditional clauses can be refined to a temporary variable named well explain the meaning of the corresponding condition clause.

practice:

  • Declare a final temporary variable, the result of the operation part of the action of a complex expression is assigned to it
  • The result of the operation to replace the temporary variable
  • Repeat on other portions

6. Split Temporary Variable (decomposition temporary variables)

Q: Your program has been copied to a temporary variable more than once, but it is neither a loop variable nor be used to collect the results.

A: For each assignment, create a separate, temporary variables corresponding.

If a temporary variable to assume more responsibility, it should be replaced broken down into a number of temporary variables, otherwise it will make the reader confused.

practice:

  • The new temporary variable declared as final
  • The second temporary variable to assign actions for the sector, modify the original temporary variable reference for the new temporary variable
  • Redeclaring temporary variables in the original assignment at second
  • This operation is repeated, each time in a statement at the renamed and modified reference point before the next assignment

7. Remove Assignments to Parameters (removal of the assignment of parameters)

Q: a code assignment parameter

A: to a substitution position of the temporary variable parameter

Parameter code assignment to reduce the sharpness, the mix is ​​passed by value or by reference pass two kinds of parameter passing. Can not have any effect on the parameters passed by value for the parameter assignment.

practice:

  • The establishment of a temporary variable to assign parameters to it to be processed
  • To the assignment of parameters for the sector, a reference point for all temporary variable substitution
  • Modify assignments, assign it to a temporary variable

8. Replace Method with Method Object (Object function to replace function)

Q: You have a large function, which use local variables so that you can not adopt Extract Method

A: A function into a single object, the result becomes a local variable within the object field, you can then this function into a plurality of small major functions in the same object.

There is a local variable degree of decomposition increases the function, the entire function can be turned into an object, the object becomes a local variable field, then further split function.

practice:

  • Create a new class
  • The new class holds a source object, create the corresponding fields for each temporary variables and parameters of the original function
  • Build building function
  • Establish compute function
  • Copy function to compute the original function, and replaces calls to other functions of the source object
  • Replace the old function as a new class of compute function

9. Substitute Algorithm (replacement algorithm)

Q: You want to replace an algorithm is another algorithm clearer

A: A function is replaced with another body algorithm.

Just place it!

Guess you like

Origin blog.csdn.net/weixin_33989780/article/details/90973538
001