Professional C__ - Marc Gregoire [70]
The Curly Brace Alignment Debate
Perhaps the most frequently argued-about point is where to put the curly braces that demark a block of code. There are several styles of curly brace use. In this book, we put the curly brace on the same line as the leading statement, except in the case of a function, class, or method name. This style is shown in the code that follows (and throughout the book).
void someFunction()
{
if (condition()) {
cout << "condition was true" << endl;
} else {
cout << "condition was false" << endl;
}
}
This style conserves vertical space while still showing blocks of code by their indentation. Some programmers would argue that preservation of vertical space isn’t relevant in real-world coding. A more verbose style is shown below.
void someFunction()
{
if (condition())
{
cout << "condition was true" << endl;
}
else
{
cout << "condition was false" << endl;
}
}
Some programmers are even liberal with use of horizontal space, yielding code like that in the following example.
void someFunction()
{
if (condition())
{
cout << "condition was true" << endl;
}
else
{
cout << "condition was false" << endl;
}
}
Of course, we won’t recommend any particular style because we don’t want hate mail.
When selecting a style for denoting blocks of code, the important consideration is how well you can see which block falls under which condition simply by looking at the code.
Coming to Blows over Spaces and Parentheses
The formatting of individual lines of code can also be a source of disagreement. Again, we won’t advocate a particular approach, but we will show you a few styles that you are likely to encounter.
In this book, we use a space after any keyword, a space before and after any operator, a space after every comma in a parameter list or a call and parentheses to clarify the order of operations, as follows:
if (i == 2) {
j = i + (k / m);
}
The alternative, shown next, treats if stylistically like a function, with no space between the keyword and the left parenthesis. Also, the parentheses used to clarify the order of operations inside of the if statement are omitted because they have no semantic relevance.
if( i == 2 ) {
j = i + k / m;
}
The difference is subtle, and the determination of which is better is left to the reader, yet we can’t move on from the issue without pointing out that if is not a function.
Spaces and Tabs
The use of spaces and tabs is not merely a stylistic preference. If your group does not agree on a convention for spaces and tabs, there are going to be major problems when programmers work jointly. The most obvious problem occurs when Alice uses four spaces to indent code and Bob uses five space tabs; neither will be able to display code properly when working on the same file. An even worse problem arises when Bob reformats the code to use tabs at the same time that Alice edits the same code; many source code control systems won’t be able to merge in Alice’s changes.
Most, but not all, editors have configurable settings for spaces and tabs. Some environments even adapt to the formatting of the code as it is read in, or always save using spaces even if the tab key is used for authoring. If you have a flexible environment, you have a better chance of being able to work with other people’s code. Just remember that tabs and spaces are different because tabs can be any length and a space is always a space. For this reason, we recommend that you use an editor that always translates tabs into spaces.
STYLISTIC CHALLENGES
Many programmers begin a new project by pledging that, this time, they will do everything right. Any time a variable or parameter shouldn’t be changed, it’ll be marked const. All variables will have clear, concise, readable names. Every developer will put the left curly brace on the subsequent line and will adopt the standard text editor and its conventions for tabs and spaces.