Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Beginning curly brace's position? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginning curly brace Watch "Beginning curly brace New topic
Author

Beginning curly brace's position?

Roshni Singhania
Ranch Hand

Joined: Mar 11, 2007
Posts: 78
Hi,

I have a very basic question to ask - where should we put the beginning curly brace, on the same line



or the next line



I'm confused, because in this site's "Chicken Coop" section (if I remember correctly) I read that it it should be on the next line, as shown in eg. 2). But in SCJD part of the 1.5 certification book, it says that it should be on the same line or the marks could be reduced if we put it on the next line. I'm kind of confused as to which one to use. I'm personally used to the way number 2) & get kind of confused at first when reading code which uses the style number 1).
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
This is a style issue, it can be anywhere. There is no need (by the parser) for whitespace, and there is no problem with any added whitespace. (Comments are counted as whitespace by the compiler.)

It's generally a good idea to have a consistent coding style within your code. It's also good to have a consistent coding style any any group of developers working together.

I differ with the Chicken Coop on a few issues, but understand their reason to make the choice.

Beware, the debate can become a religious issue.



All valid Java (and C) code.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

indeed, anywhere you work will have a style guide. contrary to what some folks will tell you, no style is better or worse. you should follow whatever guide is appropriate for the project. If you are doing a SCJD project, use their style guide. if you are doing the JavaRanch cattle drive, use theirs.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Roshni Singhania:
...in SCJD part of the 1.5 certification book, it says that it should be on the same line or the marks could be reduced if we put it on the next line...

For what it's worth (another matter of debate), Sun recommended Code Conventions for the Java Programming Language.

This is the style preferred for the SCJD assignment, although I don't know whether you would actually lose points for not following it.


"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
cortin flaneir
Greenhorn

Joined: May 26, 2008
Posts: 13
this is not an hard and fast that you must put it in same line. but its standred pattern recmonded by sun . that you put it in same line.for further information fallow the coding style guide by sun microsystem.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
As there are no prons and cons in using any of the styles, if you are still confused just toss a coin.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by fred rosenberger:
indeed, anywhere you work will have a style guide. contrary to what some folks will tell you, no style is better or worse. you should follow whatever guide is appropriate for the project. If you are doing a SCJD project, use their style guide. if you are doing the JavaRanch cattle drive, use theirs.


I totally agree - except for the fact that *my* coding style is the only true one...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Shashi Kala
Ranch Hand

Joined: Jan 27, 2008
Posts: 46
I saw in many books using the first style(Eg. Java Complete reference). But I use second style. I think the second style ensures more readability.
That depends on the coding style you follow..But it's good to follow the uniform style.

And I don't think that marks get reduced for this reason.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The "brace-on-the-same-line" style originated with Kernighan and Ritchie's "The C Programming Language". They have gone on record saying that this was only done to save vertical space in the book, enabling them to keep listings together on a single page.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

So you're saying that this entire braces war ( ) is caused by a lack of space in a book?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ilja Preuss:
... I totally agree - except for the fact that *my* coding style is the only true one...

That's only correct if your coding style happens to match my coding style.
Roshni Singhania
Ranch Hand

Joined: Mar 11, 2007
Posts: 78
Originally posted by Shashi Kala:

And I don't think that marks get reduced for this reason.



Actually I'd read so in the book K&B 1.5 SCJP & SCJD certification book.. in Chapter 11 (Understand the Sun Certified Java Developer Exam Process).. it's mentioned twice -


First on Page 8 -


We�ve seen people fail the exam because they put the curly braces on the line below the method declaration rather than immediately following the declaration (on the same line), violating the official Java Coding Conventions. While this infraction alone probably might not cause you to fail, the points deducted for code convention violations might be the ones that sink you where you otherwise might have squeaked by.


And then in the summary, on Page 14 -


The Assignment is graded by giving your application a starting number of points and then deducting points for violations including minor things (curly braces in the wrong place) and major things (locking doesn�t work in every situation).
Roshni Singhania
Ranch Hand

Joined: Mar 11, 2007
Posts: 78
Oh and thank you all you guys for replying. So I understand now that for the exam, I've to follow exam's conventions and for projects, I've to follow the project's style guidelines.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Originally posted by marc weber:

That's only correct if your coding style happens to match my coding style.
Marc and I are tending this part of the bar, so Marc's right. What he says goes.

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
I was taught the second example at college and uni, and it is there for my prefered style.
Oddly, a friend and fellow developer said it was more mature to use the first style.

I think "religious" wars have been started for a lot less then vertical space in a book
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Like which Unix text editor is the best.

The answer to which is of course VI.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Ernest Friedman-Hill:
The "brace-on-the-same-line" style originated with Kernighan and Ritchie's "The C Programming Language". They have gone on record saying that this was only done to save vertical space in the book, enabling them to keep listings together on a single page.


I've seen this mentioned a few times on the internet, but I could never find an actual source for this. So I emailed K&R, and got a reply back from Brian Kernighan, the 'K' in K&R. He does not recall ever saying this. He writes :

"My memory of the indentation quote (if it really existed) is dim at
best. The basic style is pretty much what was used in the Unix kernel
and other software written by Ken Thompson, Dennis, and others; it was
pretty much homogeneous except that Ken had a weird way of formatting
else clauses that I thought was deceptive, and he didn't put the
semicolon of an empty block on a separate line, which again I thought
bad. It is true that in general we were working on devices with a
limited number of lines -- either terminals with actual paper, or
screens with 24 lines. Those both encouraged a reasonably compact
style.

I still prefer the style that Ken and Dennis came up with, and still for
the same reasons: it fits somewhat more code on a screen than putting
braces on separate lines, without interfering with readability. But
it's still sort of a religious issue, and I don't mind people who
believe differently."

So this idea that they wrote their braces in the K&R style for publishing reasons is false.

Just thought you'd want to know.


Rob
SCJP 1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Rob Ross:

Just thought you'd want to know.


You got email from Brian Kernighan! That is wicked cool! Thanks for sharing this with us.

Best I ever did, is I have some emails saved someplace that I exchanged with Richard Stallman -- and he emailed me first.
 
GeeCON Prague 2014
 
subject: Beginning curly brace's position?