`
irican
  • 浏览: 31651 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Composing Methods

阅读更多
Extract Method
这个不难,用eclipse就可以解决大部分问题。需要注意的有几点:
1.有局部变量的情况,要作为参数传入目标函数
2.对局部变量再赋值,要将新值作为目标函数的返回值
3.如果目标函数需要返回多个值,则把它拆开,确保一个函数返回一个值

Inline Method
和Extract Method相反,在以下几种情况使用:
1.函数体极少(一行?),而且与函数名一样清晰易懂
2.有一堆组织不合理的小函数,需要inline到一个大函数中,然后在Extract Method
3.无用的间接层

Inline Temp
作为Replace Temp with Query的一部分使用,把对无用临时变量的赋值去除

Replace Temp with Query
太多的临时变量会造成无法Extract Method(原因应该是参数太多),所以这个步骤可能是在Extract Method前必不可少的。前提是临时变量只被赋值一次。如果赋值了多次,就用Split Temporary Variable或Separate Query from Modifier,确保它被赋值一次。

Introduce Explaining Variable
一个表达式可能很长,很难读懂。这时候引入用于解释的临时变量。在较长的算法中,也可以引入临时变量解释每一步的意义。注意:如果可以用Extract Method就不用Introduce Explaining Variable。仅当Extract Method难以进行时用。

Split Temporary Variable
一个临时变量只能承担一个责任。如果承担了多个责任,则需要引入另外的临时变量达到这个要求。

Remove Assignments to Parameters
与Split Temporary Variable类似,参数只表示“被传递进来的东西”。对参数赋值不会改变原引用的内容,只会使程序更加混乱。修改方法是给参数加final,但是一般情况下不必把所有参数都强制加final。

Replace Method with Method Object
当一个大型函数无法Extract Method时,可以尝试这种方法。主要原则是把大型函数中所有的临时变量都作为field放入一个新的class中。由于field可以被所有method使用,所以可以在该class内顺利的进行Extract Method,而不用在方法间传递参数。引入一个class,只包含一个函数,并通过构造函数把所有的参数传入,并赋值到field中。原先的函数仅起到一个delegate的作用。

Substitute Algorithm
这个方法让我匪夷所思,感觉就是不改变函数的任何外部行为的情况下,修改函数的内部实现。感觉叫替换函数的实现方式好像更准确些。对于这个重构方法,单元测试至关重要。
分享到:
评论

相关推荐

    重构_改善既有代码的设计

    第 6 章:重新组织你的函数(Composing Methods) 第 7 章:在对象之间移动特性(Moving Features Between Objects) 第 8 章:重新组织你的数据(Organizing Data) 第 9 章:简化条件表达式(Simplifying...

    重构_改善既有代码的设计[高清版]中文版

    Chapter 6:Composing Methods 组合方法   Extract Method 提取方法   Inline Method 内联方法   Inline Temp 内联临时变量   *Replace Temp with Query 用查询方法代替临时变量   Introduce ...

    重构-改善既有代码的设计+中文版

    Chapter 6:Composing Methods 组合方法   Extract Method 提取方法   Inline Method 内联方法   Inline Temp 内联临时变量   *Replace Temp with Query 用查询方法代替临时变量   Introduce Explaining ...

    重构——改善既有代码的设计

    Chapter 6:Composing Methods 组合方法   Extract Method 提取方法   Inline Method 内联方法   Inline Temp 内联临时变量   *Replace Temp with Query 用查询方法代替临时变量   Introduce Explaining ...

    重构-改善既有代码的设计(中文版)

    Chapter 6:Composing Methods 组合方法   Extract Method 提取方法   Inline Method 内联方法   Inline Temp 内联临时变量   *Replace Temp with Query 用查询方法代替临时变量   Introduce Explaining ...

    重构,改善既有代码的设计

    Chapter 6:Composing Methods 组合方法   Extract Method 提取方法   Inline Method 内联方法   Inline Temp 内联临时变量   *Replace Temp with Query 用查询方法代替临时变量   Introduce ...

    重构 改善既有代码的设计

    Chapter 6:Composing Methods 组合方法   Extract Method 提取方法   Inline Method 内联方法   Inline Temp 内联临时变量   *Replace Temp with Query 用查询方法代替临时变量   Introduce ...

    composing-methods

    composing-methods

    Refactoring

    The heart of the book is a catalog of refactorings, organized in chapters on composing methods, moving features between objects, organizing data, simplifying conditional expressions, and dealing with...

    论文研究-Learning Compact Translation Model by Composing-Based Phrase Extraction.pdf

    使用组合短语抽取方法构造精简翻译模型,李强,肖桐,在基于短语的统计机器翻译系统中,传统的短语对抽取方法启发的抽取所有与词对齐保持一致的短语对。本文提出了一种基于组合的短语��

    Image Embedding of PMU Data for Deep Learning towards Transient Disturbance

    A real synchrophasor set composing of three different types of disturbance events from the Frequency Monitoring Network (FNET) is used. An image embedding technique called Gramian Angular Field is ...

    Scala for the Impatient 2nd (完整英文第二版 带书签)

    17.5 Composing Future Tasks 252 17.6 Other Future Transformations 255 17.7 Methods in the Future Object 256 17.8 Promises 258 17.9 Execution Contexts 260 Exercises 260 18 TYPE PARAMETERS L2 265 18.1 ...

    JavaScript.Application.Design.A.Build.First.Approach

    Chapter 6 Understanding Asynchronous Flow Control Methods In Javascript Chapter 7 Leveraging The Model-View-Controller Chapter 8 Testing Javascript Components Chapter 9 Rest Api Design And Layered ...

    Python Cookbook英文版

    5.14 Keeping References to Bound Methods Without Inhibiting Garbage Collection 5.15 Defining Constants 5.16 Managing Options 5.17 Implementing a Set Class 5.18 Implementing a Ring Buffer ...

    Effective Software Test Automation

    Software test engineers would like to see a fully automated software test tool on the market, one that is capable of completing testing tasks from generating test scripts and composing the testing ...

    Mastering Ansible - Second Edition

    Finally, you will discover the methods used to examine and debug Ansible operations, helping you to understand and resolve issues. By the end of the book, the readers will be able to unlock the true...

    Mastering.Ansible.178439548X

    Design, develop, and ...Chapter 5: Composing Reusable Ansible Content with Roles Chapter 6: Minimizing Downtime with Rolling Deployments Chapter 7: Troubleshooting Ansible Chapter 8: Extending Ansible

    Java 8 Lambdas

    Composing Collectors 67 Refactoring and Custom Collectors 69 Reduction as a Collector 76 Collection Niceties 77 Key Points 78 Exercises 78 6. Data Parallelism. . . . . . . . . . . . . . . . . . . . . ...

    C# Programming Cookbook [EPUB+MOBI+PDF]

    Generate tests from the Code Contracts for mission critical methods. Integrate code in Visual Studio with GitHub. Create a web application in Azure. Design and build a microservice architecture ...

    Packt.Csharp.Programming.Cookbook.2016

    Generate tests from the Code Contracts for mission critical methods. Integrate code in Visual Studio with GitHub. Create a web application in Azure. Design and build a microservice architecture ...

Global site tag (gtag.js) - Google Analytics