Forums Register Login

Coding Style Standards

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
Cool. Thanks for the responses folks. I was interested in seeing how other people were doing it. Looks like a good mix.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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/
+Pie Number of slices to send: Send
I'll second that motion for Jalopy -- I love it, and it works great with not-quite-IDE text editors like Textpad.
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1198 times.
Similar Threads
Java Coding Standards
Code conventions & design doc size
Compile error
how to continue a for loop after breaking?
Upcasting of Classes Problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:14:50.