• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Where do those opening braces go?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A truly trivial topic for my first post here: I notice with pleasure that your style guide recommends the style I've always followed, of indenting matching braces at the same level, e.g.
public void someMethod()
{
}
... but I always argue about this with my colleagues as they all prefer this style:
public void someMethod() {
}
Is there any advantage in "their" style, other than because Sun says so, and because it's a hangover from the days of old C programmers, where they were always trying to make source files as small as possible? I know I'm biased, but I think the style you recommend is SO much more readable.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reducing source file size isn't entirely passé. I find it easier to understand a method when I can see the entire method on my screen at once, with no scrolling necessary. So I prefer to avoid blank or nearly-blank lines when I don't get any value from them. Others see more benefit in those blank lines as visual cues that tell you at a glance what's going on. I think I get all the visual cues I need here from the horizontal indentation; additional vertical spacing is unnecessary. I'd rather see more actual content.
Ultimately I don't care too much which standard is used; it's not (or should not be) difficult to read or write in either style. A good IDE will allow you to quickly reformat a file either way.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nick Way:
A truly trivial topic for my first post here


Actually I've seen developers debate this subject with more vigor then politics and current events. In the end, it is often a matter of personal preference; or team policy. I personally like the opening brace on a new line. For me, it is more visually offsetting and identifying of a block of code. I can actually read and digest blocks of code quicker when it is formatted this way. Also, it makes it easier to match up opening & closing braces; but this isn�t as much of an issue in modern IDE�s like it use to be in vi or notepad (Since modern IDE�s will highlight matching braces.)
There really is no "technical" advantage of one over the other. Personally, I have a theory that books always use the brace on the end in order to save pages (and thus money). As a result people get use to that style and then use it. So you see, it's just a big conspiracy by book publishers. (I think the CIA & NSA may also be in on it).
In the end though, Jim makes a good point; most modern IDE�s have a formatting utility which allows you to easily reformat any code to your preference. Whenever I download a book�s examples code, the first thing I do is run it all through my IDE�s formatter. The only time using such a formatter can be problematic is that it can wreak havoc when you diff two files and the only difference is the formatting of the opening brace. Two otherwise identical files will appear to be very different. This can be a annoying if you and anoter team member use different formats and use a common code repository. Fortunately my IDE allows me to quickly change formatting styles so I can use one style when editing and, if need be, another that conforms to team standards before checking a document into CVS.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Vender:

Personally, I have a theory that books always use the brace on the end in order to save pages (and thus money). As a result people get use to that style and then use it.


Your theory has been confirmed by statements made by Brian Kernighan and Dennis Ritchie, whose little white book introduced the style. They personally used the brace-alone-on-a-line style but the end-of-line style made it easier to fit code samples on a single page without breaking them up, so it was used for the book. They were not proud of what they accomplished
Personally, I like the end-of-line style. Colorized editors make most arguments for brace-alone-on-a-line moot, and end-of-line lets you see more code in a window.
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consistency appears to be the important thing. Over years of putting up with sloppy, inconsistent brace formatting, indentation, etc., I've learned to ignore it all. When it really gets bad, as others have mentioned, I just hit the reformat key sequence in Eclipse.
However, I have found one minor, non-visual benefit of the aligned brace style (which I used to favor heavily).
The aligned braces:

make it easy to quickly comment out the conditional clause (Ctrl-/) temporarily. K&R one-true-brace-style requires a few more keystrokes (Ctrl-/, down, down, Ctrl-/). Big deal. I still do one-true-braces, since that's the way most shops like it.
-Jeff-
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting point, Jeff. Just to muddy the waters I'll note that dropping braces entirely (around a single statement) can produce the same effect. But this bothers many people. Oh well. I've long felt Python had the right idea about braces - we shouldn't need them at all for control structure. Unfortunately this idea hasn't caught on in any other languages I'm aware of. Too bad.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's caught on in Ruby.

Not the most readable of languages in all respects
In Java is would be something like

In C you'd possibly do it using function pointers (oh, joy).
 
Nick Way
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your theory has been confirmed by statements made by Brian Kernighan and Dennis Ritchie, whose little white book introduced the style. They personally used the brace-alone-on-a-line style but the end-of-line style made it easier to fit code samples on a single page without breaking them up, so it was used for the book. They were not proud of what they accomplished


Sounds logical to me. I just think that making code more compact is not a thing to aim for, better by far to space it out to make it more readable. And I notice a lot of people who use K&R style leave a blank line after the opening brace, which kinda defeats the object anyway.
And yes, Mark, I was being ironic about it being a trivial topic - I know what strong feelings this subject arouses, hence my wish to start a discussion of it here. Thanks to all for feedback and comments ...
[ February 09, 2004: Message edited by: Nick Way ]
 
Jeff Langr
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Interesting point, Jeff. Just to muddy the waters I'll note that dropping braces entirely (around a single statement) can produce the same effect. But this bothers many people. Oh well. I've long felt Python had the right idea about braces - we shouldn't need them at all for control structure. Unfortunately this idea hasn't caught on in any other languages I'm aware of. Too bad.


I'm all for dropping the braces, I do it most of the time (unless the client complains). My unit tests catch the potential error of adding a second statement but forgetting to add the braces.
I should've made my example more than a single statement. Good call.
The nice thing about Python is you *have* to make your code look a little nicer, otherwise it doesn't work.
-J-
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Jim]: I've long felt Python had the right idea about braces - we shouldn't need them at all for control structure. Unfortunately this idea hasn't caught on in any other languages I'm aware of.
[Jeroen]: It's caught on in Ruby.
Mmmm, yes. There are plenty of other languages that don't use braces for control structure. What I meant though (as opposed to what I said) is that I like Python's idea of using indentation as the only means of indicating block structure. Ruby doesn't use braces, but it does use "end" in place of the final brace. Other languages use other delimiters - "end if", "fi"...
I don't object to this aspect of Ruby; I'm just saying it's not quite what I had in mind when I wrote "hasn't caught on" above.
[Nick]: I just think that making code more compact is not a thing to aim for, better by far to space it out to make it more readable.
I should note that readability is of very high importance to me; we just have some difference in what we think constitutes readability. I'm all for inserting the occasional blank line into a method to group related operations together, for example. But if there's a lot of blank vertical space being used anyway, then that dilutes the power of the blank line on th times I actually need it. Also I'm very agressive about improving readability by refactoring long methods into smaller methods (with nice descriptive names), so there's often not a whole lot of need for additional subgrouping of code within a method.
[Nick]: And I notice a lot of people who use K&R style leave a blank line after the opening brace, which kinda defeats the object anyway.
Yeah. I think those are people who share your views on the benefit of whitespace, but they follow K&R just because it's an established standard. Though so is brace-on-new-line - it's just a matter of which crowd you find yourself in.
[ February 09, 2004: Message edited by: Jim Yingst ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO using indentation and nothing else to indicate blocks is a bad thing.
It just about makes breaking long lines impossible (the rest of the statement could not be indented to prevent it looking like another level block...).
Maybe it's my old Pascal background, but I'm partial to using keywords like 'end' and even 'begin'. When I learned C it took some getting used to {}
 
Nick Way
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Also I'm very agressive about improving readability by refactoring long methods into smaller methods (with nice descriptive names), so there's often not a whole lot of need for additional subgrouping of code within a method.


Totally agree. Had to debug some code not so long ago which contained single methods 300 lines long with multiple levels of indenting, nested try/catch/finally blocks, single character variable names, you name it. F***ing nightmare ...
 
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic