Home  >  Article  >  Backend Development  >  PHP Talk "Refactoring - Improving the Design of Existing Code" Part 4 Simplifying Conditional Expressions_PHP Tutorial

PHP Talk "Refactoring - Improving the Design of Existing Code" Part 4 Simplifying Conditional Expressions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:19:04915browse

思维导图
点击下图,查看大图。


 介绍
 
 条件逻辑有可能十分复杂,因此本章提供一些重构的手法,专门用来简化它们。
 
全文简述(你可直接跳过下面的内容)
  核心重构:Decompose Conditional——分离”转辙逻辑“(switching logic)和”操作细节“(details)分离。
  多处测试有相同结果:Consolidate Conditional Expresssion
  条件代码中去掉重复成分:Consolidate Duplicate
  标识特殊情况:Replace Nested Conditional with Guard Clauses
  去除讨厌的控制标记:Remove Control Flag
 
 
 专业术语
 
decompose:分解,分离
consolidate:合并
eligible:合适的,合格的
fragment:碎片,片段
nest:嵌套
guard:保卫
clause:从句
polymorphism:多态
assertion:断言
unchecked exception:不可控异常
 
 Decompose Conditional
 
状况:你有一个复杂的条件(if-else if-else)语句,那么从if、else if、else三个段落中分别提炼出函数。

 

 

 

 Consolidate Conditional Expression
 
状况:你有一些条件测试,都得到相同的结果,那么将这些测试合并为一个条件式,并将这个条件提炼称为一个独立的函数。
动机: 1、合并后的条件代码会告诉你“实际上只有一次条件检查,只不过有数个并列条件需要检查而已“,——使检查的用意更清晰。
     2、为Extract Method做好准备。——将检查条件提炼成一个独立函数,对于理清代码意义非常有用。它把描述“做什么”的语句换成了“为什么这样做”。

 

条件语句的“合并理由”也同时指出了“不要合并”的理由:如果你认为你的这些检查的确彼此独立,的确不应该被视为同一次检查,那么就不要使用本项重构。因为在这种情况下,你的代码已经清楚表达出自己的意义。

 

 

 Consolidate Duplicate Conditional Fragments
 
状况:在条件式的每个分支上有着相同的一段代码,那么将这段重复代码搬移到条件之外。
 

 
 Remove Control Flag
 
状况:在一系列布尔表达式中,某个变量带有“控制标记”的作用,那么以break语句或return语句取代控制标记。
 

 
 

 
 Replace Nested Conditional with Guard Clauses
 
状况:函数中的条件逻辑使人很难看清正常的执行路径,那么使用卫语句(Guard Clauses)表现所有特殊情况。


条件式的两种形式:
  1、所有分支都属于正常行为:使用[if ... else..]
  2、条件式极其罕见:应该单独检查该条件,并在该条件为真时,立刻从函数中返回。——这样的单独检查常常被称为”卫语句“
Replace Nested Conditional with Guard Clauses精髓:给某一分支以特别重视。

 
 Replace Conditional with Polymorphism
 
状况:你手上有个表达式,它根据对象型别的不同而选择不同的行为,那么将这个条件式的每个分支放进一个subclass内的覆写函数中,然后将原始函数声明为抽象函数。
 

此代码的坏味道:

  1、它太长,当视频有新类型的时候,它会变得更长。

  2、它明显做了不止一件事。

  3、它违反了单一权责原则,因为它有好几个修改它的理由。

  4、它违反了开放闭合原则,因为每当添加新类型时,必须修改它。不过最麻烦的可能是到处皆有类似结构(_get类型名Rank())的函数。

 

 Introduce Assertion
 
状况:某一段代码需要对程序状态(state)做出某种假设,那么以断言(assertion)明确表现这种假设。
 

 

 

运行结果:

运行结果:

 
采点:

  1、常常会有这样的代码,只有当某个条件为真时,该段代码才能正常运行。——实际上程序最后成品往往将assertion统统删除。
  2、这样的假设通常并没有在代码中明确表现出来,你必须阅读整个算法才能看出。——有时候程序员会以注释写出这样的假设,而assetion是一种更好的技术。
  3、assertion是一个条件式,应该总是为真。如果失败,表示程序员犯了错误
 4. assertion can be used as an auxiliary for communication and debugging . ——Communication: Can help programmers read and understand the assumptions made by the code. Debugging: Help programmers find bugs and catch them at the nearest location.
5. assertion does not change any behavior of the program.
 6. assertion value: Helps programmers understand the necessary conditions for the correct operation of the code.
7. It is recommended to use Extract Method for the conditional expression of assertion, in order to extract the repeated codes in several places into the same function, perhaps just to explain the purpose of the conditional expression more clearly.
Summary
I prefer the method "Replace Nested Conditional with Guard Clauses" in this chapter. I often use this method in my daily code. Some people also call this method "Guard Clauses".
Another thing is that the debugging I often use in PHP development is var_dump() or print_r(). This is the first time I discovered that there is an assert method in PHP, which is good!
In the process of learning and practice, I also learned many good methods. But I think in team development, sometimes "the overall situation is the most important thing", coding according to the team's customary way, or you can communicate with the team, and after getting everyone's approval, use the methods here, so that everyone can debug each other It is more convenient when reading each other's code.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325364.htmlTechArticleMind Map Click the image below to view the larger image. Introduction Conditional logic can be complex, so this chapter provides some refactoring techniques designed to simplify it. Brief description of the full text (you can directly...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn