Coding Conventions
Some more general guidelines
- Fully qualified imports should be used, rather than importing x.y.*.
- Use newlines for opening braces, so that the top and bottom braces can be visually matched.
- Aid visual separation of logical steps by introducing newlines and appropriate comments above them.
A class that conforms to JBoss coding guidelines
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package x;
// Explicit imports
import a.b.C1; // good
import a.b.C2;
import a.b.C3;
// Do not write
import a.b.*; // bad
// DO NOT USE "TAB" TO INDENT CODE USE *3* SPACES FOR PORTABILITY AMONG EDITORS
/**
* A description of this class.
*
* @see SomeRelatedClass.
*
* @version <tt>$Revision: 1.3 $</tt>
* @author <a href="mailto:{email}">{full name}</a>
*/
public class X
extends Y
implements Z
{
public void startService() throws Exception
{
// Use the newline for the opening bracket so we can match top
// and bottom bracket visually
Class cls = Class.forName(dataSourceClass);
vendorSource = (XADataSource)cls.newInstance();
// Jump a line between logically distinct steps and add<
// line of comment to it
cls = vendorSource.getClass();
// Comment lines always start with an uppercase
// except if it is the second line
if(properties != null)
{
try
{
}
catch (IOException ioe)
{
}
for (Iterator i = props.entrySet().iterator(); i.hasNext();)
{
// Get the name and value for the attributes
Map.Entry entry = (Map.Entry) i.next();
String attributeName = (String) entry.getKey();
String attributeValue = (String) entry.getValue();
// Print the debug message
log.debug("Setting attribute '" + attributeName + "' to '" + attributeValue + "'");
// get the attribute
Method setAttribute =
cls.getMethod("set" + attributeName, new Class[] { String.class });
// And set the value
setAttribute.invoke(vendorSource, new Object[] { attributeValue });
}
}
// this is a bad comment line because it starts with a lower case
vendorSource.getXAConnection().close();
// Bind in JNDI
bind(new InitialContext(), "java:/"+getPoolName(),
new Reference(vendorSource.getClass().getName(),
getClass().getName(), null));
// Block must always be delimited explicitely
if (0 == 0)
{
System.out.println(true);
}
}
}