1. Code should be easy to understand

  • The standard for good code
Write your code in a way that minimizes the time it takes for others to understand it. 2. Simplify your codeCopy the code

2. Put information in your name

  • Choose professional terms

    eg: get/fetch

  • Avoid generic nouns

    eg: foo/tmp

  • Concrete instead of abstract

  • Use prefixes/suffixes to attach information to names

    eg: start_ms

  • Determine the length of the name

    1. Use specific names for larger scopes

    2. Small scopes can have short names

  • Use the name format to express meaning

3. A name you can’t misunderstand

Max /min, start/stop, beigin/end, etc., are used when defining the upper and lower limits of a value. Boolean values can be defined using words such as is/hasCopy the code

4. The aesthetic

  • Use a consistent layout so that the reader will quickly get used to the style
  • Make similar code look similar
  • Groups related lines of code into blocks
  • Consistent code compartments are more important than the “right” code style

5. What kind of comments should be made

The main idea: Don't comment on facts that can be quickly inferred from the code itself. A good name is more important than a comment.Copy the code
  • What doesn’t need a comment
1. Facts that can be quickly inferred from the code itself. 2Copy the code
  • You should write down your thoughts
1. Reasons why the code is written the way it is 2. Flaws in the code 3. Anticipate which parts will confuse the reader. 5. Summarize code blocks with commentsCopy the code

6. Write concise and comprehensive notes

/**Function(/**arg*... /)*/ 4. Declare high-level meaning rather than details of the codeCopy the code

7. Make the control flow readable

1. When writing a comparison, write the changed value on the left and the stable value on the right 2. 3. Minimize the use of operators such as do/while,goto, etc., which cause code to become less readable 4. Avoid deep nestingCopy the code

8. Break up very long expressions

Key idea: Break up very long expressions into more understandable chunksCopy the code

Variables and readability

  • Reducing the variable
  • Reduce the scope of each variable
  • It’s better to write a variable only once

10. Extract irrelevant sentences

Specific code to do specific things, as far as possible not to business irrelevant code accounts for a lot of space, you can put forward tool functions, simplify the interface code

11. Write less code

Question and break down your needs

2. Keep your code base small and create as many tools as possible to minimize duplication of code

3. Be familiar with useful tool libraries