PHP program in the various if judgment cause the program messy, there is no good solution?

Source: Internet
Author: User
Due to the pressure from the need side, the need to add a variety of if judgment in the program caused the program messy, do not know whether there is a scientific way to solve the problem.

Reply content:

Just answer the code skills, do not answer the schema, I think that there is no standard answer, of course, the small skill also depends on the specific use of the scene.

Go Ternary
if (A)  $var = xxxelse  $var = ooo↓$var = A ? xxx : ooo
Learn more about OOP, Factory mode \ policy mode \ Adorner mode. You can use polymorphism instead of conditional judgment, and refer to the implementation of design patterns such as simple Factory mode or strategy mode.
Switch can not write as many judgments as if-else, but it is still not able to meet the requirements of closed, extended open. Is it estimated that nesting if causes clutter? Exception Problem Solving reference refactoring: improving the design of existing code chapter 9th-Simplifying conditional expressions
Replace Nested Conditional with guard claused (replace nested conditional expressions with Guardian statements) You can look at the notation of large frames!! Recommended zendframework with arrays and closures I think a good, high-reading program or do not omit curly braces, do not use too much three mesh operations, but also less with && this logical operator to replace the conditional judgment. Really want to make if concise, I think should be less with the if after the else, give a lot of return value, say the next judge in another if block, in addition, we should learn to design patterns, reduce if statement less than set. Judge1

if (!expression) {

Code

[Return/break]}



Judge2

if (!expression) {

Code

[Return/break]}

... switch, haha
  • Related Article

    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.