[Home]Thoughts On Code Formatting

wiki home | RecentChanges | preferences | index | random

Introduction

In my experience, most people with strong opinions on code formatting simply have personal preferences that they've built up over time. They then push the preferences onto others without any consideration of the actual asthetics of their preferences. I'm not sure they actually think through the aesthetic details -- they have just gotten used to the way they do it, it's the way they like to see code, and therefore they think it is objectively "good".

I've gone through a number of phases of code formatting practice, both externally- and self-imposed. I think about this stuff quite a bit (yes, I have problems :-). So, here are some of my thoughts on code aesthetics, organized by major points of religious dogma:

White line theory

This is not a point of dogma, but an axiom I'd like to lay down. A white line is an empty line of code (as opposed to white space). White lines should have value. If there are too many white lines, value is diminished. Too little, and value is overstated. Therefore, use white lines sparingly to group major concepts in your code.

Opening brace on "new line" vs on "same line"

I've coded both ways, and over time I've come to the conclusion that "same line" is better. Why? First, review White line theory. Then, consider this: a line with a single curly brace is a "virtual" white line. The brain interprets it as a white line, therefore it is a white line. White line theory states that if there are too many white lines, meaning is diminished. Therefore, the practice of putting the opening brace on a new line leads to diminished value of white lines. The virtual white lines are essentially noise.

Consider a series of if/else constructs using "new line" formatting, each with a white line between them. There is little to distinguish virtual white lines from real white lines, so the grouping of the statements is lost.

 if (this)
 {
     doThat();
 }
 else 
 {
     doTheOtherThing();
 }
 
 if (that)
 {
     doThis();
 }
 else 
 {
     doTheOtherOtherThing();
 }

vs.

 if (this) {
     doThat();
 } else {
     doTheOtherThing();
 }
 
 if (that) {
     doThis();
 } else {
     doTheOtherOtherThing();
 }

Which formatting practice conveys structure and semantics more clearly? I say it's pretty obvious that the second does. With the first approach, you lose the forest for the trees, or at least separate stands of trees are difficult to make out. Of course, you could argue that perhaps the code should be refactored anyway, to which i'd agree. But you still have the virtual blank line problem with methods as well.

To give a more abstract example, consider the following:

 | | | | | | | | | | | | | | | | | | | | | | | | | | |

 ||| | || ||||| || || |||||| |

If you were forced to guess, which one of the above would you choose as an arrangement that contains information? I rest my case.

Tabs vs. Spaces

Spaces are better. Why? Because spaces are not open to (mis)interpretation by different editors and editor preferences. Note that this does not mean that you shouldn't use the tab key. Map the tab key of your favorite editor to produce the correct number of physical spaces and code will look good in every editor.

wiki home | RecentChanges | preferences | index | random
This page is read-only | view other revisions
Last edited August 29, 2004 7:01 am by seed (diff)

Search:
Site Meter