Style guidelines are not overly strict. The important thing is that code is clear and readable with an appropriate amount of whitespace and reasonable length lines.
The only requirements are:
- 2 spaces indent level. Tabs are not allowed, and a mixture of tabs and spaces is strictly forbidden. Modern autoindenting IDEs and editors require a consistent standard to be set.
int myFunction(bool t_b)
{
if (t_b)
{
// do something
}
}
- Prefer pre-increment to post-increment when it is semantically correct. Pre-increment is faster then post-increment because it does not require a copy of the object to be made.
// Correct
for (int i = 0; i < 15; ++i)
{
std::cout << i << std::endl;
}
// incorrect
for (int i = 0; i < 15; i++)
{
std::cout << i << std::endl;
}
{``}
are required for blocks. Leaving them off can lead to semantic errors in the code.
// this compiles and does what you want, but can lead to confusing
// errors if close attention is not paid.
for (int i = 0; i < 15; ++i)
std::cout << i << std::endl;
// the cout is not part of the loop in this case even though it appears to be
int sum = 0;
for (int i = 0; i < 15; ++i)
++sum;
std::cout << i << std::endl;
// Correct
int sum = 0;
for (int i = 0; i < 15; ++i) {
++sum;
std::cout << i << std::endl;
}
Very nice and detailed, thank you.