C ++ Coding Standerd (1.1-1.9)

xiaoxiao2021-03-13  181

Translation: Derry Zhang / 2006-03-17

Note: In fact, this book has already been translated, and the reason it is once again translated, and it is this book, and I have the need to improve the needs and ideas in English reading, so I am not afraid of waste time and energy. To do this. If you see any of these errors, please inform me that you will be grateful!

Name Specification (Names)

1.1 Suitable Name (Make Names Fit)

The name is a programmed heart. In the past, as long as I know that someone's real name can get magic from that person. If you can think of a real name for a thing, then you will give you your own and later people with the magic of these words. Don't laugh! The name is actually the result of the long-term in-depth thinking of the ecological environment process of something. Only those programmers who really understand throughout the system can create the name of the system. If the name is appropriate, then you need anything naturally soft together, the relationship is clear, the meaning is traceable, and can be expected and reasoning for ordinary people. If you find that your name is "Thing" or "doit", then you probably need to re-examine your design.

Class name

You should rename it after you completely understand the true meaning of the class. If you can't think about it, then this means that you have not had enough understanding of this design. The mixing name of more than three words means that your design may confuse the individual entities in the system. This is what you need to retrurit your design. Try a CRC card discussion to verify that your object is responsible for it. Avoiding the names of their parent classes in derived classes, each class should have its own independent class name, which is related to which parent class is independent. The suffix is ​​sometimes effective. For example, if you use a proxy (agent) in your system, you can use the true information such as DownloadAgent.

Method and function name

Generally speaking, each method and function is performing a behavior, so its name should also be clearly explained: CheckForerrorS () is better than ErrorCheck (), DumpDataTofile () is more appropriate than DataFile () . This also makes functions or data objects easier to distinguish. Class names are generally nouns. By using the verbs in the function name, and follow other naming rules, then your code readability can be greatly improved (more natural). The suffix is ​​sometimes very effective: Max - indicates a minimum CNT - counter variable of a variable, the current value of the CNT - counter variable, for example: Retrymax indicates the maximum number of "retry", Retrycnt means that the current retry is sometimes retrying. Sometimes The prefix is ​​also useful: IS - is used to ask questions, once you find IS, it is proposed a question GET - Get Value Set - Settings, for example: IshitRetrylimit.

1.2 In the name included in the name

If the variable indicates the meaning of time, weight, or other units, then the unit is added to the variable name. This allows developers to identify more. E.g:

UINT32 Mtimeoutmsecs; uint32 mmyweightlbs;

Transforming a variable into a class will be a better approach, so illegal transformation can be captured.

1.3 Do not use all uppercase words

Imagine that when you use the acronym, you will choose to use all uppercase letters, not the first letters, and the back characters are lowercase?

Explanation

People's intuition is very different when using the acronym. The best choice is to unify thinking, so that the variable name can be fully predicted. For example, NetWorkabcKey. Please note that how KEY in the ABC is to distinguish between KEY? Some people don't care about it, but others are very disgusting. Therefore, you may find several rules in different codes, you never know what name is called this variable. Example

Class Fluidoz / / Not Fluidoz Class Networkabckey / / Instead of NetWorkAbcKey

1.4 name naming

Use uppercase letters as a word segmentation method, other characters will be lower than the first character of the class name. Don't use the underline ('_')

Explanation

In numerous naming strategies, many people find that this method is the best.

Example

Class Nameonetwo

Class name

1.5 class library naming

Since Name Spaces is increasingly widely implemented, there should be a problem with other developers or organization name conflicts when using the namespace. When using the namespace, a common way to avoid class conflicts is to add a unique string identifier before class name. Two characters are enough, of course, several characters have nothing to do again.

Example

John Johnson's full data library can use JJ as a prefix, so class named is as follows:

Class JjlinkList {}

1.6 class method named

Name the law similar naming

Explanation

In many different naming strategies, many people find that this method is the best.

Example

Class Nameonetwo

{

PUBLIC:

INT DOIT ();

Void HandleError ();

}

1.7 class attribute naming

The attribute name should be added to the character "m". Use the same rules as the class name later after "M". "m" is the most preferred name modifier, such as the front of the pointer modifier "P" should also be brought.

Explanation

In front of the attribute, add a "M" to avoid confusion between the method name. This is especially true because of your methods and properties, especially for those access programs.

Example

Class Nameonetwo

{

PUBLIC:

Int varAbc ();

Int errornumber ();

Private:

INT mvarabc;

Int MerrorNumber;

String * mpname;

}

1.8 method of parameter name

The first character should lowercase, just like a class naming rule, and the subsequent words first characters should be capitalized.

Explanation

You can declare which parameters are passed through variables, while you can also have no ambiguity and name of the same name.

Example

Class Nameonetwo

{

PUBLIC:

Int Startyouregines

Engine & RsomeEngine,

ENGINE & RANOTHENGINE

}

1.9 Stack variable name

All using uppercase characters using underscore '_' as a word separator

Explanation

In this way, the scope of the variable seems to be obvious in the code, and all variables in the code can be obvious.

Example

int

NameOnetwo :: HandleError (int ErrorNumber)

{

Int error = oserr ();

Time Time_OF_ERROR;

ERRORPROCESSOR ERROR_PROCESSOR; TIME * P_OUT_OF_TIME = 0;

}

Although it seems that the standard pointer symbol is not very satisfactory, it has ensured at least consistency.

So how do you handle static variables? Because there is no reason to declare local static variables in the function, there is no need to invent a new syntax for this purpose.

转载请注明原文地址:https://www.9cbs.com/read-129048.html

New Post(0)