• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Java Coding Standards

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been given the task of establishing Java coding standards and have reviewed the "Chicken Coop" coding styles. I also have reviewd Sun's "Code Conventions for the java Programming Language" and "The Elements of Java Style" by Allan Vermeulen.
My question is where do I start?
What is the 5% of The "Chicken Coop's" Java Programming Style Guide that has been modified from the JDK source?
What are the more important conventions that should be covered in the standards?
 
Trailboss
Posts: 23036
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow,
For such a short message, there are sure some big questions.
I'm going to avoid those questions in favor of this response:
Different companies adopt different standards. These topics are typically the foundation for religious wars.
A long while after the first draft of the coop, Sun came out with their standards which contradicted several pieces of their actual code. And those contradictions continue to this day.
Naturally, I think what is in the coop (and I have updated it as recently as a few months ago) is the very best way to go. Equally naturally, others will think there are points that should be done in a very different way. I'm happy to discuss any particular point, but to cover the whole document in one message is more than I'm willing to type today.
A common point is the case usage of class names. A point of argument is the case usage of final names.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The one I always notice most, though it's really quite trivial: opening curley braces. The coop standard puts them at the beginning of a new line, same indentation as what went before:
<code><pre> if (someCondition)
{
doThis();
}</pre></code>
And the Sun standard always puts them at the end of the previous line:
<code><pre> if (someCondition) {
doThis();
}</pre></code>
The former looks nicer to me aesthetically, while the second allows me to see more of the structure of code at one time in my editor, which I think is more important. Over such minor details are mighty religious wars fought...
 
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
JWoody, another document you may want to read is 'Writing Robust Java Code' by Scott Ambler.
The URL is: http://www.ambysoft.com/javaCodingStandards.pdf
(you'll need a pdf plugin to read this in your browser)
Jim, I always find the first formatting style much easier to read. You just need a bigger screen
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But with a bigger screen I could see even more code instead! Or if I've been staring at the screen too long I might want to increase the font size so that I can still read it. There are just so many things you can do with screen space other than fill it with curley braces.
Actually I wish more languages were like python, eschewing curly braces entirely and letting block struture be defined entirely by indentation. That way it looks good and is compact. Ah well...
Incidentally there's a book called The Elements of Java Style which has gotten pretty good reviews at Amazon. Might be worth checking out.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and on further reading I see that the book I cited is actually descended from the AmbySoft document George cited. An amusing coincidence - I guess this counts as multiple recommendations.
[This message has been edited by Jim Yingst (edited September 28, 2000).]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coding standards, heck any standards are like.... religion.
Or maybe, not.
The standards have to be followed/enforced/followed, lol.
Set, oops, define, then set, your own standards.
Follow YOUR own standards.
Post back and hopefully the silly standards questions disappear.
Squishees are the fundamental food for life.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking of standards and naming conventions:
take a look at this link: http://www.javaranch.com/ubb/Forum10/HTML/000180.html if you have only one name like "JWoody"
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to curly braces: My personal preference is the Sun way. It seems to be the standard found in most books. (Although "Java Server Programming" from Wrox uses both methods... the result of having different chapters written by different authors, no doubt.) When I created our standard, I left this as an open issue. As long as developers were consistent within a class and didn't mix styles, I didn't think this was a big issue.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, yeah, a curly braces argument! Let me have some.
Here's where the ancient style of the opening brace on a new line came from: In the old (really old) days of K&R C, we didn't have function prototypes and we put the formal parameters before the opening brace (now, that's old). When you wrote a function with arguments, you wrote something like:<pre><code>
int foo()
int a;
char c;
{
...
}</code></pre>
And so the practice began, and it persists like other outworn traditions. Eventually it'll fade away, because the newer style allows more code density and actually adds clarity - <code>{</code> just looks too much like <code>}</code>.
Of course, that's just my opinion and I could be wrong...
jply
 
paul wheaton
Trailboss
Posts: 23036
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought that that brace placement came from Pascal programmers moving into the C world.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dunno about "eventually it will fade away, because the newer style..." At this point both styles have been around for some time, and I don't think most people know or care which is older - they just know which one they like, and will continue to use the one they prefer unless required to do otherwise. IMO of course.
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never used Pascal until Delphi, so I don't really know, but didn't C predate Pascal, and didn't Pascal originally use <code>begin</code> and <code>end</code> vice braces?
 
paul wheaton
Trailboss
Posts: 23036
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pascal used begin and end. When you move to C, you're told that the left brace is begin and the right brace is end. So guess where the braces go?
C came out before Pascal, but not by much.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, the { on a separate line will go away because Sun and the majority of tutorial books are pushing { on the same line. I just wish someone would come up with a good way to handle the empty catch problem:

When you first look at it, it appears that the "//more code" belongs inside that catch block until you notice the closing brace. I'm beginning to think that the Python solution really is the best.
 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to force myself to convert to the Sun/K&R way at the moment. It's just that in the current team, there are two separate camps, the BSD style vs. the Sun/K&R style. As Java is a Sun technology, I believe I should fall into line with their coding style. Besides, probably my only reason for using the original BSD style is because I've been conditioned by too much C&C++ programming. Hopefully, it'll be easier than giving up smoking was
What's more, it would be nice to have a consistent style across all the code produced for the project.
I still think that the BSD style looks more readable, things look more structured to me that way. But what Thomas says is true, and I have to admit that the vast majority of Java coding examples and Java books I have seen use the Sun/K&R style.
[This message has been edited by George Brown (edited October 10, 2000).]
 
paul wheaton
Trailboss
Posts: 23036
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George, you're confusing me.
K&R is like this:

And Pascal style is like this:

 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, my goof, I've changed the post above to refer to it as the BSD style.
you must admit that 'Pascal-style' is a tad confusing too, as Pascal didn't use curly-braces.
Having mulled it over, I think the name for the style I'm talking about is the BSD style (my colleague informs me that it's also called the Allman style)...
BSD/Allman Style:

K&R/Kernel Style:

"... Hal, I think my mind is going ..."
[This message has been edited by George Brown (edited October 10, 2000).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And then there's the Python style:

Nice and clean with no annoying braces. And best of all, it automatically forces indentation since the indentation replaces the braces.
[This message has been edited by Thomas Paul (edited October 09, 2000).]
 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
python-style gets my vote. peace through removal. although i'm not sure about python itself.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a perl replacement, I really like Python. Where Perl had OO thrust upon it, Python was built OO from the ground up.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!