Given the following two coding styles, please specify a rationale (some pros/cons) for why one would possibly be preferable over the other when writing C++ code.
( Please do not answer with "it is not important"; "just stick to one"; etc. The question is specifically about the possible pro/cons (if any) of the two spacing styles below. Thanks. )
// VARIANT A (NO space after control keyword / space before curly brace)
if(condition) {
// ...
}
else if(c2) {
// ...
}
else {
// ...
}
for(int i=0; i<e; ++i) {
// ...
}
...
// vs. VARIANT B (space after control keyword / NO space before curly brace)
if (condition){
// ...
}
else if (c2){
// ...
}
else{
// ...
}
for (int i=0; i<e; ++i){
// ...
}
...
Note: Apart from taste issues, I am asking this because I see both styles in our code-base and would try to get some arguments for which is to be preferred.
The analysis performed by the team at Stack Overflow found that programmers who use spaces instead of tabs are making more money. David Robinson, the data scientist who performed this study found that programmers using space over tabs made an average of 9 percent more each year than their tab using counterparts.
A C coding standard is a set of rules for source code that is adopted by a team of programmers working together on a project, such as the design of an embedded system. Programming teams and companies write down their C coding standards for a variety of reasons but often bicker internally about which rules to follow.
No. The emitted binary doesn't change based on what spacing you use in your program. The amount of space the source file takes up does change though. spaces and tabs are each one character, so using 1 tab vs 4 spaces takes up different amounts of memory.
You should leave a space between for, if, or while and the following open parenthesis; otherwise, the statement looks too much like a method call, which is confusing.
A lot of people get very obsessed about where to place the spaces, braces, parenthesis, brackets, semi-colons and so on whilst at the same time forgetting that the most important thing about source code is that it needs to be understood by another human being.
The compiler couldn't care less about formatting.
After many years in the programming profession, I've come to use this one simple rule:
Is it easy to read and understand what the code is doing?
It doesn't matter how you format the code, if the above condition isn't met, the code is of poor quality.
I have a personal preference to formatting, and I won't say what it is here as it really doesn't matter what it is.
I find it useful having different programmers code in different styles.
There is of course an exception to this rule: Documentation and tutorial examples should always be consistent - you need to get the reader to follow the important elements being shown and not get sidetracked by the formatting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With