Summary of Coding Standards


Table of Contents

1. References
2. Introduction
3. Documentation
4. Naming
5. Spacing
6. Other style conventions
7. Specific MMBase issues
7.1. Logging
8. Input validation
9. String operations
10. Portability
11. Minimize ripple effects
12. Error message conventions
13. Exception handling
14. Other conventions

The following is a summary of coding standards that need to be applied to source code.

All future sources need to conform to these standards.

The style conventions were based on suggestions from the following documents:

We understand that formatting is a matter of taste and that many developers prefer other formmatting styles, but try to stick to the guidelines. We are using them to keep the code readable. Don't take it personally when someone points you to the coding standards when you submit some code. We will accept your code anyway.

Every method, whether public or private, must start out with a documentation comment that describes what the method does. The documentation should mention every parameter received by the method, every possible return value, and (if not obvious) the conditions under which the method could return an error. Put the parameter names in the same case in the doc string as in the method signature,

Read over the MMBase code to get an overview of how documentation looks in practice; in particular, see src/org/mmbase/bridge/* for examples.

If you are one of those developers who has a personal issue with documentation then at least document the public methods of your code. Public methods can be used by everyone and should have a clear contract. One exception to this rule are getter and setter methods.

MMBase has it's own logging facility (similar, but older than commons logging).

Every class has its own logger, identified with the name of the class. The logger instance is final static private. It is not necessary that the logger instance is used, and therefore this is perhaps the only variable which need not be removed if it is not used.

MMBase has the following logging levels, from low to high.

When using the value of a variable in the log message and there exists even the tiniest chance that this variable's toString evaluates to the empty String, single quotes must surround the value. Make absolutely sure that not accidently an Exception (especially NullPointerException) can be caused during the creation of the message.

The first lines of a method are usually devoted to checking the validity of all arguments. The idea is to fail as quickly as possible in the event of an error. This is particularly important for constructors.

For non-private methods, an Exception is thrown if a test on an argument fails. This is often IllegalArgumentException or NullPointerException. (These are RuntimeExceptions. Checked exceptions may also be thrown Document these exceptions in the @throws clause of the method's javadoc, since they clearly state the method's requirements to the caller (the pre-conditions).

If every object parameter of every method in a class needs to be non-null in order to avoid throwing NullPointerException, then it is acceptable to state this once in the general class javadoc, instead of repeating it for each method.

To build Strings dynamically, one may use either the String concatenation operator + or the StringBuffer class. In the great majority of cases, only a few items are concatenated, and either style may be used freely, according to taste, without concern for performance.

On relatively rare occasions, however, when performing extensive String manipulation, replacing + with StringBuffer.append is recommended. This is because the + operator does not scale well to large numbers of operations, and is much slower than StringBuffer.append under such circumstances.

Cases in which + is very likely acceptable :

Portability is one of the principal advantages of using Java. Guidelines for ensuring that a Java application remains portable are:

Much of object programming is centered on minimizing the ripple effects caused by changes to a program. This is done simply by keeping details secret (information hiding or encapsulation).

The principal ways of doing this are

All of these techniques accomplish the same thing - they confine knowledge of implementation details to the smallest possible part of a program. That is, they keep a secret of some sort.

Constant and liberal use of the above techniques is recommended. For example:

For error messages the following conventions apply:

Exceptions in java are used to communicate errors between a callee and a caller. They aren't supposed to be used for control flow (throw and catch in the same method). Exception declarations are usually found on the boundaries of an API.

A section on writing error messages can be found elsewhere in this document under the title 'Error message conventions'.

Here are some of the guidelines for exception handling in MMBase.

There are many other unspoken conventions maintained throughout the code, that are only noticed when someone unintentionally fails to follow them. Just try to have a sensitive eye for the way things are done, and when in doubt, ask.


This is part of the MMBase documentation.

For questions and remarks about this documentation mail to: [email protected]