- The
Navy Java Style Guide
- is essentially intented for the students of the N.P.S. School (some recommandations are not exportable outside the school). It is available on our site as a revisited mirror
(original author's agreement pending) of
http://vislab-www.nps.navy.mil/~java/course/styleguide.html (Last Updated 8 JUL96)
by the
Naval Postgraduate School
(
java@nps.navy.mil(JAVAPage)).
The Java style guide is mirrored from the NPS CS C++ style guide used and developed by John Falby.
Unfortunately, some features available only in C++ (enum, typedef, cout, etc) were NOT removed! So, handle this text with care!!!
General - section of the
Java Style Guide
of the
NPS
Visual Layout - section of the
Java Style Guide
of the
NPS
You should format your program making use of page boundaries and blank lines to separate code groups, functions and groups of logically related blocks. As a general guideline, no method or sub-class should be longer than one page, and formatting should ensure that it prints out on one page. There are exceptions, such as breaking a method up to fit a page may be poor design and worse than writing one that is too long.
(quoted from Visual Layout - section of the
Java Style Guide
of the
NPS)
Output from a program should be both informative and professionaly formatted. A person reading the output should be able to determine for himself whether he believes that the program works reasonably, without knowing the internal details of the program.
(quoted from Visual Layout - section of the
Java Style Guide
of the
NPS)
A printed page should be no more than 72 columns and 60 lines (12 point font).
(quoted from
Visual Layout - section of the
Java Style Guide
of the
NPS)
Indentation and Whitespace - section of the
Java Style Guide
of the
NPS
Indentation is used to highlight the block structure of the code and must therefore be used consistently. The proper use of white space and indentation makes the structure of the program evident from the layout of the code. You must use proper indentation, which in this class is three spaces. We strongly recommend the use of spaces vice tabs. Printers and other users might have different tab spacing.
(quoted from
Indentation and Whitespace - section of the
Java Style Guide
of the
NPS)
Vertical whitespace (i.e. blank lines) helps set off blocks of code and can increase readability of the code. Code with no vertical whitespace becomes very busy and hard to follow.
(quoted from
Indentation and Whitespace - section of the
Java Style Guide
of the
NPS)
A method of class name and its associated braces are aligned in the left most column, and all other code is indented from there. The closing brace will always be on a line by itself and aligned with the beginning of the block opening brace. Braces used to block code will be indented consistently with the rest of the code.
(quoted from
Indentation and Whitespace
- section of the
Java Style Guide
of the
NPS)
Comments will be indented consistently with the block of code they describe.
(quoted from
Indentation and Whitespace - section of the
Java Style Guide
of the
NPS)
One way to improve the readability of a method is to paragraph it, or in other words indent your code
within the scope of a code block. Any code within braces, the { and } characters, forms a block. The basic
idea is that the code of within a block should be uniformly indented one unit 1 . To ensure consistency,
start your method and class declarations in column 1 (NPS , 1996).
(see the NPS'
Navy Java Style Guide)
(quoted from
2.4 TECHNIQUES FOR WRITING CLEAN CODE 2.4.2 Paragraph Your Code in AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 10
)
Use Whitespace in Your Code
A few blank lines, called whitespace, added to your Java code can help to make it much more readable by
breaking it up into small, easy-to-digest sections (NPS , 1996; Ambler, 1997).
(see the NPS'
Navy Java Style Guide)
The Vision 2000 team
(1996) (see The NASA
Coding Standards for C, C++, and Java, Vision 2000) suggests using a single blank line to separate logical groups of code, such as control structures,
with two blank lines to separate method definitions. Without whitespace is very difficult to read and to
understand.
(quoted from 2.4 TECHNIQUES FOR WRITING CLEAN CODE 2.4.3 Use Whitespace in Your Code in AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 11
)
Control Structures
- section of the
Java Style Guide
of the
NPS
The use of braces is required for all control structures.
quoted from Control Structures
- section of the
Java Style Guide
of the
NPS)
Avoid the use of continue and break in loops.
quoted from Control Structures
- section of the
Java Style Guide
of the
NPS)
Always use a default case in switch statements. Use a break to end all cases (including default) in a switch statement.
quoted from Control Structures
- section of the
Java Style Guide
of the
NPS)
The use of //end if, //end while, and //end switch comments after the corresponding ending right brace is optional but highly recommended.
quoted from Control Structures
- section of the
Java Style Guide
of the
NPS)
Naming - section of the
Java Style Guide
of the
NPS
Variables will have meaningful names/identifiers. This must be balanced against using names which are too long, which can obscure the code. (A variable name greater than around fifteen characters is approaching the "too long" limit.) The only exception to this is for loop iteration counters. These special case variables can have names like "ix", "jx", etc. Single letter variables or constants should not be used. The exception is when it is a common practice to identify something with a single letter. An example of this is the coordinate axis (X, Y, and Z).
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
An identifier consisting of multiple names shall have each name set off. This is done by making the first letter of each name part capital (e.g. redCarColor), or by using underscores between parts (e.g. red_car_color), but not both (e.g. red_Car_Color).
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Do not use the letter `l' (lower case `L'), `o', or `O' in names unless they are part of normal words. This is to avoid confusion with the numbers 1 and 0.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
All normal identifier names must be in lower or mixed case. (method, typedef, variable names, class, struct, union, and enum tag names)
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Enum elements must be in all CAPS, but instantiations can be in lowercase.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Names with a leading or trailing underscore are usually reserved for system purposes, and may not be used for any user-created names with the exception of preprocessor defines.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Avoid names that differ only in case, look similar, or differ only slightly.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Avoid redefining names of standard identifiers (e.g cout).
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Names of methods with return type of void should reflect what they do (printArray).
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Names of methods which return something should reflect what they return (initialize).
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
All global variables will start with "G_". Globals used only in a single class will be defined in that class, be declared as static, and start with "L_". const names are not considered global variables.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Define TRUE, FALSE and NULL, and use them by name as opposed to using 1 and 0 explicitly.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Avoid using absolute path addressing when referring to files. Use relative addressing based upon the location of the executable.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Specify only the file names (i.e. without paths) for includes and libraries.
quoted from Naming
- section of the
Java Style Guide
of the
NPS)
Avoid long names (< 15 characters is a good idea). Although the class name
PhysicalOrVirtualProductOrService might seem to be a good class name at the time (OK, I'm
stretching it on this example) this name is simply too long and you should consider renaming it to
something shorter, perhaps something like Offering (NPS , 1996).
(see the NPS'
Navy Java Style Guide)
(quoted from 1.3 WHAT MAKES UP A GOOD NAME in AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 1)
Avoid names that are similar or differ only in case. For example, the variable names
persistentObject and persistentObjects should not be used together, nor should anSqlDatabase and
anSQLDatabase (NPS , 1996).
(see the NPS'
Navy Java Style Guide)
(quoted from 1.3 WHAT MAKES UP A GOOD NAME in AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 1)
Avoid leading or trailing underscores. Names with leading or trailing underscores are usually
reserved for system purposes, and may not be used for any user-created names except for pre-processor
defines (NPS , 1996).
(see the NPS'
Navy Java Style Guide)
More importantly, underscores are annoying and difficult to type so I
try to avoid their use whenever possible.
(quoted from 1.3 WHAT MAKES UP A GOOD NAME in AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 1)
Naming Constants
In Java, constants, values that do not change, at typically implemented as static final attributes of classes.
The recognized convention is to use full English words, all in uppercase, with underscores between the
words (Gosling, Joy, Steele, 1996; Sandvik, 1996; NPS , 1996).
(see the
Java Coding Style Guidelines by Kent Sandvik)
(see the NPS'
Navy Java Style Guide)
Examples:
MINIMUM_BALANCE
MAX_VALUE
DEFAULT_START_DATE
The main advantage of this convention is that it helps to distinguish constants from variables.
(quoted from 3.1 NAMING ATTRIBUTES 3.1.2 Naming Constants in AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 13
)
Classes - section of the
Java Style Guide
of the
NPS
One way to make your classes easier to understand is to declare them in a consistent manner. The
common approach in Java is to declare a class in the following order:
(NPS , 1996)
(see the NPS'
Navy Java Style Guide)
- public methods
- public attributes
- protected methods
- protected attributes
- private methods
- private attributes
Laffra ( in
Advanced Java: Idioms, Pitfalls, Styles, and Programming Tips by
Chris Laffra - PART I, Idioms and Programming Tips)
points out that constructors and finalize() should be listed first, presumably because these
are the first methods that another developer will look at first to understand how to use the class.
(quoted from 6.1 STANDARDS FOR CLASSES 6.1.3 Class Declarations in AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 27
)
Methods - section of the
Java Style Guide
of the
NPS
The main() method shall be declared as the last method in the class, ie:
quoted from Methods
- section of the
Java Style Guide
of the
NPS)
Avoid multiple return statements in the same method.
quoted from Methods
- section of the
Java Style Guide
of the
NPS)
Test the return values of methods. It is not uncommon for a method to fail and an error manifest itself much later in the code.
quoted from Methods
- section of the
Java Style Guide
of the
NPS)
Each method (starting with the method comment) must be separated from other text/code by at least two blank lines.
quoted from Methods
- section of the
Java Style Guide
of the
NPS)
Globals - section of the
Java Style Guide
of the
NPS
For the most part, globals are bad and do not use them. However, there are a certain cases when globals might be used. All use of globals must be completely documented and justified.
quoted from Globals
- section of the
Java Style Guide
of the
NPS)
Files - section of the
Java Style Guide
of the
NPS
All Java source files will have a "java" extension and the prefix before
the extension will be the same name as the class with in the file.
quoted from Files
- section of the
Java Style Guide
of the
NPS)
Comments - section of the
Java Style Guide
of the
NPS
Comments can be divided into two general categories, strategic and tactical. Strategic comments are used to give the reader a general overview of what is going on. These comments appear at the top of the files and before the function code. Strategic comments tend to be written in a prose style. Tactical comments are designed to explain tricky areas of code, what parameters do, and hints about the control flow of the program. These comments are imbedded in the code and tend to be shorter and in bullet format. The javadoc will be used to develop documentation files.
quoted from Comments
- section of the
Java Style Guide
of the
NPS)
If you use proper naming, block structure, indentation, and straight forward programming, your code will be mostly self documenting. This is what you should strive for. Keep comments simple. Do not attempt to explain the obvious. Do not use banner-like comments. Avoid decoration.
quoted from Comments
- section of the
Java Style Guide
of the
NPS)
Use the Java (C++) "/*"......"*/"construct for all comments. This will allow you to block out sections of code with the old comments.
quoted from Comments
- section of the
Java Style Guide
of the
NPS)
Implementation comments should be intermixed within the code. Usage comments will precede the code, normally in the file or function comment.
quoted from Comments
- section of the
Java Style Guide
of the
NPS)
Each class and method declaration must have a description in comments preceding it. The declaration will contain a comment pertaining to usage, and the definition should contain comments relating to the implementation.
quoted from Comments
- section of the
Java Style Guide
of the
NPS)
Avoid decoration, i.e. don't use banner-like comments. In the 1960s and 1970s COBOL
programmers got into the habit of drawing boxes, typically with asterisks, around their internal
comments (NPS , 1996).
(see the NPS'
Navy Java Style Guide)
Sure, it gave them an outlet for their artistic urges, but frankly it was a
major waste of time that added little value to the end product. You want to write clean code, not
pretty code. Furthermore, because many of the fonts used to display and print your code are
proportional, and many aren't, you can't line up your boxes properly anyway.
(quoted from AmbySoft Inc.
Java Coding Standards by
Scott W. Ambler, v. July 10, 1997, p. 3)
Miscellaneous - section of the
Java Style Guide
of the
NPS
Always use the most appropriate operator or construct.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
Always use the principle of least privilege (grant only that access necessary to perform the task and no more).
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
All long int literals must have the `L' suffix (e.g. 40000L).
Numerical constants ("magic numbers") must not be coded directly. The only allowable exceptions are for 0, 1 or -1, and those which are not "likely" to change.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
Numerical constants must have a comment explaining the value if it is not evident by the name.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
Enumeration types must be used to declare constants that have a discrete set of values. A comment must be provided indicating what the start value is.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
You must test floating point numbers with <= or >=. Never use exact comparisons with floating point numbers, even when comparing to 0.0f. The appropriate means of comparison is to define a value that represents the maximum difference two values may differ by and be considered equal.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
You should test your code. It should produce the correct results for normal test conditions as well as boundary conditions.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
Good design and clarity take precedence over optimization. (To a point)
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
Do not declare or use more variables than are necessary.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
No line of code should exceed 72 characters (including blanks).
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)
If you open a file for reading/writing, close the file as soon as you are finished reading from/writing to it.
quoted from Miscellaneous
- section of the
Java Style Guide
of the
NPS)