War of the braces

March 2, 2005

Programmers care about where the braces are put. Deeply. The main reason for that is familiarity - the code looks weird if you do it in a different style

I’m not here to argue for one way or the other. I’ve done it both ways and you know what? It doesn’t matter. After a few weeks your eye has grown accustomed to the new style. The braces are not important - the importance lies in the indentation. Here is an example:

class Test

{

public int zahl = 0;

public String s = 1;public void do()

{

for(int i=0; i < 10; i++)

{

for (int x=0; x < 10; x++)

{

if (zahl < 20)

{

zahl += x;

}

else

{

zahl += i;

}

}

}

}

}

This is just as hard to read as:

class Test

{

public int zahl = 0;

public String s = 1;public void do()

{

for(int i=0; i < 10; i++) {

for (int x=0; x < 10; x++) {

if (zahl < 20) {

zahl += x;

}

else {

zahl += i;

}

}

}

}

}

On the other hand, if you cut all the braces and put in indentation the code is a lot clearer:

class Test    public int zahl = 0;

    public String s = 1;

public void do()

        for(int i=0; i < 10; i++)

for (int x=0; x < 10; x++)

if (zahl < 20)

                    zahl += x;

                else

                    zahl += i;

So what I’m saying is: Don’t worry about where to put the braces. Just do whatever the rest of the team does - it’ll work out fine. If your team can’t find a concensus, play poker or something - the winner gets to decide.

Posted by Jerome Mueller

Leave a Reply