There are quite a lot of rules in the C++ Core Guidelines dealing with expressions and statements. To be precise, there are more than 50 rules are about declarations, expressions, statements, and arithmetic expressions.

I forget to mention two rules that are just called general. Here we are.

The next déjà vu. In one of my last C++ seminars, I had a long discussion followed by an even longer analysis of a few quite sophisticated and handmade functions for reading and writing strstreams. The participants had to maintain this functions and had after one week no idea what was going on.

The main obstacle to don't understand the functionality was that the functionality was not based on the right abstraction.

For example, compare the handmade function for reading an std::istream.

The right abstraction often means you have not to think about ownership such in the function read1. This will not hold for the function read2. The caller of read1 is the owner of result and has to delete it.

A declaration introduces a name into a scope. To be honest, I'm biased. On one hand, the following rules are little bit borrowing for you, because they are quite obvious. On the other hand, I know a lot of codebases which permanently break this rules. For example, I had a discussion with a former Fortran programmer, who stated: Each variable should have exactly three characters.

Anyway, I will continue presenting the rules, because good names a probably the key to make code readable, understandable, maintainable, extensible, ... .

If a scope is small, you can put it on a screen and get an idea what is going on. If a scope becomes too big, you should structure your code into function or objects with methods. Identify logical entities and use self-explanatory names in your refactoring process. Afterward, it is a lot easier to think about your code.

The variable result (1) is only valid inside the if and else branch of the if statement. result will not pollute the outer scope and will be automatically destroyed (2). This can not be done before C++17. You have to declare result in the outer scope (3).

This rule sounds strange but we are already used to it. Giving a variable the name i or j, or giving a variable the name T will make the intention of the code immediately clear: i and j are indices, and T is a type parameter of a template.

There is a meta-rule behind this rule. A name should be self-explanatory. In a short context, you get with a glance what the variable means. This will not automatically hold for longer contexts; therefore, you should use longer names.

To be honest, I often have problems with the number 0 and the big capital O. Depending on the used font it looks quite similar. Two years ago it took me quite a time to log into a server. My automatically generated password had a character O.

What's next?

Get your e-book at leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11,C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 550 pages full of modern C++ and more than 100 source files presenting concurrency in practice.