James Madison University, Fall 2016 Semester
Lab03: Style guide and Checkstyle
Source: https://xkcd.com/1513/
Background
Virtually every organization that does software development has strict guidelines on how to format source code. Here are two examples of "industrial strength" Java style guides:
Code Conventions (April 1999)
Google Java Style (March 2014)
This course will more or less follow a simplified version of these guidelines. Rather than memorize a long list of rules, you should develop good habits and intuition when it comes to style.
Checkstyle is a tool that can be used to determine if your source code follows a set of style rules. It also checks for common programming mistakes, such as empty statements and shadow variables.
Running Checkstyle
Download these two files into the same directory as your code:
checkstyle-7.0-all.jar - command line tool
cs149.xml - configuration file for CS 149
Open a terminal and use the following command to run Checkstyle:
java -jar checkstyle-*-all.jar -c cs149.xml *.java
Note the * characters are wildcards that match whatever version of Checkstyle you have and whatever java source files are present. The output indicates the file and line number of each problem. This example refers to a method beginning on line 93, column 5 of Hello.java:
Hello.java:93:5: Missing a Javadoc comment
If you apply Checkstyle to your source code regularly, you will likely internalize good style habits over time. Note however that Checkstyle does NOT evaluate the quality of comments, meaning of variable names, and placement of declarations.
Example Program
If you haven't already, you should create a top-level CS149
folder to organize all your labs and other assignments for the semester. Then create a Lab03
folder for today's lab. (If you logged in as student
today, please log out and log back in with your own account.)
Download: Payroll.java
Submission: At the end of class today, or by 11:00 PM if you would like more time, submit a corrected version of Payroll.java via Canvas. Your code must pass Checkstyle without any warnings AND meet all the requirements outlined below. (It must also compile and run correctly; don't change the program's behavior.)
Collaboration: You are encouraged to work with another student to complete this lab. Each of you should submit your own copy of the program. It's okay if your files are similar or identical, as long as both of your names are present at the top.
A. Comments
- Every class must contain a Javadoc comment with the following three elements.
- All methods (including
main
) must contain an applicable Javadoc comment. - In-line comments (
//
) should describe major structures and steps within a method. - All comments should use normal English spelling and grammar. Phrases are okay.
- Comments must come before the code that they are describing or on the same line.
/** * Overall description of the class goes here. * * @author Your name goes here * @version Due date goes here */
/** * Overall description of the method goes here. * * @param paramterName describe each input parameter * @param anotherParam use a separate line for each * @return describe the value that this method returns */
B. Names
- All names should be descriptive and readable.
(
subTotal
rather thans
,grade
rather thangrd
) - Multiple-word names should use capital letters to separate words.
(
subTotal
, notsub_total
) - Variable and method names should begin with a lowercase letter, and:
- Variable names should be nouns or noun phrases. (
studentName
orsubTotal
) - Method names should be verbs or verb phrases. (
printLine
oraddColumn
)
- Variable names should be nouns or noun phrases. (
- Class names should begin with a capital letter and use title case. (
HelloWorld
) - Constant names should be all caps with an underscore separator. (
PI
orINTEREST_RATE
)
C. Declarations
- All constants should be named and initialized at the top of the method in which they are used.
- All variables should also be declared at the top of the method, directly after any constant declaraions.
- It is strongly recommended (in CS 149) to separate variable declaration and initialization statements.
- Each declaration should be on its own line. Comment to the right if the name is not self-explanatory.
final double CENTIMETERS_PER_INCH = 2.54; centimeters = inches * CENTIMETERS_PER_INCH; // NOT inches * 2.54;
Scanner input = new Scanner(System.in); // discouraged Scanner input; input = new Scanner(System.in);
D. Literals
- Numeric literals should be of the correct type for the context in which they are used.
// integer expressions should use integer literals int a; double b; a = 2; b = 2.0; // double expressions should use double literals double x; double y; double average; average = (x + y) / 2.0; // NOT 2, which is an integer
E. Indentation
- Subsections of code should be indented consistently with four spaces.
- Always use four space characters, not tab characters, for indentation.
- Statements too long for one line should be indented on subsequent lines.
- All blocks of code (even if one line) should be surrounded by curly braces.
- Left braces must appear on the same line as the structure header.
F. Whitespace
- There should be a space after cast operators, commas, and //'s.
- Use whitespace to separate logical segments of code. There should be a blank line after variable declarations.
- Lines should be kept to a short length (< 80 chars). You should be able to see the full line in your text editor.
- Binary operators should be separated from their operands by a single space.
(
sum = myGrade + yourGrade;
) - One exception is the dot (
.
) operator, which should not have space surrounding it. (System.out.println();
) - Unary operators should not be separated by a space.
(
myGrade++;
)