Optimized code in a lot of if / else, what is your plan?

A quick iteration of the project, after a long time, the code may be filled with a lot of if / else, nested 6,7 layer, a function hundreds of lines, Jane! straight! Look! dead! people!
In fact, this is fairly good, more serious nest I have seen, people took to this project, the heart should be desperate.
This happens for many reasons
  • Design is not perfect
  • Demand consider incomplete
  • Developers change
But the most deadly is the "lazy", no dream is a very terrible thing!
Too lazy to optimize the early iterations, to a demand, a plus if, over time, they strung together a pyramid.
Once the code has been difficult to maintain the level of complexity to the only mind to reconstruct optimization. Well, what solutions can be elegantly optimize away those extra if / else?

01 ahead of return

This determination condition is negated practice, it will be more clear in the code logic expression, see the following codes:
In fact, every time I see this kind of code above, I was scratching my heart, can first judge! Condition, get rid else.

02 strategy pattern

Have such a scenario, take a different logic based on different parameters, in fact, this scenario is common. The most common realization:
See the above code, there are four strategies, there are two optimization schemes.

More than 2.1 state

The achievement of specific policy objects stored in a Map, the optimized
This optimization program has an above drawbacks, in order to be able to quickly get the strategy to achieve the corresponding need to map objects to save the policy, when adding a new policy when the need to manually add to the map, it is easy to be ignored.

2.2 Enumeration

Found that many students do not know the method can be defined in the enumeration, enumeration defined here as a representation of the state, in addition to a run method can be achieved.
Redefine strategy enumeration
After optimization of the code by the following enumeration

03 learn to use Optional

Optional mainly used to determine non-empty, because it is jdk8 new features, so it is not particularly used, but with it is really cool. Before use:
If, after the user login is empty, execute action1, otherwise execute action 2, using the Optional optimized, so that non-null check more elegant, if indirectly reduce operating

04 Tips array

From google explained that this is a programming model, called the table-driven method, essentially a table of information from queries instead of logic statements, such as there is such a scene, the number of days to get through the month of May, the demonstration only as a case, the data is not rigorous. General implementation:
After the optimized code

05 Conclusion

if else as each programming language are an integral part of a conditional statement, when programming will be a lot of use. General recommendations nest should not exceed three, if a piece of code there are too many nested if else, the readability of the code will decline rapidly, post-maintenance difficulty is also greatly improved. If you have any other tips are welcome to leave a message! ! !


Guess you like

Origin juejin.im/post/5d2c47346fb9a07ebf4b9f53