Details determine quality: poor if/else usage habits

Source: Internet
Author: User

Code refactoring is everywhere, focusing on getting started. We often see the following code:

1. Do not compare bool with false.If (false = isxxx ()){} This is superfluous, and it is hard to please, just as if (! Isxxx ()) 2. Too many if/else nesting causes too many indentation, resulting in Vertigo and difficulty in readingIf () {} else {}}}}} In this case, it should be considered to be extracted into function calls to reduce the direct if/else nested hierarchy. Similarly, while/For should also avoid this situation. 3. The if code block is too long. The else code block contains only two or three lines. The typical header script causes reading difficulties.If (A = B) {// code lines that exceed one screen // code line 1 // code line 2 // code line 3 // code line 4 // code line 5 // code line 6/ /code line 7 // code line 8 // code line 9 // code line 10 // code line 11 // code line 12 // code line 13 // code line 14/ /code line 15 // code line 16 // code line 17 // code line 18 // code line 19 // code line 20 // code line 21 // code line 22/ /code line 23 // code line 24 // code line 25 // code line 26 // code line 27 // code line 28 // code line 29 // code line 30/ /code line 31 // code line 32 // code line 33 // code line 34 // code line 35 // code line 36 // code line 37 // code line 38/ /code line 39} else {// two or three lines of code // code line 1 // code line 2 // code line 3} Because the if code block exceeds one screen, it will greatly increase the difficulty of reading the Code, especially when you need to find the else corresponding to if, you have to be very careful. It is recommended that the IF and else code blocks be reversed when such code is encountered, which is much easier to read, as shown below:If (! = B) {// two or three lines of code // code line 1 // code line 2 // code line 3} else {// code line exceeding one screen // code line 1 // code line 1 line 2 // line 3 // Line 4 // line 5 // line 6 // line 7 // line 8 // line 9 // line 10 // line 11 // line 12 // line 13 // line 14 // line 15 // line 16 // line 17 // code line 18 // code line 19 // code line 20 // code line 21 // code line 22 // code line 23 // code line 24 // code line 25 // code line line 26 // line 27 // line 28 // line 29 // line 30 // line 31 // line 32 // Line 33 // code line 34 // line 35 // Line 36 // line 37 // line 38 // line 39} Of course, it is better to avoid long code blocks and encapsulate them into functions.

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.