Java Coding rules

background image

Prepared & edit by M.C.

J

ava coding rules


ID

Categ.

Action

1

Source files have the following ordering:

Beginning comments

Package and Import statements

Class and interface declarations

2

format

Lines of code are not longer than 80 characters. Wrapped after comma or before

operator. Wrapped lines are indented and if possible aligned to the beginning of the
expression at the same level in previous line

3

format

All class / method / variable names are created using proper English

4

format

There are no variables / classes / methods / parameters with meaningless names or

names shorter than 3 letters
Exception:

temporary variables used to go through loop

E.g.: for (int i=0; i<array.length; i++) doStuff();

local variables using broadly understood abbreviations, e.g.: ‘c’, ‘ch’ or ‘char’

for character as below(sample reading from a buffer)

‘pos’ for ‘position’, ‘avail’ for ‘available’, ‘off’ for ‘offset’ in advancing over a
string or stream

local variables of long multiword type using class/interface initials, e.g.: ‘rs’ for
ResultSet etc.

5

format

All variables, instance and class names are in mixed case with a lowercase first letter
(CamelCase).
Class names start with capital letters, method names and variables with lower.

When mapping between layers, e.g. between database table/column names and
java objects or xml document and java objects:
- if possible keep consistent naming in both layers
- use Java naming conventions for Java layer (do not repeat other layers conventions
or constraints)

6

format

Variable names do not contain underscore _ (except of constants) or dollar sign $
characters.

7

format

All final static (constants) have their names in upper case with words separated by _

8

format

All packages have their names in lower case

9

format

Opening curly braces are not allowed to be in their own line
E.g.:

if (a < b)
{
doSomething(a, b);

BAD !

}

if (a < b) {

doSomething(a, b);

}

OK !

10

format

Operators are separated by white spaces
E.g.:

a =b + c;

OK !

a=b+c;

BAD !

11

format

Two blank lines used:

Between sections of a source file

Between class and interface definitions

One blank line used:

Between methods

Between the local variables in a method and its first statement

Before a block or single-line comment

Between logical sections inside a method to improve readability

background image

Prepared & edit by M.C.

ID

Categ.

Action

12

format

There are no ‘for’ loops instead of ‘while(true) ‘
E.g.:

for(;true;) BAD !

or

for(;;)

BAD !

13

logic

There are no return statements inside finally blocks

14

logic

There are no ‘if’ statements with direct Boolean literal that can be evaluated during

compilation.
E.g.:

if(true)

BAD !

Exception: debugging statements with final static variables :

if(DEBUG){……}

15

format

There are no empty blocks (if/switch/while/finally/try/synchronize/catch)
The only case when empty catch blocks are allowed is in finally section when releasing
resources and preventing error to occur when resources are not occupied. Such
exception MUST be commented inside the empty catch block.
E.g.
stream = new java.io.BufferedInputStream(stream);
try {
return new PropertyResourceBundle(stream);
} catch (Exception e) {

System.out.println("Comment abort error");

e. printStackTrace();

} finally {
try {
stream.close();
} catch (Exception e) {
// to avoid propagating an IOException back into the caller
// (I'm assuming this is never going to happen, and if it
// does, I'm obeying the precedent of swallowing exceptions
// set by the existing code above)
}
}

16

format

There are no methods with names equal to the name of it’s class (except of

constructors)

17

logic

There are no unused variables

18

logic

There are no unused parameters in the method definitions

19

logic

There are no switch statements without default case and that default case is in every
statement as the last one

20

logic

There are no methods that reassign it’s parameters values

21

logic

Constant values should be defined as ‘final static’

22

perform. There are no Collection.toArray calls without specifying size of the Collection

23

logic

There are no explicitly java.lang.RuntimeException and java.lang.Error caught

24

logic

Null parameters are checked with ‘if’ statements i/o catching NullPointerExceptions

25

logic

There are no duplicate or unused imports

26

logic

No Magic Numbers. There are no literal numbers used inside methods apart from -1, 0

and 1 in loops. All are defined as final static variables.

27

logic

All if/while/for statements/loops use curly braces
E.g.:

if (x < y)

doStuff();

- BAD !!!


if (x < y) {

doStuff();

- OK

}

background image

Prepared & edit by M.C.

ID

Categ.

Action

28

format

Conditional operators/ternary expressions ( boolean ? sth : sth2; ) should not be used in:
- operations where either condition or any of the alternatives are long (multiple
instructions)
- nested conditional logic
It is OK to use them in following cases:

- for short single line methods:

public String pluralSuffix(int numberOfThings) {

return (numberOfThings > 1) ? “s” : “”;
}
or
int max = (a > b) ? a : b;

- for inline decision logic:

int counter = 0;

while (someCondition) {
performAction();
counter++;
}
System.out.println(“Action executed “
+ counter
+ ( (counter > 1) ? “s” : “”)
+ “ times.”);

29

logic

There are no methods longer than 60 lines. Exceptions need to be justified with

comments

30

logic

There are no methods with more than 7 parameters.

32

logic

There are no variables with type being implementation class, when interface can be
used

33

format

No two variables are declared in the same line

34

logic

No static method / variable is accessed using object instance

35

format

All static final fields are put on the first place in class. All static fields (not final) are next.

Then there are non-static fields and then methods.

37

format

There is complete javadoc description for every class and every method

38

format

All exceptions thrown by methods are declared with @throws tag

39

format

All method’s parameters are declared with @param tag and have proper description

40

logic

All exceptions are logged using logging framework, System.out and
Exception.printStackTrace are forbidden.

41

logic

All queries and their parameters MUST be logged on the INFO level BEFORE query is
being executed

42

design

All configuration files containing data that can change during the application life (like
database connection details, LDAP servers URLs, etc) must be stored in external file
located in /config subfolder of the folder to which the ear file is deployed


Wyszukiwarka

Podobne podstrony:
Java Media FreamWork
java 2
BESM d20 Optional Rules
2010 Ne Waza Judo BJJ Rules
Projekt java
JAVA tablice
Inzynieria oprogramowania w ujeciu obiektowym UML wzorce projektowe i Java iowuje
Java Przewodnik dla poczatkujacych Wydanie V javpp5
zasady grupy, java, javascript, oprogramowanie biurowe, programowanie, programowanie 2, UTK, systemy
Eurocode 5 EN 1995 1 1 Design Of Timber Structures Part 1 1 General Rules
praktyczny kurs java NX2M5DXARLEHY6NHXOS5FJHKLI6ACRLQQSDRS2Q
notatki java 08
Pirates Optional Rules
Praktyczny kurs Java
Java praca domowa 10
java 3 id 226367 Nieznany
Draft Import implementing rules 22072008

więcej podobnych podstron