Good coding turns out to be so easy--code four principles

Source: Internet
Author: User



Wen/Ronlian









Good coding is hard. Even in the case of many coding books, programming specifications, most of the project output code is still not high quality.






Why does this happen? I think the main reason is that the " All- out " will inevitably lead to "not being". Dozens of this code must read books, good thousands of pages, 22 kinds of bad taste, 72 kinds of reconstruction methods, so that most of the programmers overwhelmed, simply remember, not to mention the effective application.












Is there a simple and effective way to quickly improve the team's coding level, everyone can write high-quality good code?






This article summarizes the most important four principles of extracting excellent coding, not exhaustive, but simply apply the four principles, my department team's coding level has improved a level. Some members of the team also feedback that the current encoding level is no longer the same level as the two-month-old encoding.





four principles of excellent coding











Excellent coding principles 1 single level of abstraction











I personally think that the function of a single level of abstraction is the most important principle of good coding, not one! A single level of abstraction means that all operations in a function or method are at the same level.

























As shown, all operations in a function or method are at the same logical level.





Excellent coding principles 2 Minimize Indentation





Overly deep indentation, or "nesting", has plagued the computer industry for dozens of of years and is still one of the culprits for confusing code.






Noam Chomsky and Gerald Weinberg have done a study showing that few people can understand more than 3 layers of nesting (Yourdon 1986a), and many researchers recommend avoiding the use of more than 3 layers of nesting.












As shown, replacing nested conditional expressions with a Guardian statement is one of the essence of minimizing indentation. The Guardian statement frees our gaze from exception handling and concentrates on the normal processing of the code.









The above code can be changed into the following style:























Summary: As shown, simplifying a complex if else statement is basically three means:





    • For top-heavy if else, return with return as early as possible, thus reducing nesting levels;

    • Merge branches. Some of the supporting execution of the same content, can be merged into a branch;

    • Flattened. This example is the best example of flattening.













Excellent coding principles 3 Clear Expression





Good code is not as small as possible, but the less time it takes to understand it, the better.






The code should make it easier for others to understand the time it takes---this is a very important "basic theorem of readability".






The principle of clear expression is one of the important principles of this fundamental theorem.






What is the principle of clear expression? Here are some examples for your reference.




















Excellent coding principles 4 use of auxiliary class splitting











The three principles discussed above are all about writing good functions, writing well-readable lines of code and blocks of code, and now let's focus on the higher levels of code organization, which will involve the most important capabilities in software design- the assignment of responsibilities to classes .






If you write a class that has dozens of or even hundreds of methods, or thousands of lines of code, it's generally a problem with this class design.






Classes, like people, have more responsibilities than leaders, and leaders need secretaries and assistants, as secretaries and assistants help to liberate leaders from Trifles. The same is true for jumbo classes, which also require helper classes to remove things unrelated to the main business logic.






What chores should be handed over to the assistant? Functions that do not produce data, functions that do not modify data, or functions that have input that have explicit output, functions that do not interact with external objects, and so on.






Here are two examples:

























Concluding remarks Some coding Concepts




    • The time it takes to understand a function should be as short as possible.





    • I think everyone can write excellent code, good code is changed over and over. It is allowed to write dirty code first, but it must be refactored.

(For more information about , please follow Developer community, own open door: don't ask me what my name is, others call me Lei Feng )





Good coding turns out to be so easy--code four principles


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.