Composing Methods

来源:互联网 发布:生存分析需要什么数据 编辑:程序博客网 时间:2024/06/18 05:28

Extract Method

  • Create a new method, and name it after the intention of the method (name it by what it does, not by how it does it).

     

  • Copy the extracted code from the source method into the new target method.

  • Scan the extracted code for references to any variables that are local in scope to the source method. These are local variables and parameters to the method.

  • See whether any temporary variables are used only within this extracted code. If so, declare them in the target method as temporary variables.

  • Look to see whether any of these local-scope variables are modified by the extracted code. If one variable is modified, see whether you can treat the extracted code as a query and assign the result to the variable concerned. If this is awkward, or if there is more than one such variable, you can't extract the method as it stands. You may need to use Split Temporary Variable and try again. You can eliminate temporary variables with Replace Temp with Query (see the discussion in the examples).

  • Pass into the target method as parameters local-scope variables that are read from the extracted code.

  • Compile when you have dealt with all the locally-scoped variables.

  • Replace the extracted code in the source method with a call to the target method. 

  • Compile and test

Inline Method

  • Check that the method is not polymorphic.

  • Find all calls to the method.

  • Replace each call with the method body.

  • Compile and test.

  • Remove the method definition.

Inline Temp   

  • Declare the temp as final if it isn't already, and compile.

     

  • Find all references to the temp and replace them with the right-hand side of the assignment.

  • Compile and test after each change.

  • Remove the declaration and the assignment of the temp.

  • Compile and test.

Replace Temp with Query

 

  • Look for a temporary variable that is assigned to once.

     

  • Declare the temp as final.

  • Compile.

     

  • Extract the right-hand side of the assignment into a method.

     

  • Compile and test.

  • Use Replace Temp with Query on the temp

  •  

     

    Introduce Explaining Variable

     

  • Declare a final temporary variable, and set it to the result of part of the complex expression.

  • Replace the result part of the expression with the value of the temp.

     

  • Compile and test.

  • Repeat for other parts of the expression.

  •  

    Split Temporary Variable 

    • Change the name of a temp at its declaration and its first assignment. 

    • Declare the new temp as final.

    • Change all references of the temp up to its second assignment.

    • Declare the temp at its second assignment.

    • Compile and test.

    • Repeat in stages, each stage renaming at the declaration, and changing references until the next assignment.

    Remove Assignments to Parameters

    • Create a temporary variable for the parameter.

    • Replace all references to the parameter, made after the assignment, to the temporary variable.

    • Change the assignment to assign to the temporary variable.

    • Compile and test.

    Replace Method with Method Object

    • Create a new class, name it after the method.

    • Give the new class a final field for the object that hosted the original method (the source object) and a field for each temporary variable and each parameter in the method.

    • Give the new class a constructor that takes the source object and each parameter.

    • Give the new class a method named "compute."

    • Copy the body of the original method into compute. Use the source object field for any invocations of methods on the original object.

    • Compile.

    • Replace the old method with one that creates the new object and calls compute.

    Substitute Algorithm

  • Prepare your alternative algorithm. Get it so that it compiles.

  • Run the new algorithm against your tests. If the results are the same, you're finished.

  • If the results aren't the same, use the old algorithm for comparison in testing and debugging.

  •  

     

    原创粉丝点击