The four simplified conditional expressions for "refactoring-improving the design of existing code" in PHP _php tutorial

Source: Internet
Author: User
Mind Mapping
Click to see a larger image.


Introducing conditional logic can be very complex, so this chapter provides some refactoring techniques that are specifically designed to simplify them. Full text brief(You can skip the following) Core refactoring: Decompose conditional--separates the "Switch logic" (switching logic) from the "Operation Details" (details). Multiple tests have the same result: Consolidate Conditional expresssion The repeating ingredient in the condition code: Consolidate Duplicate Identification Special case: Replace Nested Conditional with Guard clauses removes annoying control tags: remove control flag professional terminology decompose: decomposition, separation consolidate: Merging eligible: Suitable, Qualified fragment: fragments, Fragment Nest: Nesting guard: Defending clause: Clause polymorphism: polymorphic Assertion: Assertion unchecked exception: Uncontrolled exception decompose Conditional Status:You have a complex condition (if-else if-else) statement, then extracts the function from the IF, else if, or else three paragraphs respectively.

Consolidate Conditional Expression Status:You have some condition tests, all get the same result, then combine these tests into a single condition, and refine the condition to be called a stand-alone function. Motive:1. The combined condition code will tell you that "there is actually only one condition check, but there are several side-by-side conditions that need to be checked"--to make the check more clear. 2, prepare for Extract method. --refining the inspection conditions into a separate function is useful for clearing the meaning of the code. It replaced the phrase "what to do" with the words "why."

The "Consolidation justification" of conditional statements also points out the reason for "don't merge": if you think that your checks are really independent of each other and indeed should not be considered the same, then don't use this refactoring. Because in this case, your code has clearly expressed its meaning.

Consolidate Duplicate Conditional Fragments Status:With the same piece of code on each branch of the conditional, Move the duplicate code out of the condition.

Remove Control Flag Status:In a series of Boolean expressions, a variable has the effect of a "control tag", so Replace the control token with a break statement or a return statement.

Replace Nested Conditional with Guard clauses Status:The conditional logic in the function makes it difficult to see the normal execution path. Use the Guardian statement (guard clauses) to express all special cases.


Two forms of conditional: 1, all branches are normal behavior: use [if ... else ...] 2. The conditional formula is extremely rare: The condition should be checked separately and returned from the function immediately when the condition is true. --Such a separate check is often referred to as the "Guardian statement" Replace Nested Conditional with Guard clauses essence: give special attention to a branch.

Replace Conditional with polymorphism Status:You have an expression on your hand that chooses a different behavior depending on the type of object. Place Each branch of the conditional into an overwrite function within a subclass, and then declare the original function as an abstract function.

The bad taste of this code:

1, it is too long, when the video has a new type of time, it will become longer.

2. It has obviously done more than one thing.

3. It violates the principle of single responsibility, because it has several reasons for amending it.

4. It violates the open closure principle because it must be modified whenever a new type is added. But perhaps the most troublesome is the function of similar structures (_get type rank ()) everywhere.

Introduce assertion Status:A piece of code needs to make some assumptions about the state of the program, so This hypothesis is clearly represented by assertions (assertion).

Operation Result:

Operation Result:

Pick Point:

1, often have such code, only when a condition is true, the code can run correctly.  -in fact, the final product of the program is often removed assertion. 2, such assumptions are usually not clearly shown in the code, you must read the entire algorithm to see.  --Sometimes programmers write such assumptions in comments, and assetion is a better technique. 3, assertion is a conditional formula, should always be true. If it fails, it indicates that the programmer made a mistake.。 4. Assertion can be used as Communication and commissioningThe auxiliary. --Communication: It helps programmers to read assumptions about what the code does.  Debugging: Help programmers to find bugs, can catch the bug in the nearest place. 5. assertion does not alter any behavior of the program. 6. Assertion Value: Helps programmers understand the necessary conditions for the code to run correctly. 7, it is advisable to use the assertion conditional formula extract Method, in order to extract several places of the duplicate code in the same function, perhaps just to more clearly explain the use of the conditional formula. Summing up this chapter I prefer the "Replace Nested Conditional with Guard clauses" This way, I often in the code of the usual use, but also someone to this way named "Wei clause." Another is that I often used in PHP development debugging is var_dump () or Print_r (), I also found in PHP for the first time there is an assert this way, good! In the process of learning and practice, I also learned a lot of good ways. But I think in the team development, sometimes still "big picture", according to the habit of the team to code, or you can communicate with the team, get everyone's approval, in the use of this method, so that everyone debugging and reading each other's code is more convenient.

http://www.bkjia.com/PHPjc/325364.html www.bkjia.com true http://www.bkjia.com/PHPjc/325364.html techarticle Mind Map Click to see a larger image. Introducing conditional logic can be very complex, so this chapter provides some refactoring techniques that are specifically designed to simplify them. Full text brief (you can directly ...

  • 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.