Home php教程 php手册 PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式

PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式

Jun 13, 2016 pm 12:00 PM
php introduce thinking condition Check Click simplify expression

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


 介绍
 
 条件逻辑有可能十分复杂,因此本章提供一些重构的手法,专门用来简化它们。
 
全文简述(你可直接跳过下面的内容)
  核心重构: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可以作为交流与调试的辅助。——交流:可以帮助程序员阅读理解代码所做的假设。调试:帮助程序员找到bug,可以在距离最近的地方抓住bug。
  5、assertion并不改变程序的任何行为。
  6、assertion价值:帮助程序员理解代码正确运行的必要条件。
  7、建议最好把assertion的条件式使用Extract Method,为了将若干地方的重复码提炼到同一个函数中,也许只是为了更清楚说明条件式的用途。
 
 总结
 
       这一章我比较喜欢“Replace Nested Conditional with Guard Clauses “这个方式,我在平时的代码中也经常这样用,还有人给这种方式取名叫”卫从句“。
      还有一个就是我经常在php开发中用的调试是var_dump()或print_r(),我也第一次发现php中还有assert这种方式,不错!
 
        在学习和实践的过程中,我也学到了很多不错的方式。但是我觉得在团队开发中,有的时候还是”大局为重“,按照团队的习惯方式去编码,或者你可以跟团队沟通,得到大家的认可之后,在使用这里面的方法,这样大家彼此调试和阅读对方代码的时候比较方便。
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles