views:

646

answers:

9

Sun's "Code Conventions for the Java Programming Language" was last updated April 1999. Ten years later a lot has changed in the language, as well as general usage patterns. Are there more up to date, widely adopted standards?

Most guidelines omit specifying file encoding and line endings. Sun recommends mixed tabs and spaces. The Eclipse IDE defaults to Eclipse's standard, which is tabs only. The Maven style guide is spaces only. Many style guides, such as JBoss, follow Sun's guidelines, but prefer K&R braces instead of OTBS. Each Apache project has it's own style guide, with slight differences between each one.

+5  A: 

Four spaces - it's what God uses.

codingfloor
It's what Sun uses (for the JDK at least).
Tom Hawtin - tackline
Funny, thats what I use too ;). Seriously though don't worry about file encodings and line endings - I have never heard that come up in a discussion on coding standards.
Corehpf
That's two spaces too many.
Apocalisp
@Tom H: Sun might *specify* that they use 4 spaces, but if you look at the source it's quite clear that they mix tabs at 8 characters with spaces; either that or the majority of the core lib developers have no sense of what good indentation is.
Software Monkey
@Tom H: Sun used the old default Emacs formatting, that uses four character indenting, but achieves it using tabs for every eight character block, and makes up the difference with four spaces. This dates back to a time where using 1 byte over 8 for two levels of indent (per line) started adding up to real storage space and money.
Dilum Ranatunga
+2  A: 

Ray Ontko has a Bibliography of Java Coding Standards. But it would be difficult to identify any single standard as "widely adopted".

Stephen C
+5  A: 

Although it may look dated, not much has been added to the core language it self ( a lot of additions have been introduced in the libraries though )

What I can remember are: Enum, and Generics right now, the rest was already there when that document was first written.

Use 4 spaces always.

Don't use K&R. While it is perfectly acceptable for C, C++ and C# it is no always the case for Java ( except of course when a project has decided to use it ). Using K&R in Java is visually unpleasant as it would be not using it on C.

I would add use always braces, including single line statements.

Generally speaking try not to mix styles between programming languages. It's like the pronunciation of natural language, you may understand it properly and read and write in it, and have a workable level in the language, but a deficient pronunciation will only make it difficult to the native speakers to understand.

OscarRyz
I feel a lot has changed. Both in terms of features, and how people use the language. Evolution is why we have HashMap not Hashmap. The language now has annotations, static imports, autoboxing, foreach, etc.. I even find it hard to read Java 1.0-1.2 code. Unicode is common enough that you now find programs with Unicode character in comments and strings. Items like "GNUmakefile - The preferred name for makefiles. " could probably be removed from the Sun coding standard.
brianegge
+3  A: 

The only coding standard you really need to follow is that as accepted by your project team. You might not agree with tabs instead of spaces, but if that is the coding convention of your team, you will do best to follow it.

akf
In trying to get a team to consensus, it's often quite useful to have a list of common standards, so you can say: "I think we should adopt standard X" or "standard Y is used by many other projects". Hence the question.
brianegge
I agree. When trying to start a team or build consensus, you are absolutely correct. Unfortunately, as you point out, your 'list of common standards' often contains conflicts!
akf
A: 

BSD/Allman is the only decent indent style. It meets the basic rule of curly brackets - if they sit on different lines, they should sit on the same column. Even Horstmann is tolerable compared to K&R.

Always put curly brackets if you code in notepad. Otherwise, due to the auto-indenting features of your IDE, it is useless and annoying.

Marian
+2  A: 

The question doesn't ask what your coding style is, but rather for existing coding standards.

I found the European Space Agency Java Coding Standards (pdf) (alt link) which seems up to date and comprehensive, though I'm not sure how widespread the adoption is.

brianegge
"Rule 1: Use the Apache Ant tool to automatically build your project."I guess that means that there are quite a few broken open source projects now. ;-)
Wilfred Springer
Yes, but this is better than Sun's recommendation of 'GNUmakefile - The preferred name for makefiles.' As far as standards go, it's a pain to pick up a project where the last developer used Groovy/Ruby/Maven etc.. to build their project. You can compile your project with awk as far as I care, as long as you also provide or export an ant file.
brianegge
A: 

Check Elements of Java Style

Wilfred Springer
This book is almost as old as Sun's guide.
brianegge
+1 This is an outstanding book that is still applicable, despite its age.
Mocky
+1  A: 

Consider using the one used by default by your IDE's reformatter mechanism. Will save you a lot of time in the long run.

We enable save actions in Eclipse and tick Format Source so the sources always are reformatted. This means that a reformat only changes things happened since last time the file was saved. Is nice in source control history.

You can naturally take your time and define your own format, but it tends to be easier just using the Eclipse standard - it is ok for us.

Thorbjørn Ravn Andersen
A: 

please check the articles java coding standards and best practices
There have some very userful standards and guidlines.

Jimmy