Reading Report-art of code modification (ii) continued 2

Source: Internet
Author: User

Here, as the second extension of (ii), we will continue to process complicated nested if Else. In order to maintain the length of the article will not be too long, it will be given in the form of a new article.

The basic method to simplify complex if else statements

  1. Use return to quickly return if else with heavy head and foot to reduce the number of nested layers.
  2. Merge branches. Some branches have the same execution content, which often means they can be merged into one branch.
  3. Flat.

The last example is provided here, which is also excerpted from a casual search on the Internet.

Original code
List <tworkflowwork> wfwlist = errorprocessingservice. findworkflowworkbyworktype ("7", workbillcode. GETID () + ""); Boolean flag = false; If (wfwlist! = NULL & wfwlist. size ()> 0) {for (INT I = 0; I <wfwlist. size (); I ++) {tworkflowwork WFW = wfwlist. get (I); If (WFW! = NULL) {// The current to-do list is the original verification. If the verification is completed, the original verification to-do list if (WFW. getstatus () is activated ()! = NULL &&! "". Equals (WFW. getstatus () {If ("1 ". equals (WFW. getstatus () {flag = true; break ;}}} if (! Flag) {// if the original verification to-do is complete, an original verification to-do tworkbillcode WB = errorprocessingservice is generated. findworkbillcodebyparameters (flow. getbussid (), flow. getbusstype (); WB. setismatchpage ("1"); errorprocessingservice. updateworkbillcode (WB );}}


Old rule: Let's briefly comment on this code: Alas ~~~~~~~
  • Magic number "7", "1"
  • Massive empty object judgment. This is not a representation of rigor, but a problem with code design.
  • The extremely deep if else Nesting is mainly an extremely important if statement.
First, if (wfwlist! = NULL & wfwlist. size ()> 0) {super head-heavy, return directly using return second, for (INT I = 0; I <wfwlist. size (); I ++) {tworkflowwork WFW = wfwlist. get (I); can be replaced with for (tworkflowwork WFW: wfwlist) by Java's sweet syntax, and then, if (WFW! = NULL) You can use the Wei statement for processing, but here you cannot use return to return directly. Instead, you should use continue to final, and then the if status judgment can be combined.
if(wfw.getStatus()!=null&&!"".equals(wfw.getStatus())&& "1".equals(wfw.getStatus())){flag=true;break;}
After careful analysis, it is not difficult to find WFW. getstatus ()! = NULL &&! "". Equals (WFW. getstatus () is redundant. So the code looks like this.
List <tworkflowwork> wfwlist = errorprocessingservice. findworkflowworkbyworktype ("7", workbillcode. GETID () + ""); Boolean flag = false; If (wfwlist = NULL | wfwlist. size () = 0) {return;} For (tworkflowwork WFW: wfwlist) {// The current to-do is the original verification. If the verification is completed, the original verification to-do if (WFW = NULL) {continue;} If ("1 ". equals (WFW. getstatus () {flag = true; break ;}} if (! Flag) {// if the original verification to-do is complete, an original verification to-do tworkbillcode WB = errorprocessingservice is generated. findworkbillcohidebyparameters (flow. getbussid (), flow. getbusstype (); WB. setismatchpage ("1"); errorprocessingservice. updateworkbillcode (WB );}


Next, I would like to extract a method for the first loop, because the actual operation is like querying-query whether all the originals have been verified. After being changed to a query, not only does it clearly indicate that no comments are required, but it can also remove the annoying flag and wfwlist null judgment. Unfortunately, I don't have all the code. I don't know if this modification will cause any side effects. Because the comment "if the original verification to-do is complete, an original verification to-do is generated" cannot be very accurate. What does this "generate one" mean to generate the next one? Therefore, the ideal situation is to make the code self-comment. If you write comments, you must maintain both the code and the comments. Incorrect comments are worse than no comments. However, we have simplified the complicated if Else. Finally, I will try again. coding in the interview age is indeed an essential part: From this code, we can see that
  • The original author is not familiar with the basic syntax of Java; // The loop statement is poor, probably not long after it is transferred from C.
  • I have been trained by the system or carefully read related coding books; // a large number of magic numbers, wfwlist. Size () = 0 instead of isempty ()
  • The amount of code is small. // This code snippet is not long, but it is very confusing. In addition, the Code doesn't have a strong sense of attention, and various high-level interfaces are mixed with the underlying interfaces.


Finally, let's make a summary. This section mainly serves as an example to discuss the processing of complex if else statements with a tooth shape. There are three basic methods
  1. Use return to quickly return if else with heavy head and foot to reduce the number of nested layers.
  2. Merge branches. Some branches have the same execution content, which often means they can be merged into one branch.
  3. Flat.
For example, I usually try to rebuild and list them step by step.
  1. If else is modified, the condition statement is rewritten. However, on the other hand, I believe you have noticed that understanding is essential. It is very tedious to make logical deformation completely dead (this is particularly evident in the second example ), sorry, this is the "Art of code modification ". The eloquence is not good, but the practice depends on the individual.
  2. Do not perform immature optimization. Relatively speaking, the definition of the Code is higher than the performance optimization. In many cases, the two are not in conflict. When the code is more fresh, there are usually better optimization solutions.
  3. Some code styles in the example, such as return quick return, are not acceptable to everyone. While reserving differences, this is my motto.
  4. There are more than the above methods to simplify if else. However, these are still very practical. Welcome to personal worship, do not be skeptical, do not have brains. (Joke, do not take it seriously, just want to say, "Don't be afraid, don't regret". At the beginning, do not doubt too much despite doing so)

Finally, if you are still interested in refactoring, you can take a look at refactoring-the path to improving my legacy systems. This is a training ppt and is well written. Recommended here

Reading Report-art of code modification (ii) continued 2

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.