/** * CS149 - Programming Fundamentals * Department of Computer Science * James Madison University * @version Fall 2019 */
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 (Apr 1999)
Google Java Style (Sep 2017)
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.
Download these two files into your home directory:
checkstyle-8.11-all.jar - command-line tool
CS149-Checkstyle.xml - configuration file for CS 149
Open a terminal and use the following command to run Checkstyle:
java -jar checkstyle-*-all.jar -c CS149-Checkstyle.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
After you have downloaded the Checkstyle .jar file and the .xml configuration file, open jGRASP and select:
Tools -> Checkstyle -> Configure.
In the "Checkstyle Tool Settings" dialog box set "Checkstyle Home" to
be the folder containing the .jar file(probably /usr/home/stu/stu-f/eid/cs149/lab04), and "Checks File" to be the CS149-Checkstyle.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
If all went well, you should see two new buttons on the jGRASP toolbar: and
. 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.
/** * Overall description of the class goes here. * * @author Your name goes here * @version Due date goes here */
main
) must contain an applicable Javadoc comment./** * 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 */
//
) should describe major structures and steps within a method.subTotal
rather than s
, grade
rather than grd
)subTotal
, not sub_total
)studentName
or subTotal
)printLine
or addColumn
)HelloWorld
)PI
or INTEREST_RATE
)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);
// 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
sum = myGrade + yourGrade;
).
) operator, on the other hand, should not have any space surrounding it.
(System.out.println();
)myGrade++;
)