Java Coding Standards
naming - documentation
- formatting - rules
The most important thing of standards is being consequent,
always using the same choices of naming, code structure, etc.
Your code will be easier to understand and to write, you will easier
remember variable, type and procedure names. This, together with
structured programming techniques makes documentation only necessary
for special non-obvious things in your code.
Naming Conventions
final int A_GLOBAL_CONSTANT= 5;
// Constants
class NameOneTwo // Classes
int mVarAbc; // Class Attributes: start with 'm'
static StatusInfo sStatus; // Static Variables: start with 's'
int handleError(int errorNumber) // Methods & Method Arguments
{
Time time_of_error; // Local Variables
}
Names should make clear what they represent, it is half of
your documentation, don't be afraid to make them long
Use the following standard abbreviations, for instance as a suffix:
- Max, Min - to mean the maximum value something can have.
- Cnt - the current count of a running count variable.
- Key - key value.
- Nbr - number
- Sz - size
- Prev - previous
- Proc - procedure
- Id - unique identifier
Prefixes can be useful for method names:
- Is - to ask a question about something (boolean
variable).
- Get - get a value.
- Set - set a value.
Create standard abbreviations for your project, for word &
concepts you will use a lot
Documentation
Besides the standard diagrams like the Nassi Schneider algorithm and
the UML class diagram, put all documentation in the code. With a clear
structure and good naming of variables and procedures, not much extra
explanation is
necessary. eg PROCEDURE NbrItemsChosen() : don't add
redundant documentation like " this procedure returns the number of
Items chosen!"
Explain only special, non-obvious unexpected things. Furthermore, your
program
should be userfriendly, so keep it simple.
Comment policy:
explanation of class (terminology, functions,
reasons, ...)
In implementation: comment only where necessary!! Special
non-obvious things.
Comments should document decisions
Formatting
Put a space in between '=', '==', all
operators and after comma's!
Indentation Policy:
switch (...){ // Switch Format
case 1:
...
// FALL THROUGH
case 2:{
int v;
...
break;
}
default:
}
int SomeMethod(){
if (condition){ // Braces {} Polic
...
}
while (6 == errorNum){ // Condition Format
...
}
}
Rules
Only Initialize Variables when Necessary
Use Short Methods
Only one Statement Per Line
Usually Avoid Embedded/Complicated Assignments
eg: while ((c = getchar())
!= EOF) or i = j++;