[The Beauty of Design Patterns Design Principles and Thoughts: Specification and Refactoring] 32 | Theory Five: 20 Programming Specifications that Allow You to Improve Code Quality Quickly (Part 2)

We talked about naming and annotations in the last lesson, and we will talk about Code Style in this lesson. Speaking of code style, it is actually hard for us to say which style is better. The most important thing, and what we need the most, is to maintain a unified style in the team and project, so that the code is written by the same person and is uniform. This reduces reading noise and improves code readability. This is what we want to achieve in practice.

Regarding the code style, I have summarized 6 points that I think are most worthy of attention. Today, I will discuss and learn with you.

1. How big is the class and function?

Generally speaking, the number of lines of code of a class or function should not be too many, but it should not be too few. There are too many lines of code in a class or function. A class has thousands of lines, and a function has hundreds of lines. The logic is too complicated. When reading the code, it is easy to read the back and forget the front. On the contrary, if the number of code lines of a class or function is too small, when the total amount of code is the same, the number of divided classes and functions will increase accordingly, and the calling relationship will become more complicated. When reading a certain code logic, It is necessary to jump between n multiple categories or n multiple functions frequently, and the reading experience is not good.

How many lines of code is the most appropriate for a class or function?

As we mentioned in Lecture 15, it is difficult to give an exact quantitative value. At that time, we also made an analogy with cooking. For the "a little" in "put a little salt", it is difficult for even a chef to tell you a particularly specific amount.

Regarding the maximum limit on the number of lines of function code, there is a saying on the Internet that it should not exceed the vertical height of a display screen. For example, on my computer, if the code of a function is to be completely displayed in the IDE, the maximum number of lines of code cannot exceed 50. I think this statement is quite reasonable. Because after more than one screen, when reading the code, in order to connect the code logic before and after, you may need to scroll up and down the screen frequently. Not to mention the bad reading experience, it is also prone to errors.

For the maximum limit of the number of lines of code of the class, this is even more difficult to give an exact value. We also gave an indirect judgment standard in Lecture 15, that is, when the code of a class makes you feel overwhelmed, and you don’t know which function to use when implementing a certain function, which function do you want to use? I couldn’t find it after searching for a long time. When only a small function is used to introduce the entire class (the class contains many functions that are not related to the implementation of this function), it means that the number of lines in the class is too large.

2. What is the most appropriate length for a line of code?

In the Google Java Style Guide document, a line of code is limited to a maximum of 100 characters. However, different programming languages, different specifications, and different project teams may have different restrictions on this. No matter how much this limit is, generally speaking, a principle we must follow is: the longest line of code cannot exceed the width displayed by the IDE. It is necessary to scroll the mouse to view all the codes of a line, which is obviously not conducive to the reading of the code. Of course, this limit should not be too small, too small will cause many slightly longer statements to be folded into two lines, which will also affect the cleanliness of the code and is not conducive to reading.

3. Make good use of blank lines to divide unit blocks

For a relatively long function, if it can be logically divided into several independent code blocks, and it is inconvenient to extract these independent code blocks into small functions, in order to make the logic clearer, in addition to the above mentioned in the previous lesson In addition to the method of using summary comments, we can also use blank lines to separate each code block.

In addition, between class member variables and functions, between static member variables and ordinary member variables, between functions, and even between member variables, we can add blank lines to make these differences Between the code of the module, the boundary is more clearly defined. Writing code is similar to writing articles. Being good at using blank lines can make the overall structure of the code look clearer and more organized.

4. Four space indentation or two space indentation?

"Is PHP the best programming language in the world? Should code breaks be indented with four or two spaces?" These should be the two most debated topics among programmers. As far as I know, the Java language tends to be two-space indentation, and the PHP language tends to be four-space indentation. As for whether it should be two-space indentation or four-space indentation, I think it depends on personal preference. As long as it can be unified within the project.

Of course, there is another selection standard, that is, to be consistent with the style recommended by the industry and to be consistent with well-known open source projects. When we need to copy some open source code into the project, we can keep the style of the imported code consistent with the code of our project itself.

However, I personally recommend using two-space indentation to save space. Especially when the code nesting level is relatively deep, if the cumulative indentation is large, it is easy to cause a statement to be folded into two lines, which affects the readability of the code.

In addition, it is worth emphasizing that whether you use two-space indentation or four-space indentation, you must not use the tab key to indent. Because under different IDEs, the display width of the tab key is different, some are displayed as four-space indentation, and some are displayed as two-space indentation. If in the same project, different colleagues use different indentation methods (space indentation or tab key indentation), it may cause some codes to be displayed as two-space indentation, and some codes to be displayed as four-space indentation .

5. Do curly braces need to start on a new line?

Should the left curly brace start on a new line? This too is controversial. As far as I know, PHP programmers like to start a new line, and Java programmers like to put the previous statement together. The specific code example is as follows:

// PHP
class ClassName
{
    public function foo()
    {
        // method body
    }
}
// Java
public class ClassName {
  public void foo() {
    // method body
  }
}

Personally, I still recommend putting the brackets on the same line as the statement. The reason is similar to the above, saving lines of code. But the way of opening curly braces on a new line also has its advantages. In this way, the left and right brackets can be aligned vertically, and which code belongs to which code block is more clear at a glance.

However, it is still the same sentence, the curly braces are on the same line as the previous statement, or a new line, as long as the team is unified, the industry is unified, and it is in line with open source projects, there is no absolute distinction between good and bad.

6. The order of the members in the class

In the Java class file, first write the package name to which the class belongs, and then list the dependent classes introduced by import. In Google Coding Standards, dependent classes are arranged alphabetically from small to large.

In a class, member variables come before functions. Between member variables or between functions, they are arranged in the manner of "static first (static function or static member variable), then ordinary (non-static function or non-static member variable)". In addition, between member variables or functions, they will be arranged in order of scope from large to small. Write public member variables or functions first, then protected, and finally private.

However, in different programming languages, the arrangement order of the internal members of the class may be quite different. For example, in C++, member variables are habitually placed behind functions. In addition, the arrangement order between functions will be arranged according to the size of the scope we just mentioned. In fact, there is another arrangement habit, which is to put functions that have a calling relationship together. For example, if a public function calls another private function, put the two together.

key review

Well, that's all for today's content. Let's summarize and review together, what you need to focus on. In this lesson, we will tell you the points of attention in code style through 6 points.

1. How big is the function and class?

The number of lines of code of a function should not exceed the size of one screen, such as 50 lines. Class size limits are more difficult to determine.

2. What is the most appropriate length for a line of code?

It is best not to exceed the width displayed by the IDE. Of course, the limit should not be too small. If it is too small, many slightly longer statements will be folded into two lines, which will also affect the cleanliness of the code and is not conducive to reading.

3. Make good use of blank lines to divide unit blocks

For relatively long functions, in order to make the logic clearer, blank lines can be used to separate each code block. Inside the class, between member variables and functions, between static member variables and ordinary member variables, between functions, and even between member variables, you can add blank lines to make the boundaries between codes of different modules more clear. clear.

4. Four space indentation or two space indentation?

I personally recommend using two-space indentation, which can save space, especially when the code nesting level is relatively deep. In addition, it is worth emphasizing that whether you use two-space indentation or four-space indentation, you must not use the tab key to indent.

5. Do curly braces need to start on a new line?

Personally, I still recommend putting curly braces on the same line as the previous statement, which can save lines of code. However, putting curly braces on a new line also has its advantages, that is, the left and right braces can be aligned vertically, and it is more clear at a glance which code belongs to which code block.

6. The order of the members in the class

In the Google Java programming specification, dependent classes are arranged alphabetically from small to large. In the class, write member variables first and then write functions. Between member variables or functions, first write static member variables or functions, then write ordinary variables or functions, and arrange them in order according to the size of the scope.

Today I said that there is no right or wrong or good or bad in all coding styles, as long as they can be unified in the team and project, but it is best to be consistent with the style recommended by the industry and the code style of open source projects.

class disscussion

Talk about the coding style of the programming language you are familiar with, such as four-space indentation or two-space indentation? Try to organize a programming specification for your project.
Welcome to write down your answers in the message area, communicate and share with your classmates. If you gain something, you are welcome to share this article with your friends.

Guess you like

Origin blog.csdn.net/qq_32907491/article/details/129891262