Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

position of the opening brace {

 
Vipin Thomas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already listed the advantages, there can not be any difference of using it that way for the compiler.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the "discussion," do we get to vote?
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic