Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginning curly brace's position?

 
Roshni Singhania
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
cortin flaneir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shashi Kala
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you're saying that this entire braces war ( ) is caused by a lack of space in a book?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like which Unix text editor is the best.

The answer to which is of course VI.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic