All | 43 Folders | Accessibility | BoingBoing | Books | Computer Related | Family | Films | General | Hacking | Hobbies | Humor | Java | Links | Omni | OpenSolaris | Puzzles and Games

« The next radio wave. | Main | Revelation Space »
20040622 Tuesday June 22, 2004

Coding Styles

This is a religious issue, just like favorite editors, programming languages or operating systems. Coding style guides are also like standards; there are so many to choose some. Here are some for C and C++. There are a few for Java too. Sun publishs one here. There are style guides for most computer languages.

If I have inherited some code that I didn't write (and assuming the original author isn't around to continue maintaining it), then I will reformat it to the coding style that I'm used to (which is close to the common coding conventions). By doing this, I find that I'm able to better understand it. Even though there are tools around to automatically reformat the code, if it's new to me, and I'm expected to be working on it for quite a while, I'll do the reformatting by hand. This allows me to view all the code that makes up a project. Of course there are limits here. The project has to be upto a certain size. I'm certainly not going to reformat the Mozilla code base by hand.

The big exception to this is if I'm contributing bug fixes or enhancements to somebody elses code. I'll respect their style (assume they have a consistent one), no matter how much it differs from mine.

Onto some pet peeves.

You should consider using applications such as cstyle or jstyle to help you find places where you can improve the readability of your code. Thanks to Danek Duvall for pointing me at these.

And finally, if you write C code, check out (and obey) the Ten Commandments for C Programmers by Henry Spencer.

[]

[]

( Jun 22 2004, 01:45:04 PM PDT ) [Listen] Permalink Comments [4]

Comments:

Tabs aren't evil, they're just tricky. You're right in that one cannot guarantee where others place their tab stops - however, in some circumstanses that doesn't matter. My own coding style involves noting that there's a difference between logical and cosmetical indentation, where logical indentation is aligning the lines from the left marging to reflect nesting level, and cosmetical indentation is everything else (e.g. lining up broken lines and making lists of definitions look like tables). The difference is on one hand that logical indentation is where people have different tastes (I can't recall ever hearing an argument about whether broken lines should be indented by a factor of two or four spaces, for instance), and on the other that the characters used for logical indentation are always the leftmost ones one every line, while cosmetical indentation is mixed with the actual contents of the line. Since logical indentation characters always come first, tabs can be used there (as long as _only_ tabs are used there), because there are no other characters there that can change which tab stop the next tab will jump to - regardless of where the tab stops are placed. Doing indentation that way means that everyone can have the amount of logical indentation they wish (by setting their tab stops), but it also means that everyone will have to be careful about their indentation, which sadly makes it impractical in many settings. It is nice when it's possible, though.

Posted by Magnus Reftel on June 23, 2004 at 05:29 AM PDT #

I agree with everything said so far, but having tried to deal with someone else's code lately, I have a couple of pet peeves. First, use comments to describe what you're doing! This can be extremely helpful for other people trying to read or modify your code. Second, do error checking. While you may think you're the only person who's ever going to use this program, and you know all the right parameters, that may not be true for long. Others may discover your program and decide it would be useful, and when they try to use it, they will get mysterious core dumps if you don't do error checking from calls to open() or fopen(). I also like the approach of writing the documentation (man page, whatever) first, so you already have the documentation before you start, and you have some idea what you're programming.

Posted by Dave Marquardt on June 23, 2004 at 08:27 AM PDT #

I was with them up until that One True Brace Style crap. I thought that backwards, pagan, heretical nonsense expired about 1981. Real enlightened programmers know to put put curly braces on a line by themselves, lest ye risk getting thou eyeballs gouged out with a power nail-gun, by angry hordes of rioting co-workers. :-)

Posted by Phillip Rhodes on June 23, 2004 at 08:38 PM PDT #

NONE of these points is something that poses a problem to any modern IDE (Eclipse, Intellij, Netbeans or else). It takes a max of few hours to correctly reformat a million lines of code. Been there, done that.

Posted by Adrian on June 28, 2004 at 01:19 PM PDT #

Post a Comment:

Comments are closed for this entry.