// Internet Duct Tape

Seven Pillars of Pretty Code // Perforce (via Coding Horror)

Posted in Links, Programming and Software Development, Technology by engtech on June 21, 2006

Christopher Seiwald of Perforce has a whitepaper that talks about the Seven Pillars of Pretty Code.

  1. Code changes should blend in with the original style.It should not be possible to discern previous changes to a file without seeing the previous revisions.
  2. Keep columns narrow.The left edge of the code holds the structure and the right side holds the detail, and big long lines mix zones of structure and detail, confusing the reader.
  3. Break code into logical blocks so that each does a single thing or single kind of thing.A reader can only avoid a total reading if a cursory inspection can reveal the whole block's nature.
  4. Set off code blocks with whitespace and comments that describe each block.Comments should rephrase what happens in the block, not be a literal translation into English.
  5. Reduce, reduce, reduce. Remove anything that will distract the reader.The tighter the scope, the shorter the name.
  6. Two or more pieces of code that do the same or similar thing should be made to look the same.Nothing speeds the reader along better than seeing a pattern. These similar looking pieces of code should be lined up one after the other.
  7. The left edge of the code defines its structure, while the right side holds the detail.You must fight indentation to safeguard this property. Code which moves too quickly from left to right (and back again) mixes major control flow with minor detail. Rearrange conditionals so that the block with the quickest exit comes first, and then return (or break, or continue) so that the other leg can continue at the same indentation level.

My comments:

  1. Blending. Very good point, quite often people will jump in with whatever style they are comfortable with. It's always a good idea if you can have (smart) coding guidelines (at least at the team level).
  2. Narrow. I'm *horrible* at this. And when I look at the example code I remember why. I'm used to working on 21" monitors where you might as well use up all your horizontal space so that you can get more vertical space on the screen.
  3. Logical blocks. Common coding guideline. A function shouldn't span more than one screen.
  4. Meaningful comments. Common coding guideline.
  5. Variable length. Common coding guideline.
  6. Repetition. Interesting, I think too often I'll try to wrap the repetitious code into a general function that does both tasks because I hate when you have to make a change in code to multiple places.
  7. Nesting. Common coding guideline, reduce nesting as much as possible.

What CodingHorror had to say about it:

It's an entirely reasonable set of advice. Until you realize that Christopher is using his very own jam/make.c as a shining example of the pillars of pretty code.

And make.c is extremely ugly to me.

Besides the fact that it's C, I have a few other problems with Mr. Seiwald's make.c code:

  • It embeds source control information in the comments. Isn't this why we have source control systems?
  • It uses complex, difficult to maintain block alignments in the comments, variables, and function declarations. Unless your IDE does this for you, this is the formatting equivalent of a house of cards.
  • There are functions named make and make0. Ouch.

The code formatting, as promised, is pretty. But the emphasis on formatting does make me wonder– rather than focusing on the external, cosmetic attributes of the code, shouldn't we be searching for something prettier on the inside?

Such as a higher level language?

And it's true. Why not just throw the code into a beautifier like Artistic Style if you just want to "make it pretty"? I also think that (especially with C code) a code documentation tool like doxygen should be used so that the user docs are inline to the code in a standard format.

>> Seven Pillars of Pretty Code

>> Coding Horror: Pretty Code, Ugly Code

One Response

Subscribe to comments with RSS.

  1. Moz said, on February 09, 2007 at 4:30 am

    Try rotating that 20″ monitor so you get more lines of code, but less of each line. Of course, if you’re using Visual Studio you’ll have to reconfigure the whole IDE so it works like this, but it can be done. Having worked both ways, I find portrait orientation is a big win.

Comments are closed.

%d bloggers like this: