Brace yourself
Apparently I'm not the only one with brace issues, but now they've come back to bite me.
A while ago I was showing Berta some code I had written. I forget what it was. Anyway, she commented as she had int he past that she didn't like my style of indentation and brace use. Some will be surprised/enlightened to find out that this is one of the few things that Berta and I ever argue about. Anyway, she was convinced that her way was good, and I liked my way and that was that. Well, in a sudden urge to get myself in line with what seemed like the rest of the world (for no particular reason... ahem), I decided to switch everything over to the way that Berta did it, and now I'm thinking to regret it.
For the record, my original style of coding is called K&R, after book authors Kernighan & Ritchie who publish in this style. It looks like this:
if (test) {
do_something();
}
Berta's style is called Allman, and is named for similar reasons. It also looks identical to the code produced by my C++ teacher at WCU:if (test)
{
do_something();
}
Now, if you're not a programmer, you don't see the difference other than where a brace (not a curly bracket, a brace) is positioned. But the difference in readability in more complex code is profound, I assure you.
Anyhow, I switched over to Allman, and now I find myself writing code in PHP for WordPress. It turns out that there is a published standard for code written in PHP (like there is in many languages, just not the ones with the most controversy) and it is... K&R.
K&R is used throughout the PHP world. It is used by the PEAR authors, whose code is routinely folded into the core of php as libraries. It is used by the WordPress authors. Everybody uses it, even I used to - but now I'm not.
So I've written about 50k of code in Allman, and just learned that K&R is the standard. Not to mention that I submitted a bunch of stuff for inclusion in CVS that now may be rejected because I listened to Berta's crazy-talk. I told you this method was good. <sigh>
If anyone has a tool that'll convert my non-standard code from Allman to K&R so I can fall back into my good habits, please let me know.
Comments
Comment by Bud on .
The little bit of PHP code I have had to write myself is a cesspool visually. In fact, all of my web code is terrible looking. I know the tags in HTML but, honestly, I don't know what the accepted formatting is suppose to look like for nested stuff especially. I end up trying to make it all look "Pascal-ish" or alternatively, depending on mood, write it all on one big line. It's ugggly.
Incidentally, I have gotten my Pascal pretty standard. I used to do something a bit unorthodox with my end for blocks that made it easier for me to read, or at least I thought it did at some point in time. I remember your thought, upon seeing it was something like WTF?
The first time I got some of your Delphi code I was seriously like WTF? Turns out that we hav our Tab settings for the IDE setup differently.
Well, I know that you can use Google so if you are still looking for a conversion tool it must be hard to find. I would have guessed that Emacs would do that sort of thing.
Comment by Bud on .
Oh, yeah I forgot. If K & R is standard for all of the stuff you are doing it's obviously the right way to go. Probably because the braces look more like Pascal begin / end blocks, I do think that the Allman style makes for better readability.
Comment by Owen on .
Funny, I've always written my Pascal in K&R style, too.
Emacs is probably the right tool to use, but it's such a clunky beast that I can't imagine someone hasn't thought of something better. Where's MiloMemo's code formatter when you need it?
Comment by Pat on .
This is the sort of religious argument I've had many times at work. My argument is that K&R gets you 1 extra line on your screen (a valuable resource, to be sure), but Allman gets you better readability. I argue that being maintainable is one of the best attributes of well-written code and, if it makes it even a second faster to figure out what's going on, it's worth it. While I started with K&R long ago, I've long since converted, just due to the readability. Besides, if you need that 1 extra line, perhaps the functions you're writing are too long.
As an addendum, if there's a code standard, it's most important to stick to that.
Comment by Owen on .
I am of the opinion that K&R is quite readable enough if you write it well.
FYI, Java coding convention is K&R, as is PHP.
Exactly why did I start using K&R? I tried to think of the reason today, and I have no idea.