good coding style

indentation

Is it better to use the Tab key for indentation, or use 2, 4, or 8 spaces?
ヽ( ̄▽ ̄)و
4 . Even if you like to use the Tab key, there are many IDEs that can be set to define the Tab key extension as a few spaces. The reason for not using the Tab key is that the Tab key will appear as a different length in different situations, and the distance of 4 spaces is the most appropriate in terms of readability.

naming style

As our programs become more and more complex, simple things like naming variables, functions or classes are not so simple. It is very important to use a consistent naming style. The format must be consistent and meaningful, and its function can be seen from the variable name. So, don't use abbreviations as variable names for convenience. If you use res as a variable name, how can others know whether you mean resource , response or reset ? (,´•ω•)ノ"(´っω•`.)

  • Constant: all caps
  • Class name: CamelCase - the first letter is capitalized, and the first letter of the subsequent word is capitalized
  • Method function name: small camel case - the first letter is lowercase, followed by the first letter of the word is capitalized
  • Variable name: small camel case or underscore - all lowercase, words are connected by underscores
  • System reserved: leading underscore (_), do not use leading underscore in naming

naming method

  • It is a good habit to declare all member variables as private . But when it declares member methods to operate private variables, it always names the methods nondescriptly. Under normal circumstances, when we operate the private variables of the class, we will name the method of assigning value to the member variable starting with set , and when taking the value of the private variable, we will name it starting with get .
  • A method returns a bool value indicating yes or no. For this type of method, the name should start with is to represent that this is a method that returns true or false .

Do not write too much per line

When there are too many logical judgments, each logic needs to be placed on a separate line.
Example:
insert image description here
Change:
insert image description here

note

Line comments and block comments are not semantically "a line of comments" or "a large paragraph of comments", but refer to the meaning in the grammar.
We recommend always using line comments, as this makes it very easy to comment a whole block of code. (๑′ᴗ‵๑)

There are only 3 cases where block comments can be used:

  1. When we need to comment out a large piece of code;
  2. When the syntax of the automated documentation generation tool requires the use of block comments, or when it is customary to use block comments as in-code documentation;
  3. When a programming language only has block comment syntax.

In other cases, we recommend never using block comments.

ヾ(◍°∇°◍)ノ:

insert image description here

Guess you like

Origin blog.csdn.net/qq_42571665/article/details/109091860