PHP: refactoring-improving the design of existing code-4 Simplified conditional expressions

Source: Internet
Author: User

Mind Map
Click to view the chart.

The introduction of conditional logic may be very complicated. Therefore, this chapter provides some refactoring techniques to simplify them. Full text description(You can skip the following content) Core refactoring: Decompose Conditional -- detach the switching logic and the details separation. Multiple tests have the same results: Repeated components are removed from the invalid lidate Conditional Expresssion condition code: Invalid lidate Duplicate identity special case: Replace Nested Conditional with Guard clses remove annoying control labels: remove Control Flag terminology decompose: break down, split into lidate: Merge eligvisible: suitable, qualified fragment: fragment, fragment nest: Nested guard: defend clause: clause polymorphism: polymorphism assertion: asserted unchecked exception: uncontrollable exception Decompose Conditional Status:You have a complex condition (if-else) statement, then Do not extract functions from the three sections: if, else if, and else.

Invalid lidate Conditional Expression Status:If you have some conditional tests and get the same results, then Combine these tests into a conditional statement and extract the condition into an independent function. Motivation:1. The combined condition code will tell you that "there is actually only one condition check, but there are only a few parallel conditions that need to be checked"-to make the purpose of the check clearer. 2. Prepare the Extract Method. -- Refining the check condition into an independent function is very useful for clarifying the meaning of the Code. It replaces the statement describing "what to do" with "Why to do this ".

The "reason for merging" of condition statements also points out the reason for "not merging:If you think that your checks are indeed independent from each other and should not be considered as the same check, you should not use this reconstruction item. In this case, your code clearly expresses your meaning.

Invalid lidate Duplicate Conditional Fragments Status:Each branch of the conditional statement has the same piece of code. Move this duplicate code out of the condition.

Remove Control Flag Status:In a series of boolean expressions, a variable carries a "control mark ", Replace the control mark with a break or return statement.

Replace Nested Conditional with Guard Clauses Status:The conditional logic in the function makes it hard to see the normal execution path. The Guard clause (Guard clses) represents all special cases.

Two conditional forms: 1. All branches are normal: use [if... else...] 2. Conditional: The condition should be checked separately and immediately returned from the function when the condition is true. -- Such a separate check is often referred to as the "Wei statement" Replace Nested Conditional with Guard clures: special attention is given to a branch.

Replace Conditional with Polymorphism Status:You have an expression in your hand. It selects different behaviors based on the object type. Put each branch of this conditional type into a override function in subclass, and then declare the original function as an abstract function.

Bad taste of this Code:

1. It is too long. When a new video type exists, it will become longer.

2. It has obviously done more than one thing.

3. It violates the single responsibility principle because it has several reasons to modify it.

4. It violates the open and closed principle because it must be modified whenever a new type is added. However, the most troublesome thing is that there are functions with similar structures (_ get type name Rank () everywhere.

Introduce Assertion Status:A piece of code needs to make some assumptions about the state of the program. This assumption is clearly expressed by assertion.

Running result:

Running result:

Collection point:

1. This code can run normally only when a condition is true. -- In fact, the final product of the Program often deletes all assertion. 2. This assumption is generally not explicitly presented in the code. You must read the entire algorithm to see it. -- Sometimes programmers write such assumptions in comments, and assetion is a better technology. 3. assertion is a conditional condition and should always be true. If it fails, it indicates The programmer made a mistake.. 4. assertion can be used Communication and debugging. -- Communication: Helps programmers read and understand the assumptions made by the Code. Debugging: Helps programmers find bugs and catch bugs in the nearest place. 5, Assertion does not change any program behavior.6, Assertion value: Helps programmers understand the necessary conditions for correct code execution.7. We recommend that you use the Extract Method as the assertion criterion to Extract repeated codes from several places into the same function, perhaps only to better illustrate the usage of the criterion. To sum up this chapter, I prefer the "Replace Nested Conditional with Guard clses" method. I often use this method in my usual code. Some people name this method "Wei clause". Another one is that the debugging I often use in php development is var_dump () or print_r (). For the first time, I found that there is also assert in php, which is good! In the course of learning and practice, I also learned many good ways. However, I think that in team development, sometimes the overall situation is "important", encoding according to the team's habits, or you can communicate with the team and get everyone's approval, use this method to facilitate debugging and reading each other's code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.