www.AmbySoft.com/javaCodingStandards.pdf
Copyright 1998-2000 AmbySoft Inc.
i
Table of Contents
1. GENERAL CONCEPTS.........................................................................................................................................1
1.1 WHY CODING STANDARDS ARE IMPORTANT...........................................................................................1
1.2 THE PRIME DIRECTIVE..................................................................................................................................1
1.3 WHAT MAKES UP A GOOD NAME ..............................................................................................................2
1.4 GOOD DOCUMENTATION..............................................................................................................................3
1.4.1 The Three Types of Java Comments.......................................................................................................4
1.4.2 A Quick Overview of javadoc..................................................................................................................5
1.5 AMBLER’S LAW OF STANDARDS.................................................................................................................6
2. STANDARDS FOR MEMBER FUNCTIONS....................................................................................................7
2.1 NAMING MEMBER FUNCTIONS....................................................................................................................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 MEMBER FUNCTION VISIBILITY.................................................................................................................9
2.3 DOCUMENTING MEMBER FUNCTIONS.......................................................................................................9
2.3.1 The Member Function Header................................................................................................................9
2.3.2 Internal Documentation.........................................................................................................................11
2.4 TECHNIQUES FOR WRITING CLEAN CODE...............................................................................................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 NAMING FIELDS............................................................................................................................................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 DOCUMENTING A FIELD .............................................................................................................................21
3.4 THE USE OF ACCESSOR MEMBER FUNCTIONS........................................................................................21
3.4.1 Naming Accessors...................................................................................................................................22
3.4.2 Advanced Techniques for Accessors....................................................................................................23
3.4.2.1 Lazy Initialization............................................................................................................................. 23
评论0