Java Coding Rules
Java Coding Rules
Printed in France
NORTEL CONFIDENTIAL
The information contained in this document is the property of Nortel Networks. Except as specifically authorized in
writing by Nortel Networks, the holder of this document shall keep the information contained herein confidential
and shall protect same in whole or in part from disclosure and dissemination to third parties and use same for
evaluation, operation and maintenance purposes only.
The content of this document is provided for information purposes only and is subject to modification. It does not
constitute any representation or warranty from Nortel Networks as to the content or accuracy of the information
contained herein, including but not limited to the suitability and performances of the product or its intended
application.
This is the Way. This is Nortel, Nortel, the Nortel logo, and the Globemark are trademarks of Nortel Networks. All
other trademarks are the property of their owners.
without notice. Nortel Networks assumes no responsibility for errors that might appear in t.All other brand and
Java Coding Rules
PUBLICATION HISTORY
8/Mar/2005
Issue 01.00 / EN, Draft
Creation
6/Apr/2005
Issue 01.01 / EN, Standard
Nortel confidential
CONTENTS
1. INTRODUCTION............................................................................................................................4
1.1. OBJECT ....................................................................................................................................4
1.2. SCOPE OF THIS DOCUMENT .......................................................................................................4
1.3. AUDIENCE FOR THIS DOCUMENT ................................................................................................4
4. READABILITY ...............................................................................................................................6
4.1.1 Limit the class members visibility ....................................................................................6
4.1.2 Limit the nested if ............................................................................................................7
4.1.3 Limit the loops complexity ...............................................................................................8
4.1.4 Avoid the abusive string creation ....................................................................................9
4.1.5 No “implements” for constants use .................................................................................9
4.1.6 No “magic numbers”........................................................................................................9
4.1.7 Iterators ...........................................................................................................................9
4.1.8 Reduce variable scope....................................................................................................9
4.1.9 Short code lines ..............................................................................................................9
4.1.10 Do not use tab character...............................................................................................10
4.1.11 Naming conventions......................................................................................................10
4.1.12 Methods factorization ....................................................................................................10
4.1.13 Variable creation at use moment ..................................................................................10
4.1.14 Class methods order .....................................................................................................10
5. PERFORMANCE .........................................................................................................................11
5.1.1 Avoid appending to string within a loop ........................................................................11
5.1.2 Avoid multiple string concatenations.............................................................................11
Nortel confidential
1. INTRODUCTION
1.1. OBJECT
This document defines the coding rules at the base off all access Java developments.
2. RELATED DOCUMENTS
3. SECURITY RULES
3.1. EXCEPTIONS
- fill a comment in the catch and eventually print the stack trace
Nortel confidential
If an API does not allow an exception to raise and if it is not clear what processing to
do, it is necessary to ask the API modification, otherwise redesign the code to avoid
the exception to rise)
Wrong
catch (Throwable e){
Right
catch (Throwable e){
Wrong
catch (NumberFormatException e) {
return false;
catch (ModelException e) {
return false;
catch (Exception e) {
return false;
Right
catch (Exception e) {
return false
Tip: Use Null object pattern (object which do nothing but is not null)
Example: for a List getList() method, rather than returning a null list (in case of errors),
returns the Collections.EMPTY_LIST instance.
Nortel confidential
Wrong
Lp_LogicalProcessor lp;
try {
catch (NullPointerException e) {
lp = node.createLp(slot);
}
Right
4. READABILITY
doSomething(mo);
...
}
Nortel confidential
…
}
In this code:
PARAM is public because is a constant used somewhere else (in tests for example)
MyMap is private because is a class member data and the others class must not have
access.
doSomething is private because it is an implementation function which must not be
used somewhere else.
By default, all declarations must be made private and changed to protected and after
to public only if good reason is identified.
Basically member class variables are made private and modifications are done via
accessors.
if (a != null) {
MyObject b = a.computeB();
if (b != null) {
b.doSomething();
MyObject c = b.findC();
if (c != null) {
// ...
}
}
}
Right
return;
}
MyObject b = a.computeB();
if (b == null) {
Nortel confidential
return;
}
b.doSomething();
MyObject c = b.findC();
if (c == null) {
return;
}
// ...
Wrong
getChildrenOfType(NodeBConstants.TYPE_ID);
getChildrenOfType(FDDCellConstants.TYPE_ID);
while (!found && fddCells.hasNext()) {
target = (ManagedObject)fddCells.next();
found = target.getKeyValue().toString().
equals(fddCellIdTarget);
}
return target;
Right
nodeBs.hasNext();) {
Nortel confidential
fddCells.hasNext();) {
equals(fddCellIdTarget)) {
return target;
}
}
return null;
int[] slots = new int[16] : use the constant PpDefs.NB_SLOTS. (“Once and
Only Once”
4.1.7 ITERATORS
Iterations must be implemented using for:
iterator.hasNext();) {
MyObj o = (MyObj) iterator.next();
// ...
Nortel confidential
TheClassNamesAreCapitalizedAndBeginsWithACapital
theMethodNamesAndVariablesAreCapitalizedAndBeginsWithALowerCase
Use the JavaBeans conventions for the accessors: toto.getThings() rather than
toto.things().
For method names, use the imperative form: “processSomething” rather than
“something“.
…
atmifCAMaxVccsValue = xxx;
Right
Nortel confidential
5. PERFORMANCE
Wrong
public class ATSWL {
}
return var;
}
Tip: Use StringBuffer class instead of String
Right
return var.toString();
}
Wrong
Nortel confidential
htmlOut = "<html>";
...
htmlOut += "<body>";
...
htmlOut += "</body></html>";
return htmlOut;
}
Right
htmlOut.append("<html>");
...
htmlOut.append("<body>");
...
htmlOut.append("</body></html>");
return htmlOut.toString();
}
6.1. ABBREVIATIONS
6.2. DEFINITIONS
END OF DOCUMENT
Nortel confidential