Coding and brace styles..
I have a co-worker that drives me insane with his brace style[0] and unfortunately I get to work with his code quite a bit[1], so today's rant is about that.
I subscribe to the good old Kernighan & Ritchie style of putting the opening brace last on line and the closing brace first.. In other words this:
if (foo == 0) {
do something;
} else if (foo == 1) {
do something else;
} else {
barf;
}
Simple and easy right? I've been asked[2] to justify this usage many times before in middle of heated debates. It's simple..
- Clearly identifies the beginning of enclosed code.
- No need to waste a line just for one brace, hence code can be more compact without sacrificing readability.
- My favourite CS professor in college used it![3]
Then there is the other quite popular style (some of my co-workers use this, I've no big issues with that).
if (foo == 0)
{
do something;
}
else if (foo == 1)
{
do something else;
}
else
{
barf;
}
It's still pretty clean and rather readable, even with nested code. It wastes a lot more space, but hey, we're in the 21st century, we have large monitors now.
Then there's this..
if (foo == 0)
{
do something;
}
else if (foo == 1)
{
do something else;
}
else
{
barf;
}
Can anyone tell me where this style came from? I've never seen it before and it's driving me literally insane (literally). I think it makes nested code hard to read, particularly when I see it mixed in with one of the other styles[4]. It's just.. well.. weird!
Additionally, every time I edit code formatted like this I need to load a different .emacs file or indent the code by hand using the spacebar. That's a big pain...
[0] - hi Burt! ^
[1] - it's actually (usually) *really good* code, just *really weird* bracing. ^
[2] - hi Burt! ^
[3] - ok, so that's not a real reason. ^
[4] - I realize this is a fault of the horrible programmers that edit someone else's code and just mix in brace styles. Either re-style the entire code, or just use the existing! ^
Comments
The argument for #2 is that these braces begin blocks, and that they should be orthoganal to the braces that surround function definitions.
int foo(int arg1, char *arg2)
{
shows that curly goes on the left margin of the block it starts. Same for other curlies that delimit code (but not for initializers or structs - they're semantically different).
Posted by: Steve | July 23, 2002 11:11 PM
(I'm XRay from DSLR)
I liked your coding style rant. That kind of stuff just bends me the wrong way! A character flaw, really (not me - those that format their code wrong).
Heaving beasts? [sic] I hope you had them declawed! ;)
Posted by: Ray Marron | July 24, 2002 10:59 AM
I used to be an advocate of K&R style but I found that a slightly less than pure BSD style (the other quite popular style) was easier to read when I was looking at other people's code, especially if that code wasn't very well written. I make exceptions to the BSD style when writting single line functions (e.g. inline accessor methods) where the additional whitespace detracts from the clarity of the code.
I'd guess Burt's style came from the depths of some smoldering cesspit that makes hell look like a pleasant vacation[0]. I reccomend setting up CVS to run indent on his code when he checks it in[1].
I've wondered in the past how CIDB[2] could be used to deal with differing indentation and coding styles. It certainly has a lot of potential, but I've never been in a position where I could justify the time cost of learning how to do it[3].
[0] - Possibly an alternate hell dimension ruled by some guy with nails stuck in his head... or Minnesota.
[1] - Personally, the idea of giving source control the ability to reformat my code gives me the willies and I'd break the fingers off of anyone who set up CVS to pull this kind of crap.
[2] - Code In DataBase. Store the source code in a relational database and use ODBC or SQL to pull it out for compilation. I worked for a company that was setting up to try it with their main product. Unfortunately, they were mostly idiots and any sucess or failure on their part would be a poor indication of the general viability of CIDB.
[3] Besides, the way things tend to work, I'd wind up in charge of the database, which would be that much less time I'd have to actually write code. I already spend too damn much time making sure the makefiles actually build properly.
Posted by: PopeMatt | July 24, 2002 01:31 PM
The style that puts the braces at the same indent level as the enclosed statements comes from people who'd prefer to be writing in Pascal or Algol 60.
if something then
____begin
____something else;
____end;
That looks fine when the statement-brackets are keywords, but not when they're single characters.
Me, I'd prefer to be writing in Algol 68
if something
then
____something else
fi
which of course matches quite nicely with K&R brace style.
(Underscores used to keep the layout correct, to stop this input box eating leading spaces)
Posted by: dave | April 9, 2003 11:39 PM
God, I bet you would hate the coding style I use on occasion...
if (foo==0)
___{do something();}
else if (foo==1)
___{do something_else();}
(ignore underscores)
However, I do prefer the Kernighan and Ritchie style...
*about to kill keyboard with messed up shift key*
Posted by: jeremy | May 1, 2003 12:30 AM
if (annoyed==1) {
...spend_time(rethinking);
}
else {
...spend_time(again);
}
if (annoyed==1) {
...if (coding_style==0) {
......emit(anger);
...}
}
.'s for indentation illustration only.
Posted by: steve | May 8, 2003 02:29 PM
I have to agree that the original C style indenting is hideous, because it is not clear where blocks of code begin and end and therefore it is hard to read. It is especially hard to read when you are used to BSD style and the ease with which you can 'track the scope of control constructs' in code that uses this style.
One drawback of BSD is that it is not as compact, but readability is certainly a lot more important.
Having said all that, at least we all have SOME logical style. There are people who indent randomly and don't even get me started on people who 'spaghetti code'. They should all be strangled.
Posted by: Docia | August 22, 2003 06:52 AM
The style you hate is called Whitesmiths because it was used in that compiler a loooong time ago. I can't stand it either, and it's pretty much standard in the code I work on. As for which one is One True Brace style, I've seen that claimed for both K&R and Whitesmiths. Since bracing and indentation style arguments are really religious wars, I have a feeling that One True Brace is kind of like "True Christian" -- the proponent of a particular style lays claim to the title.
About the person claiming K&R style is horrible because of the difficulty of using it with 50-line while loops, it's the 50-line while loops that are horrible horrible horrible, not to mention unnecessary and just plain poor design.
One more thing -- I love the way programmers use the word "orthogonal" (spelled correctly or otherwise) without having a clue as to what it actually means.
Posted by: Noel Boulanger | November 25, 2003 05:22 PM