|
Last update : September 25 2001
About
What is Cactus ?
Features/Status
Goals
News/Changes
Roadmap/Todo
Contributors
Contributing
License
Feedback
Downloads
Downloads
Documentation
How it works ?
Getting Started
Mock vs Container
API Reference
Howto Guides
Config Howto
TestCase Howto
Ant Howto
HttpUnit Howto
Sample Howto
EJB Howto
IDE Howto
Support
Bug database
Mailing list
FAQ
Misc.
Why the name ?
Resources
Developers
CVS
Coding Conventions
|
Forewords |
This 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.
|
Cactus specific coding conventions |
1. Brackets |
For class and method declaration, brackets should begin and end on a
new line. Example :
public class SomeClass
{
public void someMethod()
{
}
}
|
Brackets for blocks of code inside methods should begin and end
on the same line (this applies to if ,
for , while , try/catch , ...).
Example :
public void someMethod()
{
if (expression) {
} else if (other_expression) {
}
try {
} catch (Exception e) {
}
}
|
Brackets are mandatory even for single line statements !
// Incorrect
if (expression)
// some code
// Correct
if (expression) {
// some code
}
|
|
2. Blank Spaces |
keywords 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. Indentations |
4 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. Comments |
Javadoc 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. License |
The Jakarta Apache/Cactus License MUST be placed at the top of each
and every file.
|
6. Author references |
If you contribute to a file (code or documentation), add yourself to
the top of the file (below the existing authors). For java files the
preferred Javadoc format is:
@author <a href="mailto:user@domain.com">John Doe</a>
|
|
7. Class variables |
Class variables should not have any prefix and must
be referenced using the this object. Example :
public class SomeClass
{
private String someString;
[...]
public void someMethod()
{
logger.debug("Value = " + this.someString);
}
}
|
|
8. Parameter names |
Method parameters should be prefixed by "the " (for
differentiating them from inner variables). For example :
public void someMethod(String theClassName)
{
String className; // inner variable
}
|
|
9. Line length |
Avoid lines longer than 80 characters for Code, comments, ...
|
10. Versionning |
All .java files should have a @version tag like the one
below.
@version $Id: coding_conventions.xml,v 1.4 2001/09/22 19:19:54 vmassol Exp $
|
|
11. Logging |
Do not use System.out to log. Instead,
use the Cactus logging classes which are a facade to Log4j. Use the
name of your class as the Log4j Category .For
example :
private static Log logger =
LogService.getInstance().getLog(MyClass.class.getName());
public void someMethod()
{
logger.debug("some debug text");
}
|
Try as much as possible to log entry and exits of methods with
the parameter values. Cactus logging interface provides 2 methods
for this : logger.entry() and logger.exit() ,
used as follows :
public void someMethod(String theClassName)
{
logger.entry("someMethod([" + theClassName + "])");
[...]
logger.exit("someMethod");
}
|
This will translate in the following log :
3435 [ApplicationServerThread] DEBUG some.package.MyClass - >someMethod([SomeClassName])
3436 [ApplicationServerThread] DEBUG some.package.MyClass - <someMethod
|
If there is no value in logging the parameters (for example because
the object passed as parameter do not have a string representation
and you cannot add one), use the following :
public void someMethod(InputStream theInputStream)
{
logger.entry("someMethod(...)");
[...]
logger.exit("someMethod");
}
|
This is because you could have a method within your class with the
same name but without parameters and we need to differentiate that
in the logs.
|
12. Exception handling |
Managing exceptions correctly requires experience. This is not
supposed to be a guide on managing exceptions, simply a few best
practices.
-
Rule 1 : Try to catch exceptions as much as
possible and rethrow higher level exceptions (meaning hiding the
low level detailed and putting a message that is more related to
the function of your code).
-
Rule 2 : It is important not to loose the stack
trace which contains important information. Use chained exceptions
for that. Cactus provides a
ChainedRuntimeException
for chaining runtime exceptions.
-
Rule 3 : Always log the exception at the higher
level (ie. where it is handled and not rethrown).
-
Rule 4 : Try to avoid catching
Throwable or Exception and catch
specific exceptions instead.
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);
}
}
|
|
|
|
|