This page describes the standards used for Apache ServiceMix code (java, xml, whatever). Code is read by a human being more often than it is written by a human being, make the code a pleasure to read. If you're using Eclipse, configuration files for your IDE can be found at http://svn.apache.org/repos/asf/servicemix/smx3/trunk/etc/eclipse/
Lets follow Sun's coding standard rules which are pretty common in Java.
http://java.sun.com/docs/codeconv/
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
Correct brace style:
public class Foo {
public void foo(boolean a, int x, int y, int z) {
do {
try {
if (x > 0) {
int someVariable = a ? x : y;
} else if (x < 0) {
int someVariable = (y + z);
someVariable = x = x + y;
} else {
for (int i = 0; i < 5; i++) {
doSomething(i);
}
}
switch (a) {
case 0:
doCase0();
break;
default:
doDefault();
}
} catch (Exception e) {
processException(e.getMessage(), x + y, z, a);
} finally {
processFinally();
}
} while (true);
if (2 < 3) {
return;
}
if (3 < 4) {
return;
}
do {
x++
} while (x < 10000);
while (x < 50000) {
x++;
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
private class InnerClass implements I1, I2 {
public void bar() throws E1, E2 {
}
}
}
|
All methods of an interface are public abstract, therefore it is not necessary to specify public abstract modifiers. Similarly all fields are public static final.
However this behavior works best with most tools and IDEs and seems to be common practice so we see no reason to disallow this practice. e.g:
public interface MyInterface {
public static final int MY_INTEGER = 0;
public abstract void doSomething();
}
|
This has the added advantage that the interface can be converted into an abstract class (and copy and paste individual definitions) without changing anything.
Preferably add public/static/final to constants, and public/abstract to methods, but it's not mandatory. However, if it's there, don't take it out.
java.lang.Class.forName(String) throws ClassNotFoundExceptionRuntimeException), if the caller has a chance to handle it.RuntimeException) and do NOT throw a checked exception, because if not even your code can handle the problem, in the very most cases the caller has no chance to handle the problem, too. Instead there maybe somebody somewhere in the highest layers who catches all RuntimeException's, logs them and continues the regular service.NullPointerException or RuntimeException. Use either IllegalArgumentException, or NullArgumentException (which is a subclass of IllegalArgumentException anyway). If there isn't a suitable subclass available for representing an exception, create your own.For example, if the module name is common, then the base package name should be org.apache.geronimo.common.
Note: This is more of a guideline than a rule, as some modules simply can not follow this convention, but where applicable they should.
import java.util.Vector and not java.util.*Source -> Organise Imports to organize importsSource -> Format to format code (please use default Eclipse formatting conventions, which are as above)Tools -> Organise Imports to organize importsTools -> Reformat code to format code (uses the code style setting in IDE options)The eclipse formater settings are available here.
@version Should be: @version $Revision$ $Date$
@author Should not be used in source code at all.
*Test.java for unit tests.public static Test suite() or constructor methods, the build system will automatically do the right thing without them.org.apache.commons.logging.Log rather than raw Log4jisTraceEnabled() calls
private static final Log log = LogFactory.getLog(MyClass.class);
public void doSomeStuff(Stuff stuff) throws StuffException {
boolean logTrace = log.isTraceEnabled();
try {
if (logTrace) {
log.trace("About to do stuff " + stuff);
}
stuff.doSomething();
if (logTrace) {
log.trace("Did some stuff ");
}
} catch (BadException e) {
// don't log - leave it to caller
throw new StuffException("Something bad happened", e);
} catch (IgnorableException e) {
// didn't cache this as we don't expect to come here a lot
if (log.isDebugEnabled()) {
log.debug("Ignoring problem doing stuff "+stuff, e);
}
}
}
|