The way to clean code--format

foreword

When writing code, you should not only focus on whether the code function is complete, but also whether the format of the code conforms to the specification. Maybe you think that "making the code work" is the top priority that developers should consider. However, if there is no good code format, In future iterations and revisions, the readability of the code can have a profound impact on the behavior of subsequent revisions


Illustration from The Way to Clean Code

The vertical format

1. Read from top to bottom

When you read the newspaper, you want to have a headline that tells you the theme of the newspaper, how the first paragraph is the outline of the whole story, but hides the specific details, and then continue to read, the details are slowly presented, until you Find out when, where, and more the entire story takes place.
Newspapers are made up of many articles, most of them short and exquisite, a few a little long, but few take up a full page, and if the newspaper publishes only one particularly long piece of news, no one will read it.
Our code should do the same, from top to bottom, left to right.

2. Conceptual vertical separation

The defined functions should not be next to each other, and each function should be separated by a space sign;

3. Approaching vertically upwards

Closely related code should be close to each other, and member variables should not leave spaces

4. Closely related functions are close to each other

Avoid jumping from one function to another, which keeps jumping. When a function calls another function, try to ensure that the called function is next to the caller, so that it is easier to read.

5. Member variables should be declared in the class header

The variables of the function should be declared at the beginning of the function. If you need to declare it in the function, consider whether the function needs to be split into multiple functions for execution;

The horizontal format

1. The width of a line of code

Keep lines of code short, avoid lines of code that exceed your screen, if so, consider wrapping or modifying more refined variable names

2. Separation and proximity in the horizontal direction

Addition, subtraction, multiplication, and assignment conform to (+, -, ==, =) symbols, and spaces should be added directly to indicate low priority. There should be no spaces between multiplication and division (*, /) to indicate high priority. A space should be added after the comma for function parameters.

3. Horizontal alignment is not necessary

don't do it, it's not necessary
insert image description here

4. Code indentation

Even if the code is only one line, you should write square brackets, line breaks, and don't save space

5. Avoid loops, judge statements, and the function body is empty

In multiple if judgments, avoid executing nothing in a certain statement

Summarize

After writing the code, finally review the code, check the vertical format from top to bottom, and check the horizontal format from left to right

Guess you like

Origin blog.csdn.net/qq_45171957/article/details/122612146