File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes position of the opening brace { Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "position of the opening brace {" Watch "position of the opening brace {" New topic
Author

position of the opening brace {

Vipin Thomas
Greenhorn

Joined: Jan 02, 2007
Posts: 3
As per java coding standards the opening brace should be at the end of the line that begins the compound statement or declaration. Is there any advantage apart from :-

1. Code Readability
2. Reduction in # of lines.

Does following the standard above benefit the compiler in any way?
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
I don't think there is any other advantage(though it's a big one when it comes to maintenance)in using coding standards other than readability. Number of lines doesn't impact the(in a notable extent) performance of code.
That's my take.

Sid
rohit leeta
Ranch Hand

Joined: May 02, 2007
Posts: 49
You already listed the advantages, there can not be any difference of using it that way for the compiler.


<a href="http://www.java-forums.org" target="_blank" rel="nofollow">Java Forums</a>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38802
    
  23
There is more about indentation in Wikipedia. The ranch's Chicken Coop suggests an indentation style which called BSD/Allman in Wikipedia. This has great advantages over what is in the Java Tutorial (called K&R) because the {} pairs are always exactly aligned vertically, and BSD/Allman is much easier to read. On a modern PC with a 1280 x 1024 display one can read at least 100 lines simultaneously, so there is no need to bother about number of lines.

Indentation is (as rohit leeta corectly points out) for people to read, not for the compiler.
Vipin Thomas
Greenhorn

Joined: Jan 02, 2007
Posts: 3
What is the compilation process?
Does the compiler delete all the unwanted characters and then parses each statement or it directly parses without any deletion?
If it deletes then definately there is an advantage of keeping the brace @ end of line and can help when large # of files are compiled.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38802
    
  23
As far as I know, compilers ignore whitespace when preparing a program for compilation. This is however at a very early stage in the process and it only takes a few microseconds per line. So if you have a 1000000 line program, missing out whitespace might save a few seconds in compilation.

Don't mess up the legibility of your code for such a tiny gain in compilation time.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This gets into "religion" territory - the more strongly held an opinion, the less there is to back it up. I'm a big fan of vertical alignment, like the ranch standard. The only reasons I see for { on the end of a line are that K&R did it (which is no reason at all considering they simultaneously unleashed C on us) and it saves vertical space in code samples in magazines where they care about such things. On my current team I'm seeing a lot of { on the end of a line followed by a blank line ... to make things easier to read? Huh?

Seriously, I never used C but grew up on a bunch of other languages where block markers were usually lined up, so I did that in Java, too. It's all habit, though habits can be changed.

BTW: Yes, Java (and C) compilers ignore whitespace. allowing you to make programs in the shape of steam trains and such for goofy contests. I just took a small program and formatted it to one long line in my text editor. Compiles fine. Python considers indentation significant, I think, and COBOL has special meaning to specific columns from punch-card days, so not all languages let you do this.
[ May 03, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Code readability is often very subjective; people generally think that whatever style they're used to is "more readable". (This is true for many coding standard issues, not just brace placement.) Whichever you prefer, it's worthwhile to spend enough time working with the other that you're perfectly comfortable with it. No matter what standard you may follow, you will encounter code in you career which was written by other people with different standards, and it's useful to be able to read it easily.

[Stan]: it saves vertical space in code samples in magazines where they care about such things.

I realize the style was originally intended to save space in printed media. But even when using only electronic media, I find it helpful to not use excessive vertical space. I find it easier to understand a method that fits easily on my screen all at once, rather than one that I need to use page up and page down to see the whole thing. Eliminating the extra newline for opening braces is one tool that helps me achieve that. Not that I won't insert newlines in other cases if I think it does help readability - but I dislike having to put them in every time there's an if, else, for, while, etc.

[Stan]: Yes, Java (and C) compilers ignore whitespace.

That is, they ignore extra whitespace. It would be bad if they ignored all whitespace.
[ May 03, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I dislike having to put them in every time there's an if, else, for, while, etc.

Relucatant to admit this in public, but I also break the rule "put brackets around single line blocks" after if, else, for. I use Ctrl-Shift-F obsessively (just tried it on this post editor!) and figure the risk of "thinking two lines are in a block when only the first one is" is low enough to live with. It's some effort to put them in later, but no more than before. YAGNI maybe.

It would be bad if they ignored all whitespace.

Yup, right after I wrote that I put a CR in the middle of cl-ass and it didn't work at all.
[ May 03, 2007: Message edited by: Stan James ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

After the "discussion," do we get to vote?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Stan James:
I also break the rule "put brackets around single line blocks" after if, else, for.
I do that too, but only if the controlling statement and the controlled statement are on the same line. Thus:But not:That's because I have been burned (not once but several times) when I change the latter code like this:
Ryan Medrano
Greenhorn

Joined: Apr 29, 2007
Posts: 11
Check it out man.. you post the "{" and "}" at the end of your class name and any methods and statements you use... i.e "while" "if" "else" but if you use "else" you must use "if" too.. you probably know that.. im new at this too... so happy trails!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: position of the opening brace {