This Style Guide should be used as a reference for all work turned in for credit, both labs and programming assignments. It is based on (but not identical to) standard java coding guidelines including the Sun Java Coding Conventions and the Code Style Guidelines for Android.
All names should be descriptive and readable:
subTotal
rather than s
;
grade
rather than grd
. Multiple
word names should use a capital letter to separate words
(e.g. subTotal
). Variable names should balance
clarity with brevity. The name person
is better than
currentPersonObject
. However, per
is worse than either
(percentage? person? permitted?).
subTotal
studentName
or subTotal
)printLine
or addColumn
)HelloWorld
).
PI
, INTEREST_RATE
).Where and when to declare variables.
for
loop may be declared within
the body of the loop (inside of the for
loop
header).Indentation provides a visual structure for your program.
sum = myGrade + yourGrade;
myGrade++;
System.out.println();
pay = (hours * 1.5) + bonus;
Structure provides readability.
The point of comments is to clarify code that might otherwise be difficult for the reader to follow. Well organized Java code with informative variable names should require few comments inside of method bodies.
Every class must include a Javadoc comment like the following.
/** * Overall description of the class goes here * * @author Your name goes here * @version Vn -date (date may be in MM/DD/YY or MM/YY format) */
All methods must contain a Javadoc comment preceding the
method header as shown in the example below. The
description should describe the "black box" behavior of the
method (what it does). If there are parameters, then
there must be one @param
tag for each parameter
in the format shown below. If there is a return value,
there must be one @return
tag which describes the
return value (not the variable name). Finally, if the
method throws one or more exceptions, @throws
tags must be used to describe the circumstances under which
each exception will be thrown.
/** * Overall description (black box behavior) of the method goes here. * * @param parameterName Describe each input parameter. You must have one * @param line for each parameter * @return Describe the value that this method returns. * @throws ExceptionName Describe under what conditions the exception may * be thrown */
Every programming assignment must contain the following section which follows the class description or must cite any sources used (such as a TA). You should either include this statement in all files or minimally it may appear in the file containing your main method.
/* * References and Acknowledgments: I received no outside help with this * programming assignment. */
/* * References and Acknowledgments: TA Glenn helped me with the * foo method. */This acknowledgment is not necessary for lab assignments. Note the single star after the slash; this is not a Javadoc comment.
Math.PI
rather
than 3.14159
)else
if it is not needed.break
and continue
except in switch statements. private
unless there is a documented
reason for not doing so.public
if they are
provided as a service to outside classes (such
as Math.PI
).import utilities.GoodClass;
instead of import utilities.*;
)