Why the Whitespace?

Uncategorized Add comments

Style of writing sourcecode must be one of the largest religious turfwars in hackerdom; seriously, consider the following for its subtle differences; I’ll tell you the difference at the end:

K&R
int main (int argc, char *argv[])
{
    if (0 == argc) {
        do_something();
    }
    return 0;
}
Borland
int main (int argc, char *argv[])
{
    if (0 == argc)
    {
        do_something();
    }
    return 0;
}
GNU
int main (int argc, char *argv[])
  {
    if (0 == argc)
      {
        do_something();
      }
    return 0;
  }
Consistent K&R
int main (int argc, char *argv[]) {
    if (0 == argc) {
        do_something();
    }
    return 0;
}

It’s all about where the brackets are, and coders from one style don’t like another’s.

Personally, I have zero support for:

  1. “Tiger Woods wears Omega watches, so that means they’re the best” (as opposed to actual analysis)
  2. “Daniel Craig wears Armani, so I should wear Armani”
  3. “Dennis Ritchie writes like this, so I should”

K&R format is inconsistent (compare the first and last examples: opening brace always at the end of the previous line?)

K&R is more difficult for new engineers to recognize their mistakes without formatting tools to show them. This is fine if their tools show them, but they cannot always have all the tools. The “bracket hidden at the end of the line” caused a number of students I personally interacted with to have predictable errors, and those errors went away with a coding style change.

For many, it’s simply a case of K&R style used in the examples of the first ever coding text the new engineer reads; lacking an alternative, the engineer simply keeps doing the same thing (such as when the master mechanic shows you “the trick for pulling out that #1 spark plug” and you never make that error again). Some engineers don’t evaluate other options, others are being constrained by the lead engineer, who has K&R ingrained.

Engineers will always have differing levels of experience; I’m a fan of techniques and trends which reduce human error occurrences. It’s not a rite of passage to do things in the more difficult way, and it’s no loss of respect to use a simpler, easier, less-error-prone style.

The argument I hear for K&R is “there’s less whitespace, it’s more compact” — and somehow, this is better; obviously I disagree. There are bigger screens, and smaller font sizes, and conventional limits on how big each functional block of code should be.

…but that’s why I code in Borland C style: benefits, and a weak drawback. It’s the second style I saw, not the first.

You do your style, I’ll do mine, let’s just get some work done, OK?

One Response to “Why the Whitespace?”

  1. What’s in a Coding Style? Says:

    […] commented before about the religious holy-war of coding […]

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in