Private: Used whenever no other qualifyer
is necessary
Protected: Used when a subclass should be
allowed to overwrite the value
Public: Used for methods that should be
called from outside
All
variables in a class are private or protected. Write setXXX() and getXXX() methods to access them from
outside, if necessary.
Exception:
Global constants are defined as public static. Do not use final, unless
it is absolutely necessary! Do not declare static variables if you do
not use them as global constants!
All public
methods must be safe to be called at any time, from any place.
Whenever
possible, don’t use class variables.
Always use a qualifyer! Package
structure might change, then all unqualified variables change their accessability.
We don’t want that!
All final
variables use uppercase letters and underscores only (FILE_MAX).
All
variables that are meant and used as global static variables use
uppercase letters and underscores only (FILE_MAX).
All
variables have meaningful names, exceptions are loopcounters,
enumerations and exceptions, which
might have only 1-letter-names.
All Labels or JLabels start with
l_.
Only Labels or JLabels start with
l_.
All Panels or JPanels start with
p_.
Only Panels or JPanels start with
p_.
All Buttons
or JButtons start with b_.
Only
Buttons or JButtons start with b_.
All Textfields
or JTextfields start with t_.
Only
Textfields or JTextfields start with t_.
All methods
start with lowercase letters.
All classes
start with uppercase letters.
All variables
start with lowercase letters.
Use different
names for different values. The only variables, that might change their meaning
during the program, are 1-letter-names used for loopcounters, enumerations
and exceptions. When recycling variable
names, make REALLY sure the variable is no longer in use before you
reuse it!
When using
nested loops with 1-letter-variables, variables of inner loops should be direct
successors of variables of outer loops.
for (int i =
0; i < WHAT_EVER; i++) {
for (int j = 0; j < WHAT_EVER;
j++) {
for (int k = 0; k < WHAT_EVER; k++) {
doAnything(i, j, k);
}
}
}
Declare
loop variables inside of loop headers whenever possible, to make sure,
the variable is only used in the loop.
for (int i =
0; …) {
Arrays are
declared by putting the brackets to the type, not to the name of the
variable:
private Hashtable[]
data = new Hashtable[10];
instead of
private Hashtable
data[] = new Hashtable[10];
All classes
are commented with JavaDoc-Style comments like that:
/**
* Description of Class (Functionality,
possible errors, anything of
* interest to a user of the class)
*
* Main author
* primary Version number (of the first
release the class is in)
* Further version numbers ( of further
releases, which use changed
* versions of this class)
*
*/
public class MyClass
extends MyBaseClasse implements MyInterface {
All methods
are commented in JavaDoc-Style like that:
/**
* Description of Method (Functionality, possible errors, anything of
* interest to a user of the method)
*
* first author
* for each change: Name of Changer, Release
which last used unchanged
* method, Type and Reason of change (if you change code used in v1.1 and
* release it in v1.2, comment looks like
Author: v1.1: reason)
*
* Parameters that go in
* Return Values that go out
* Exceptions that might be thrown
*
*/
private static
Boolean myMethod (String parameter) throws MyException
All variables
are commented in JavaDoc-Style like that:
/**
* name and usage of the variable
*
*/
private static
boolean myVariable;
If there
are many variables declared globally, a shortened style may be used:
/** name and usage of
the variable */
private static
boolean myVariable;
Use this
method ONLY after you made sure, so many global variables are necessary!
The
following case-sensitive Javadoc-tags should be used in the appropriate
places:
@author for the name of the author of a
class. Multiple authors mean multiple lines starting with @author
@version for the version of the class
@param for all incoming parameters
@return for all return values that are not
void
@exception for all exceptions that might be
thrown by that method. Multiple exceptions mean multiple lines
@see for references to other methods or
classes
/**
* Class description here…
*
* @author
Name of first author
* @author
Name of second author
* @version v0.1
* @see java.lang.Object
*
*/
/**
* Method description here…
*
* @param
name the
field name.
* @param
column the
field column.
* @return a field with the specified name and column.
* @exception NoSuchFieldException if a field with the specified name is not
found.
* @exception SecurityException if
access to the information is denied.
*
*/
public
Field getField(String name, int column) throws NoSuchFieldException,
SecurityException
JavaDoc
Style ( /** … */ ) is used for comments that are directed at the user
of the code
C++ Style (
// ) is used for comments that are directed at the programmer/developer of the code
C Style (
/* … */ ) is used to comment blocks of code, that are not used at
the moment, but might be used later.
Comment as
much as necessary. Rule of thumb: If you don’t understand your code at
the morning after a really good party, you did not comment enough!
Similar
code that appears at two or more positions wants to be a method of its
own.
Code that
initially constructs the GUI belongs to the init()-Method.
The
init()-Method looks like that:
new Container
new Subcontainer
new Component1
new Component2
Subcontainer.add(Component1)
Subcontainer.add(Component2)
Container.add(Subcontainer)
Beginning
curly brackets ({ ) are in the same line as the code that opens it.
Ending curly Brackets ( }) are always in a new line. Exception: Empty
exceptionhandlers that only throw away the exception.
Example:
try {
if (true) {
doSomething( );
}
else {
doSomethingElse( );
}
}
catch (Exception e) {
}
Don’t use bracketless shortcuts like
that:
if (true)
doSomething( )
else
doSomethingElse( );
For
indentation use 1 Tab, equivalent to 4 spaces. Don’t mix usage of space
and tab!
Use empty
exceptionhandlers ONLY if you expect an exception to happen and
you are sure you don’t need to react to it. Exceptionshandlers for unexpected
exceptions should at least contain one line of output to simplify debugging.
When you
get a class interface to implement, NEVER change public methods without
asking for permission. Other people might already write code that uses this
methods.
When you
get a class interface to implement, NEVER change other classes without
asking for permission. Other people might already write code in this classes.
If you
think a definition is especially stupid or useless, ask for reasons!
Other people make mistakes too.
If you feel
the need to throw an exception, ask for permission. All other people
using your method must change their code because of that!
Catch all
other exceptions. Don’t forget RuntimeExceptions, which you don’t have
to declare in your method signature, but should catch in your method if you
could expect them to arise!
Don’t use Enumerators
or Iterators with Vectors. Although it is a possible and working way,
it’s usually easier to go through a Vector using a for-loop. Enumerators
and Iterators should only be used with Hashtables.