CS 149: Introduction to Programming
James Madison University, Fall 2018 Semester

Lab04: Style guide and Checkstyle

Code Quality
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:

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 accidental assignment.


checkstyle.sourceforge.net

Running Checkstyle from the Command Line

Download these two files into the same directory as your code:

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. The following example refers to a method beginning on line 93, char 5 of Hello.java:

Hello.java:93:5: Missing a Javadoc comment

Running Checkstyle Through jGRASP

After you have downloaded the Checkstyle .jar file and the .xml configuration file, open jGRASP and select:
Tools -> Checkstyle -> Configure.

Configure screenshot

In the "Checkstyle Tool Settings" dialog box set "Checkstyle Home" to

Checkstyle-home
be the folder containing the .jar file(probably /usr/home/stu/stu-f/eid/cs149/lab04), and "Checks File" to be the cs149.xml file, then click "OK".  TIP: You can use the HOME button when hitting the Browse button on the checkstyle dialog to go to your users home folder in Linux as shown below

Home Button

If all went well, you should see two new buttons on the jGRASP toolbar: Checkbutton and Check folder image. Clicking the first button will run Checkstyle on the current file in the editor window. Clicking the second button will run Checkstyle on all of the Java files in the current directory.

Also, You may want to setup jGRASP to indent properly and handle tabs well. In the jGRASP menu select:
Settings->CSD Window settings -> Workspace.  Select the Soft Tabs checkbox then set tab size to 4.

If you apply Checkstyle to your source code often, you will likely internalize good style habits over time. Note however that Checkstyle is unable to 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 Lab04 folder for today's lab. See the beginning of Lab01 for more details.

Note: For Fall 2018 - use your own Bakery.java file rather than the Payroll.java file indicated here. Your Lab04 grade will be the best style grade you have for PA1 in AutoLab.

A. Comments

  1. Every class must contain a Javadoc comment with the following three elements.
  2. /**
      * Overall description of the class goes here.
      *
      * @author Your name goes here
      * @version Due date goes here
      */
    
  3. All methods (including main) must contain an applicable Javadoc comment.
  4. /**
     * 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
     */
    
  5. In-line comments (//) should describe major structures and steps within a method.
  6. All comments should use normal English spelling and grammar. Phrases are okay.
  7. Comments must come before the code that they are describing or on the same line.

B. Names

  1. All names should be descriptive and readable. (subTotal rather than sgrade rather than grd)
  2. Multiple-word names should use capital letters to separate words. (subTotal, not sub_total)
  3. Variable and method names should begin with a lowercase letter, and:
    • Variable names should be nouns or noun phrases. (studentName or subTotal)
    • Method names should be verbs or verb phrases. (printLine or addColumn)
  4. Class names should begin with a capital letter and use title case. (HelloWorld)
  5. Constant names should be all caps with an underscore separator. (PI or INTEREST_RATE)

C. Declarations

  1. All constants should be named and initialized at the top of the method in which they are used.
  2. final double CENTIMETERS_PER_INCH = 2.54;
    centimeters = inches * CENTIMETERS_PER_INCH;    // NOT inches * 2.54;
    
  3. All variables should also be declared at the top of the method, directly after any constant declarations.
  4. It is strongly recommended (in CS 149) to separate variable declaration and initialization statements.
  5. Scanner input = new Scanner(System.in);    // discouraged
    
    Scanner input;
    input = new Scanner(System.in);
    
  6. Each declaration should be on its own line. Comment to the right if the name is not self-explanatory.

D. Literals

  1. Numeric literals should be of the correct type for the context in which they are used.
  2. // 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

  1. Subsections of code should be indented consistently with four spaces.
  2. Always use four space characters, not tab characters, for indentation.
  3. Statements too long for one line should be indented on subsequent lines.
  4. All blocks of code (even if one line) should be surrounded by curly braces.
  5. Left braces must appear at the end of the same line as the structure header.

F. Whitespace

  1. There should be a space after cast operators, commas, and //'s.
  2. Use whitespace to separate logical segments of code. There should be a blank line after variable declarations.
  3. Lines should be kept to a short length (< 80 chars). You should be able to see the full line in your text editor.
  4. Binary operators should be separated from their operands by a single space. (sum = myGrade + yourGrade;)
  5. The dot (.) operator, on the other hand, should not have any space surrounding it. (System.out.println();)
  6. Unary operators should also not be separated by a space. (myGrade++;)