Coding Styles

Java (except MathletFactory and MIAU)

1. No tabs

Different editors may (and usually will) interpret tabs differently.
This can destroy the formatting of the code.

2. Indent in GNU style with a depth of 2 characters

Examples:

if ( resultSet.next() )
  {
int id = resultSet.getInt(DbColumn.ID);
// ...
}
public int getName ()
{
return this.name;
}
public Class Test
{
// ...
}

(Note: no indentation of the {} block of method and class definitions)

3. In method/constructor definitions, separate name and argument list by exactly one space

Example:

public void setType (int type)
{
this.type = type;
}

WRONG:

public void setType(int type)
{
this.type = type;
}
public void setWidth  (int width)
{
this.width = width;
}

Note: This rule does only apply to method/constructor definitions, not to method/constructor calls. For the latter see the next rule.

4. In method/constructor calls, avoid whitespaces between name and argument list

Write the argument list directly after the method/contructor name provided the argument list is not too long. Example:

long created = data.getAsLong("created");

WRONG:

long created = data.getAsLong ("created");

If the argument list is very long, start a new line for the argument list, with an indentation of two spaces with respect to the previous line. Example:

ResultSet resultSet = this.debHelper.getDataOfReferencedDocuments
(this.fromDocType, this.fromDocId, this.toDocType, new String[] {DbColumn.ID, DbColumn.NAME, DbColumn.DESCRIPTION});

If the line is still very long, break it in several lines like this:

this.logDebug
(METHOD_NAME + " 1/5: started." +
" type = " + type +
", id = " + id +
", withPath = " + withPath +
", onlyLatest = " + onlyLatest);

5. Separate arguments by one comma and exactly one space

Separate arguments of methods and constructors by ", " I.e., by one comma and exactly one space. Put no spaces after the opening or before the closing bracket. Example:

initProcess("service", name, count, true);

WRONG:

initProcess("service", name,count,true);
initProcess("service" , name , count , true);
initProcess( "service", name, count, true );

6. Surround boolean expressions with exactly one space

Insert one space after the opening bracket and one before the closing bracket
of a boolean expression. Example:

if ( value > 2 || value < -1 )
  {
// ...
}

7. Exactly one space before and after operators

Examples:

String name = "foo";
return (name != null ? name : "unknown");

WRONG:

String name= "foo";
return (name != null ? name: "unknown");

= 8. Names of classes and variables

Start names of classes with a capital letter, those of variables with a
lower-case letter. Use speaking names. Separate adjacent name parts by capitalizing the
first character of the next part. In exceptional cases, "_" may be used for separation.

Examples:

absFilename
getAbsFilename

WRONG:

f // not a speaking name
abs_filename

Names of static final variables should be capitalized altogether; name parts
separated by "_"

Example:

static final int MAX_LINE_NUMBER = 10000;

9. Maximum line length: 90 character

Exceptions are allowed. However, keep even below the limit of 90 if possible.

Separate code blocks by exactly one blank line

Example:

// Initialize services:
dbHelper = (DbHelper)this.serviceManager.lookup(DbHelper.ROLE);
user = (User)this.serviceManager.lookup(SessionUser.ROLE);
// Process parameters:
int id = ParamUtil.getAsId(this.parameters, "id");
int type = ParamUtil.getAsInt(this.parameters, "id");

WRONG:

// Initialize services:
dbHelper = (DbHelper)this.serviceManager.lookup(DbHelper.ROLE);
user = (User)this.serviceManager.lookup(SessionUser.ROLE);
// Process parameters:
int id = ParamUtil.getAsId(this.parameters, "id");
int type = ParamUtil.getAsInt(this.parameters, "id");

This applies to the space between function definitions and top-level comments,
too. Example:

/**
 * Sets the id.
*/
public void setId (int id)
{
this.id = id;
}
/**
 * Returns the id.
*/
public id getId ()
{
return this.id;
}

WRONG:

/**
 * Sets the id.
*/
public void setId (int id)
{
this.id = id;
}
/**
 * Returns the id.
*/
public id getId ()
{
return this.id;
}

WRONG:

/**
 * Sets the id.
*/
public void setId (int id) {
this.id = id;
}
/**
 * Returns the id.
*/
public id getId () {
return this.id;
}

Add picture from clipboard (Maximum size: 500 MB)