File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Coding Style Standards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coding Style Standards" Watch "Coding Style Standards" New topic

Coding Style Standards

Brock Barnes

Joined: May 18, 2002
Posts: 20
Greetings all. I've noticed in 95% of the Java books I have read that the norm for doing curly braces is:

Yes I'm currently reading "Head First Java" and this is where I got the example... Great book so far!!!
According to the Style guide here at it should be where the beginning and ending curly braces are aligned such as:

I've been coding in PHP for a couple of years now and have always used style standards identical to those here on I'm starting to do some coding in Java now and I'm using the same standards because I'm used to them and I think they make the code way easier to read.
My question is ( finally ), what is the industry standard? Are most business' doing it like the first example which I've seen in many books... or are they doing it like suggested on javaranch? I would hate to get in the habit of coding one way just because I think it is better and have to train myself to code another way because my company prefers it differently. I'd rather train myself now to do the industry standard ( if there is one ).
Thanks in advance for any replies.
[ July 21, 2003: Message edited by: Brock Barnes ]
Joel McNary

Joined: Aug 20, 2001
Posts: 1815
I've seen and used both styles, although my preferred is the former. The latter takes up too many lines....
Actually, my reason for preferring the former is that the "if", "while", "for", or whatever statement already mentally starts the code block for me; I don't really read the '{', and putting it on a separate line draws unnecessary attention to it, IMHO.
However, putting the '}' on its own line is the only way to draw attention to the close block, so that is what I do.
However, after being burned early on in my programming days by a bug, I always use curly braces, even on one-line if/for/while/whatever statements:

My reasoning for this is that, if I ever want to go back and add another line into the for block, the curly braces are already there; I won't forget to put them in. (Like I did on that day so long ago, and suddenly my entire senior project wouldn't work, and I spent days tracking down why not....)
If you don't insist on the curly-braces-for-one-line-statements paradigm, then you are probably better off putting the '{' on its own line, since a "for" or whatever statement will not necessarily have a closing '}'
Be warned, however--this is just the way I code. Your mileage may vary, and you will certainly get alternative views on the subject.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Brock
I think that both sytle are acceptable in the student world.
Some notes from Stanford show me that both sytle are being used to illustrate some concept by Instructor "David Merrill"
One of the Instructor "Nick Parlante" love to use previous one.
For me,I always use the last one to write the programme.

Francis Siu
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

you'll see the first style more in books because it uses less lines. Less lines for the same code translates into cost savings come printing time. I use the second style because personally, I like white space. It makes everything more readable for me.
but the real answer can be found here
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'll vote for aligned braces. The open brace at end of line came all the way from the first K&R C code and then books and it has many years of tradition in the C crowd. I came from REXX and Pascal - all that C tradition is out the window. It's a very personal thing, so I don't argue it much, but set my IDE to reformat code MY way, dammit!

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
Brock Barnes

Joined: May 18, 2002
Posts: 20
Cool. Thanks for the responses folks. I was interested in seeing how other people were doing it. Looks like a good mix.
Bert Bates

Joined: Oct 14, 2002
Posts: 8764
Just as an FYI, Sun recommends :

"Recommends" may be too casual a term. If you take the developer''s exam and submit your code using a different style, you risk getting points deducted!
Sun produces a 'Java Code Conventions' document which outlines all the Java code style conventions they expect.
Hope that helps!

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9043
If the company you work for has a style guide (code conventions document), you'd be wise to follow it. If you're writing for Sun, follow their style guide. If you're submitting assignments for the
JavaRanch Cattle Drive, you should follow the JavaRanch Style Guide.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Brock Barnes

Joined: May 18, 2002
Posts: 20
Wow, a response from the author himself! COOL! Thanks for the info Mr. Bates. I just purchased "Head First Java" yesterday afternoon and I didn't want to put it down last night... I had to pry myself away from it. Great job! I'll consider you being responsible for helping me make that first step towards my SCJP.
Simon Pearce

Joined: Jul 22, 2003
Posts: 4
If you use Jalopy, you can reformat at will. Saves heaps of time. You can configure it to the conventions you like ..
Joel McNary

Joined: Aug 20, 2001
Posts: 1815
I'll second that motion for Jalopy -- I love it, and it works great with not-quite-IDE text editors like Textpad.
I agree. Here's the link:
subject: Coding Style Standards
Similar Threads
Code conventions & design doc size
what is going wrong plz help
Does everybody seriously follow the Style Guide?
NX: Coding Standards
Java Coding Standards