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.
Deprecated keywords
register
. It has been officially deprecated in C++11, and the word should no longer appear in current C++ code.Disabled keywords
export
. C++98 introduced this keyword, it was deprecated but retained in C++11.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 isgoto
that the process confusion it brings is well known. Keywords such asbreak
,continue
,return
and 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 ofswitch-case
, and can also be usedif+return
tomap+function
replaceswitch-case
statements.
else
. Abusingif-else
andif-else的多重嵌套
beingswitch-case
equally 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. Eliminatingelse
keywords 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{}
anddo{}while
loops are legacy of the old times, they can be completelyfor
replaced by , and in C++11,for
range-based loops are also supported, which is safer. The more commonly used infinite loopwhile(1)
can also be replaced by a more concise onefor (;;)
.
new/delete
. They used to be tasked with replacingmalloc
andfree
, but they didn't solve the fundamental problem of memory leaks, and smart pointers andvector
such containers in the standard library should now be used.
- Suggested keywords for active use
auto
. The keywords in C++11auto
can 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,auto
always 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
. Thisdefault
is not used forswitch
the 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 ambiguousNULL
and 0, and is a real null pointer at the grammatical level, which is equivalent to that in other languagesNone/Nil
. Using it can make the code more secure.
- "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:
final
andoverride
, 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-return
to 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-return
It 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]]