![]() | ![]() |
Apache > Jakarta > Cactus > Participating | Docs for: v1.7.2 | v1.7 Last update: November 29 2007 |
ForewordsThis document describes a list of coding conventions that are required for code submissions to the project. By default, the coding conventions for most Open Source Projects should follow the existing coding conventions in the code that you are working on. For example, if the bracket is on the same line as the if statement, then you should write all your code to have that convention.
If you commit code that does not follow these conventions and
you are caught, you are responsible for also fixing your own code.
Below is a list of coding conventions that are specific to Cactus, everything else not specificially mentioned here should follow the official Sun Java Coding Conventions. How to apply?Having coding conventions is nice but having a way to ensure they are applied is even better ... :-)
The Cactus Ant build file has a Please run this target before committing any code. Cactus specific coding conventions1. BracketsAll brackets should begin and end on a new line. Example: public class SomeClass { public void someMethod() { if (...) { } } } Brackets are mandatory even for single line statements ! // Incorrect if (expression) // some code // Correct if (expression) { // some code } 2. Blank Spaceskeywords followed by a parenthesis should be separated by a space. Example: while (true) { // some code } Blank space should appear after commas in argument lists. Binary operators should be separated from their operands by spaces: a += c + d; a = (a + b) / (c * d); while (d++ = s++) { n++; } printSize("size is " + foo + "\n"); 3. Indentations4 spaces. NO tabs. Period. We understand that a lot of you like to use tabs, but the fact of the matter is that in a distributed development environment, when the cvs commit messages get sent to a mailing list, they are almost impossible to read if you use tabs. 4. CommentsJavadoc SHOULD exist on all your class members (methods + class variables), including the private ones. Also, if you are working on existing code and there currently isn't a javadoc for that method/class/variable or whatever, then you should contribute and add it. This will improve the project as a whole. Also add code comments when you think it's necessary (like assumptions), especially when the code is not obvious. 5. LicenseThe Jakarta Apache/Cactus License MUST be placed at the top of each and every file. 6. Author references
Do not put
However you are very much encouraged to edit the
7. Class variables
Class variables should not have any prefix and must
be referenced using the public class SomeClass { private String someString; [...] public void someMethod() { logger.debug("Value = " + this.someString); } } 8. Parameter names
Method parameters should be prefixed by " public void someMethod(String theClassName) { String className; // inner variable } 9. Line lengthAvoid lines longer than 80 characters for Code, comments, ... 10. Versioning
All .java files should have a @version $Id: coding_conventions.xml 238846 2004-03-13 14:40:48Z vmassol $ 11. Logging
Do not use private static final Log LOGGER = LogFactory.getLog(MyClass.class); public void someMethod() { LOGGER.debug("some debug text"); }
As of Cactus 1.3, LogAspect autmatically logs all method entries and
exits.
12. Exception handlingManaging exceptions correctly requires experience. This is not supposed to be a guide on managing exceptions, simply a few best practices.
An example: public void getTestClass() { try { Class responseClass = Class.forName("some.package.MyClass"); } catch (ClassNotFoundException cnfe) { String message = "Cannot instantiate test class"; logger.error(message); throw new ChainedRuntimeException(message, e); } } 13. Qualified imports
All An example: // Correct import java.util.Date; import java.net.HttpURLConnection; // Not correct import java.util.*; import java.net.*; |