Learning Boost II: Looking at Coding Habits from Appendix 3

Appendix C Keyword Discussion

In the C++11 standard (C++11.2.12), a total of 73 keywords (keyword), 2 "quasi" keywords (identifiers with special meaning) and 11 operator substitution words (alternative representation) are defined. [1].

Although there are many keywords in the C++ standard, not all of them are useful. Some of them have been discarded, and some of them will lead to "bad code smell" due to improper use. The author will talk about his understanding of them during years of development , only for the words of the family.

  1. Deprecated keywords
    register. It has been officially deprecated in C++11, and the word should no longer appear in current C++ code.

  2. Disabled keywords
    export. C++98 introduced this keyword, it was deprecated but retained in C++11.

  3. Keywords not recommended
    C++ inherits many keywords from C, so it also inherits some disadvantages of process-oriented C language. Overcome some confusing pitfalls.

goto. Of course, the first thing to bear the brunt is gotothat the process confusion it brings is well known. Keywords such as break, continue, returnand so on should be used to control the program flow and make the code logic clearer.

switch-case. Multi-branch switch statements will also make the code difficult to understand, which is not conducive to maintenance. In object-oriented languages, the use of state patterns can be used to eliminate the use of switch-case, and can also be used if+returnto map+functionreplace switch-casestatements.

else. Abusing if-elseand if-else的多重嵌套being switch-caseequally disgusting exists, it introduces multiple logical conditions, makes the flow of the program very confusing, and will lead to hidden dangers of missing logical judgments. Eliminating elsekeywords requires a clear mind, careful analysis of business logic, and design of code structure. In most cases, it can be used toif end functions in a timely manner in statements .return

while/do. while{}and do{}whileloops are legacy of the old times, they can be completely forreplaced by , and in C++11, forrange-based loops are also supported, which is safer. The more commonly used infinite loop while(1)can also be replaced by a more concise one for (;;).

new/delete. They used to be tasked with replacing mallocand free, but they didn't solve the fundamental problem of memory leaks, and smart pointers and vectorsuch containers in the standard library should now be used.

  1. Suggested keywords for active use

auto. The keywords in C++11 autocan automatically deduce the expression type, providing programmers with the ability that only the compiler had before, which greatly simplifies our work when declaring objects, which is very convenient, so we should use it as much as possible. But you need to pay attention to its semantics, autoalways deduce the "value" semantics, and sometimes you need to use the form of " auto&" or " " to avoid copying costs (such as in loops).auto&&for

default. This defaultis not used for switchthe statement, but for declaring the default implementation of the constructor or destructor, explicitly writing it can tell not only the compiler but also the code reader that this class is a "no trival" class.

nullptr. It replaces the ambiguous NULLand 0, and is a real null pointer at the grammatical level, which is equivalent to that in other languages None/Nil. Using it can make the code more secure.

  1. "quasi" keywords

The so-called "quasi" keywords are actually not keywords, they are just ordinary words,

But it can produce grammatical effects in a specific C++ context, and has no other special meaning. At present, the C++ standard only defines two "quasi" keywords: finaland override, which are used to control the class inheritance system, can enhance the expressive ability very well, and are recommended to be used.

final. It is used to identify a class or member function, and subclass inheritance or overloading is prohibited.

override. It is used to identify the virtual function, which clearly indicates that the member function overloads the function of the same name of the parent class, so as to avoid overloading by mistake.

I hope that readers can adopt the suggestions in this article in future practice, and write codes that are more elegant and easy to read, and that are read by humans instead of robots.


Use if-else to complete the whole process coverage, it is better to use if-returnto complete the screening.

Override is indicated at the end of the quotation marks of the subclass declaration that inherits the virtual function.

map + function is a logic selection alternativeif-else implemented through overloading .

if-returnIt is another way of thinking, through reverse screening , to eliminate unqualified data. Thereby ensuring data security.

The state design pattern is a higher level, from the specific code logic level to the object-oriented implementation method, which has better scalability (increasing inheritance subclasses), better privacy (management within a single class , separated from each other), which is more suitable for low coupling and high cohesion.

  • [[Twenty-Three Design Patterns: State Pattern]]
  • [[State Design Pattern: Code Introduction]]
  • [[map + function instead of if - else]]
  • [[C++11 new feature: default]]

Guess you like

Origin blog.csdn.net/qq_29111047/article/details/132124554