www.AmbySoft.com/javaCodingStandards.pdf
Copyright 1998-2000 AmbySoft Inc.
i
Table of Contents
1. GENERAL CONCEPTS.........................................................................................................................................1
1.1 W
HY
C
ODING
S
TANDARDS ARE
I
MPORTANT
...........................................................................................1
1.2 T
HE
P
RIME
D
IRECTIVE
..................................................................................................................................1
1.3 W
HAT
M
AKES
U
P A
G
OOD
N
AME
..............................................................................................................2
1.4 G
OOD
D
OCUMENTATION
..............................................................................................................................3
1.4.1 The Three Types of Java Comments.......................................................................................................4
1.4.2 A Quick Overview of javadoc..................................................................................................................5
1.5 A
MBLER
’
S
L
AW OF
S
TANDARDS
.................................................................................................................6
2. STANDARDS FOR MEMBER FUNCTIONS....................................................................................................7
2.1 N
AMING
M
EMBER
F
UNCTIONS
....................................................................................................................7
2.1.1 Naming Accessor Member Functions....................................................................................................7
2.1.1.1 Getters................................................................................................................................................. 7
2.1.1.1.1 Alternative Naming Convention for Getters – Has and Can..................................................8
2.1.1.2 Setters ................................................................................................................................................. 8
2.1.1.3 Constructors ...................................................................................................................................... 8
2.2 M
EMBER
F
UNCTION
V
ISIBILITY
.................................................................................................................9
2.3 D
OCUMENTING
M
EMBER
F
UNCTIONS
.......................................................................................................9
2.3.1 The Member Function Header................................................................................................................9
2.3.2 Internal Documentation.........................................................................................................................11
2.4 T
ECHNIQUES FOR
W
RITING
C
LEAN
C
ODE
...............................................................................................12
2.4.1 Document Your Code..............................................................................................................................12
2.4.2 Paragraph/Indent Your Code...............................................................................................................13
2.4.3 Paragraph and Punctuate Multi-Line Statements............................................................................13
2.4.4 Use Whitespace in Your Code...............................................................................................................14
2.4.5 Follow The Thirty-Second Rule............................................................................................................14
2.4.6 Write Short, Single Command Lines....................................................................................................14
2.4.7 Specify the Order of Operations...........................................................................................................14
2.5 JAVA CODING TIPS.......................................................................................................................................15
2.5.1 Organize Your Code Sensibly...............................................................................................................15
2.5.2 Place Constants on the Left Side of Comparisons............................................................................15
3. STANDARDS FOR FIELDS (ATTRIBUTES/PROPERTIES)......................................................................16
3.1 N
AMING
F
IELDS
............................................................................................................................................16
3.1.1 Use a Full English Descriptor for Field Names.................................................................................16
3.1.1.1 Alternative – Hungarian Notation................................................................................................16
3.1.1.2 Alternative – Leading or Trailing Underscores .......................................................................... 17
3.1.2 Naming Components (Widgets)............................................................................................................17
3.1.2.1 Alternative for Naming Components – Hungarian Notation....................................................17
3.1.2.2 Alternative for Naming Components – Postfix-Hungarian Notation.......................................17
3.1.3 Naming Constants...................................................................................................................................18
3.1.4 Naming Collections................................................................................................................................19
3.1.4.1 Alternative for Naming Collections – The ‘Some’ Approach...................................................19
3.1.5 Do Not “Hide ” Names............................................................................................................................19
3.2 FIELD VISIBILITY .........................................................................................................................................20
3.3 D
OCUMENTING A
F
IELD
.............................................................................................................................21
3.4 T
HE
U
SE OF
A
CCESSOR
M
EMBER
F
UNCTIONS
........................................................................................21
3.4.1 Naming Accessors...................................................................................................................................22
3.4.2 Advanced Techniques for Accessors....................................................................................................23
3.4.2.1 Lazy Initialization............................................................................................................................. 23