问题
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.
回答1:
Four spaces - it's what God uses.
回答2:
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.
回答3:
Although it may look dated, not much has been added to the core language itself ( the libraries have had a lot of a lot of additions though )
Those I can remember are right now are enum
and generics, the rest was already there when the document was first written.
Use 4 spaces always.
Don't use K&R or Allman: While it is perfectly acceptable for C, C++ and C# it's not always the case for Java (unless the project has explicitly decided to use it ). Using K&R or Allman in Java is visually unpleasant as it would be not to use it in C.
Use braces always, including single line statements.
Generally speaking try not to mix styles between programming languages. It's like the accent pronunciation in a natural language, you may be understood, you can read and write in it, and have a workable level in the language, but a deficient pronunciation will only annoy the native speakers.
回答4:
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.
回答5:
Ray Ontko has a Bibliography of Java Coding Standards. But it would be difficult to identify any single standard as "widely adopted".
回答6:
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.
回答7:
Check Elements of Java Style
回答8:
Official coding conventions are found at this URL: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
回答9:
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.
来源:https://stackoverflow.com/questions/1365210/are-there-any-widespread-modern-java-coding-conventions