Composing Methods

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.

  •  

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spanned.SPAN_COMPOSING 是一个常量,用于表示一个跨度对象(Span)正在处于组合文本输入的状态。它是用于处理复杂文本输入和编辑的 Android 文本样式类 Spanned 的一个标记常量。 当用户正在进行组合文本输入(如中文输入法的拼音输入)时,输入的文本可能会被拆分成多个部分,并且在用户最终确定输入之前,这些部分可能会不断变化。SPAN_COMPOSING 被用于标记这些处于组合输入状态的文本部分,以便在显示和处理这些文本时进行特殊处理。 在使用 Spanned 接口及其实现类(如 SpannableString 或 SpannableStringBuilder)处理文本时,可以使用 Spanned.SPAN_COMPOSING 来标记处于组合输入状态的文本部分。例如,你可以使用 setSpan() 方法将 Spanned.SPAN_COMPOSING 应用于文本的特定范围,以便在显示或处理文本时对这些部分进行特殊处理。 以下是一个使用 Spanned.SPAN_COMPOSING 的示例: ```java SpannableString spannableString = new SpannableString("Hello World"); spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, Spanned.SPAN_COMPOSING); textView.setText(spannableString); ``` 在上面的示例中,"Hello" 这个单词将被标记为处于组合输入状态,并且会应用红色前景色。这样,当用户在输入过程中修改或删除这部分文本时,可以对其进行特殊处理,以提供更好的用户体验。 希望这个解释对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值