HomeHome SitemapSitemap Contact usContacts

Personal Codes Of Ethics » Arguments For Code Of Ethics

Line Up Code Braces

Hunting for braces is one of the biggest (and most annoying) hindrances when I'm scanning code. It slows me way down. I should be able to quickly scan through a chunk of code and get a relatively solid understanding of its flow. If I have to hunt around for the braces just to figure out what code is in what block, it forces me out of "scanning" mode and into "in-depth study" mode.

So, rather than cuddling the braces on the line of code that starts the block, put each brace on it's own line and align them vertically with their corresponding ending braces. It makes it much easier to see the scope of the block. As a bonus, it also makes it easier to spot brace misalignments and indentation inconsistencies.

Don't do this:
  for (int i = 0; i < 5; i++) {
    if (isOkToDoSomething(i)) {
      doSomething(i);
      doSomethingElse(i);
    }
  }

Instead do this:
  for (int i = 0; i < 5; i++)
  {
    if (isOkToDoSomething(i))
    {
      doSomething(i);
      doSomethingElse(i);
    }
  }

Still not convinced? I've heard 3 arguments for cuddling opening braces on the previous line of code:

It saves space.

Well, cuddling does take a little less screen space, but the beginning and ending of a code block is one of the most significant structural elements in code. Given that significance, using a little more space to make sure it's clear gives you much more value than the space costs. Screen space is not a precious commodity. I'm not advocating the sprawling waste I've seen in some programs. I would argue that this is a measure of frugalness, wisely spending a bit of space in order to ensure the significance of the code framing is noted.

The code block is defined by the indentation level.

This is simply not true. Well, unless you're working in a language like Python, but that's not really what we're talking about here. The braces define the code block. The indentation is just a convention we all use to help us be able to read the code.

It makes the code easier to read.

Of course, ultimately this is subjective. But even if it were true, if you're reading the code, you're already spending time to go through the code in depth. Therefore it doesn't matter if the brace is snuggled on the previous line or explicated on the next line. However, when you're scanning the code you're not spending time, and it absolutely does matter where the brace is.

Many people who cuddle their braces do so because "that's how they were taught way back when and that's how they've always done it and they can't possibly do it any other way."

Regardless of our individual brace religions, we should all be working to improve ourselves, to progress, to grow, to evolve, to become better than we are now. If I'm not working to improve my game, I probably shouldn't be in this industry anymore.

Topher has been writing code long enough that if his career were a child he'd be charging it rent. Amongst other things, Topher writes weekly articles for the column NotesOfaSoftwareEngineer.

Source: www.articlesphere.com