• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Coding Style Standards

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Javaranch.com 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 JavaRanch.com. 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.
-Brock
[ July 21, 2003: Message edited by: Brock Barnes ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Jamie
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Brock Barnes
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool. Thanks for the responses folks. I was interested in seeing how other people were doing it. Looks like a good mix.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
-Bert
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Brock Barnes
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use Jalopy, you can reformat at will. Saves heaps of time. You can configure it to the conventions you like ..
See http://jalopy.sourceforge.net/
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll second that motion for Jalopy -- I love it, and it works great with not-quite-IDE text editors like Textpad.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic