I prefer K&R style. Where the bracket after the if or while is on the same line. But where I work now we use Allman style. It was relatively easy to get adjusted.
The reason I reacted a little strong at your similar question a few weeks ago, is because coding style at my current job. When I applied for the job, I asked them: "do you have a coding style". Their answer was: "not one style for everyone, but each sub-component of our software has its own style". That turned out to be not true. Sometimes each file has a different style. Sometimes each function. Sometimes I see 3 different style on one line. It drives me nuts. Or depressed. It really makes me depressed sometimes for an hour when I see new code, just checked in last week by a colleague from over-sees.
In a previous job, long time ago, we had hundreds if not 1000+ programmers working on one software system. And they all used K&R style. And when someone would do something ugly, or something stupid, they would get corrected. In my current job, it's almost forbidden to criticize the coding-style of a colleague. Unless his code crashes. It's unbelievable.
Your eyes get used to seeing patterns. If code is written in one style, you see quicker what is going on. You see quicker when something is unusual, and requires a little extra thought to make sure you understand what is going on. If you use 10 different coding styles in a file, your mind is constantly on alert. "Do I understand what is going on here ? Did I miss a subtle thing ?"
I'll give you an example that I saw this week in our own code. In all my years of programming, I've only ever seen people write code like:
Code:
for (i = 0; i < max; i++)
Right ?
When you increment something, you write: i++; counter++; pointer++;
This one programmer in our over-seas team writes: ++i; ++counter; ++pointer;
It drives me nuts. Usually when you use ++(*pointer) in stead of (*pointer)++, it is for a reason. If you change the location of the increment, the behaviour of your code changes. Doesn't happen often, but sometimes it does. You immediately are on alert when you see the ++ before the expression. But no, my colleague has to write ++i;
Pfff.
She also like to write:
Switching the left and right operand of the comparison. I understand why she does it. To prevent mistakes like "if (pointer = NULL)". But you know, we got a compiler to warn us for that. Switching the operands is unusual. And it hurts my eyes. I might look like a whiner, but I know I am not the only one who doesn't like "if (5 == i)". And there are dozens of examples like this.
I think it begins with realizing why it is important to write code that is readable. Code is written once. But if you write professional code, your code will be read hunderds if not thousands of times. If you spend and extra few seconds, or an extra minute, to make your code better readable, it will always pay off in the long run. Some people don't realize this. It can be hard to convince them (or, in my opinion, educate them).
So the first time I speak with the lead-programmer of the component I am working at. He says: "our code is old, has been changed a lot, lots of people worked on it. The comments are often not correct any more. So I have removed a lot of comment over the last years". I asked "but then you add new comments, right" ? Nope. He proudly told me he only removes comments. Hardly ever adds any.
It's taken me a year to start understand how our code hangs together.
Some of my colleagues think they save the company money by not worrying about comments, in-code documentation, coding-style, names of functions and variables.
But my boss just paid me a year's worth of salary, just for me to understand what we're doing.
And every new hire has to go through that process.
So yes, William (OP), your question is very relevant imho.
I prefer K&R over Allman, because when you have 1 line if-clauses, you pay only 1 line penalty of adding curly-brackets. Consider these examples:
Code:
if (i == 8)
do_something();
if (i == 9)
{
do_something();
}
if (i == 10) {
do_something();
}
I prefer the last style. But right now I am using a mix of the first 2 styles. Because the others in my group do that.
I don't like the GNU-style at all. 2-Space indentation makes it harder to see where the curly brackets belong.