Bracing styles

Just had a debate amongst the development team here about Java bracing styles.

Here we favour “Allman style” where the braces are on a line all on their own.

I generally feel that it enhances readability, but recognise that it’s mostly a personal preference and not something to have a fight over.

We were simply discussing people’s preference and other aspects of it when a contractor waded in to our discussion to explain why he preferred “K&R style“.

His argument was that you could fit more code on screen (a fact that you can’t disagree with) and he also didn’t like white space in files for the same reason.

He then showed us some code where there were no blank lines within any of the methods.

To me it looked like an unreadable blob of text.

In the past when I had seen code like that I had always assumed the coder didn’t care much about readability. I subsequently took a dim view of the general quality of their code.

I’d never heard anyone try to justify such messy difficult to read code before.

Ignoring the fact that he’s a contractor, does he have a point?

6 thoughts on “Bracing styles”

  1. He’s a contractor – of *course* he doesn’t have a point. πŸ˜‰

    Seriously, I have to say that I disagree. I think that splitting your code up into ‘stanzas’ hugely improves readability.

    My advice on the braces issue – switch to Python. No braces, no brace wars.

  2. I used to have an opinion, but having coded in shops that do braces either way, I think it’s more important to do what everybody else does, because:

    (a) People can parse the code quicker if the braces are where they expect them to be, and
    (b) It cuts down unnecessary diffs.

    Eclipse’s code formatter is brilliant from that point of view, you just need to make sure everybody has the same settings, then insist they press Ctrl+Shift+F before they checkin.

    As for blank lines, I think they’re generally a good idea but when I start to use them it’s a warning sign that perhaps my methods are a little too long, and if there is a sensible way to refactor, then I do.

    Anyway… back to your question: while your contractor may have a point, I reckon you do too: readability is especially important in a team environment and conformance to formatting standards is part of that.

    PS: Ask Simon whether Python programs should be indented with spaces or tabs, and see if he has an Opinion πŸ™‚

  3. No, he doesn’t have a point about eliminating whitespace completely. Although more code on the screen increases code readability, some whitespace increases code readability quite significantly.

    Personally, though, I prefer “K&R style”. I don’t like “unecessary” added whitespace. Wherever there are braces, the entire next block of code is always indented. Isn’t that enough to be able to tell where functions/loops start and end?

    I agree with Alan that it’s the *most* important that everyone just do it the same way.

  4. This is such a silly arguement to get into especially when one can use a formatting tool if it really bothers them that much. One should generally follow the style of the code in place. Eliminating all white space is a problem for most but do you really care about the brace. Come on, hasn’t python proven that indenting blocks is really all you need? How much time to Python coders waste because they go the block indentation wrong. I’ll bet it’s about the same about of time as Java coders who got the brace placement wrong.

    This is really a matter of style. I wish people would stop trying to micro-manage others by focing their style on them and then try to justify it with lame arguements.

    I like my opening brace on the same line… get over it!!!

  5. He has a point, but it only makes sense if you need to show a lot of code at the screen at the same time, which is an issue only if you have very long functions, which you shouldn’t have. So, there!

    About bracing style, K&R is just plain wrong! Why? Because it isn’t symmetrical! Our eyes tries to understand code by finding patterns and symmetry. K&R braces works against that. So, there!

    So, it isn’t a matter of style at all! It is about readability, and as such it should be possible to test if my theory is correct, by simply picking a bunch of programmers previously untainted by braces or bracing wars, and then let them simply read code in both styles, and measure how long it takes before they understand it correctly, for example to be able to spot bugs or make changes.

Leave a Reply

Your email address will not be published. Required fields are marked *