[Code cleanliness] self-perception

Source: Internet
Author: User

The program is first for people, followed by computers. A good program should be close to the minimum understanding time, keeping the code neat for reading, maintenance, and, in most cases, not causing a lot of performance damage. Around the minimum understanding time, we should be able to think of the following good code features:

  • The function name should fully express the meaning, if there are words such as and or or, or process,execute, it is necessary to split, because a function does only one thing.
  • Try to be as simple as possible. A function one thing, a line of code one thing, a loop a thing, a variable one thing.
  • The body of the function should be at an abstraction level for easy comprehension.
  • Try to use positive variables and function names.
  • Even if there is only one sentence in the function, if the function name is more convenient to understand, then a function can be written.
  • Minimize the complexity of functions and cyclomatic complexity. The complexity of the function has the number of lines of code, number of parameters, number of calls, number of operators per line, number of reversed statements, number of nested layers, number of variables, variable reference span, recursion, function exit, branch statement scale, etc. Cyclomatic complexity can be imagined, refers to a circle, if inside another if, like a circle inside another circle. If,while,case,catch, ternary operators, and so on. We write programs, each function as far as possible to keep the cyclomatic complexity less than 5, it is not prone to error.
  • For the public method, mainly to reflect the process, like a directory, here can actually be understood as the hierarchy of the system, in the logical layer should be like a directory, in the data operation layer, then the various operations of the data, the logic layer calls the data operations layer, it is convenient to understand the business like reading books directory, At the same time, you can quickly navigate to specific business details.

In short, writing a good function is an important aspect of writing good code, writing functions to pay attention to is simple, simple, and then simple! Short, short, and short! Like writing beautiful prose, write a story, make others easy to read, code as far as possible self-explanation, so easy to maintain, greatly reduce maintenance time, improve the efficiency of troubleshooting, error-prone, code more flexible.

Writing code is divided into two kinds, one is complex, complex to no obvious flaw, one is simple, simple to obvious no defect. When we really write good code, it is also very convenient to see if the code is defective.

[Code cleanliness] self-perception

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.