The most important little thing for programmers - clean code

Why write clean code

Whether you are a newcomer to the industry or a veteran who has been developing for many years, reading this blog, you must have encountered "ancestral code" (some people also call it a mountain of shit, which is rough but not rough) and have a headache. Every modification to the code affects two or three other codes. Every time you add or modify the code, you have to be aware of the mess. Otherwise, as the mess gets bigger and bigger, it will never be cleaned up. Finally helpless. I would rather rewrite this function myself than touch it. Being held back by ancestral code, developers are under deadline pressure and have no choice but to continue to pile up chaos. In fact, creating chaos will not help you meet the deadline. As chaos increases, team productivity continues to decline toward zero. The only way to maintain and improve efficiency is to always keep the code as clean as possible.

name

worthy of the name

  1. Choosing a good name takes time, but the time saved is more than the time spent.
  2. If the name needs to be supplemented by comments, it is not worthy of the name.

make meaningful distinctions

  1. If the names are different, the meanings should also be different.
    For example: add and insert
    both mean to add. We need to try to keep the usage consistent when using it. We cannot use insert to mean adding in the next method of using add.
  2. Nonsense is redundant.
    Example: The word Variable should never appear in a variable name, and the word Table should never appear in a table name.

Class name, method name

  1. Class names and object names should be nouns or noun phrases.
    Example: Customer, WikiPage
  2. The method name should be a verb or verb phrase.
    Example: postRequest, deletePage

function

short

  1. The first rule of functions is to be short. The second rule is to be even shorter.
  2. Each line should not exceed 150 characters, and the total length of the function should be capped at 20 lines.
  3. What it should be: Every function is self-explanatory, every function says only one thing, and every function takes you to the next function in turn.

only do one thing

A function should do one thing, do it well, and do one thing only

Comment

Annotation type

  1. Line comments
    As shown below, each line of code uses comments to describe clearly what is done.
    image.png
  2. Block comments
    are as shown below. Add a comment description on a method or a certain logical operation.
    image.png

good comment

  1. legal information
  2. Informative comments
  3. explanation of intention
  4. interpret
  5. explain
  6. TODO comments

bad comments

  1. Mumbling to oneself, redundant comments, nonsense comments
  2. misleading comments
  3. Log comments
  4. commented out code

Format

Purpose

Code format is about communication. Features written today are likely to be modified in the next version, but code style and readability will affect maintainability and scalability.

vertical format

  1. Use a blank line between the import declaration and each function.
  2. Variable declaration, variable declaration should be as close as possible to the location where it is used
  3. Entity declaration, entities should be declared at the top of the class
  4. Related functions, if a function calls another, they should be put together, and the caller should be placed above the callee as much as possible.

landscape format

Use spaces to connect closely related things together and separate weakly related things (formatting code, idea shortcut key: ctrl+alt+L)

team rules

Every programmer has their own favorite formatting rules, but if you work in a team, the team has the final say. A group of developers should agree on a formatting style, and every programmer should adopt that style.

Summarize

image.png
Most of the content shared today comes from the book "Clean Code". If you want to know more methods and techniques of clean code, you can study this book in depth, and you will benefit a lot.

The above is the content shared today. If you find it useful, you can follow it and give it a like.

Guess you like

Origin blog.csdn.net/zwb568/article/details/129189656