How to write well-formulated code

The way to clean and standardized code

meaningful naming

  1. Knowing the meaning by seeing the name: You can easily know the general purpose based on the name, instead of using pinyin abbreviations, which is confusing.

  2. Meaningful distinction: When the uses are different, remember to distinguish the names to avoid misuse due to similar names.

  3. Avoid using encoding (letters, numbers, suffixes, etc.): the uses and differences cannot be well understood, which increases the cost of understanding.

  4. Class name, object name, use noun or noun phrase

  5. Method names should be verbs or verb phrases: it is easier to understand what the method does

  6. Each concept corresponds to a word, which is consistent and convenient for subsequent searches:

    For example, search includes get/find/obtain/search, etc. It is recommended to use unified concepts to reduce understanding and communication costs.

function

  1. Be short and avoid overly long functions. If they are too long, they can be extracted into small functions: to enhance readability, and small functions can be reused, reducing the cost of modifying the main function.
  2. Single responsibility: a function only focuses on one thing to avoid bloated functions
  3. Input parameters should be as few as possible: too many parameters increase the cost of understanding, and it is easy to pass wrong parameters; if there are too many parameters, consider encapsulating them into an entity
  4. Use exceptions instead of returning error codes: The judgment of error codes will lead to a deeper nested structure. The caller needs to understand the error codes, and it is difficult to judge whether the error code is an exception or a business error.
  5. Extract the try/catch code block into a new function, and try/catch only includes the exception code: reduce the granularity of try/catch and maintain performance
  6. DRY principle to avoid duplication of code: duplicate code can be extracted and reused, reducing the cost of subsequent code changes
  7. Use third-party libraries: Prioritize the use of methods provided by class libraries. Codes that have been verified multiple times have a lower probability of bugs and can be used selectively; avoid pitfalls and reinventing the wheel by writing your own code.

Comment

  1. Comments cannot beautify bad code: elegant code is the foundation, don’t try to cover up bad code with too many comments
  2. Use good code names that are self-explanatory: avoid too many redundant comments
  3. Warning places must be commented: add comments in key places to encourage attention when making changes.

Format

  1. vertical format
    1. learn from newspapers
    2. Code blocks of different concepts are separated by blank lines: improving readability
    3. Closely related code should be close to: high cohesion
    4. vertical distance
      1. Local variables: as close as possible to where they are used
      2. Entity variables: declared at the top of the class
      3. Related functions: Functions of the same type call each other, and the caller is placed above the callee as much as possible.
      4. Related concepts: related code should be put together
  2. landscape format
    1. Avoid single lines that are too long
    2. Horizontal alignment
    3. Pay attention to the indentation level

coding

  1. Guard statement return: For conditions that do not meet the requirements, use guard statements to quickly fail and return to avoid excessive branch judgments.
  2. Avoid magic values: The meaning of magic values ​​may only be known at the time of writing, and subsequent understanding is extremely costly; it can be extracted into a constant class or enumeration for unified management
  3. When the value of a member variable does not change, it is preferred to define it as a static constant.
  4. Pay attention to checking the null pointer, especially when calling in a chain
  5. Minimize the visibility of class members and methods: the least known principle to avoid incorrect calls
  6. Minimize double counting of variables

References
"The Way to Clean Code"
How to Write Elegant Code
Summary of 25 Bad Code Smells + Optimization Examples

Guess you like

Origin blog.csdn.net/weixin_40709965/article/details/126615053