13 Quality

Oracle Academy Section

  • None


Textbook Chapters

  • Appendix F: Program Style, Formatting, and Documentation

Read the book using the SQ3R technique.

Java Foundations Certification Exam Topics

  • The content of this module is not on the certification exam.

Most Important Concepts / Code

SpotBugs

  • SpotBugs in Eclipse

  • To install the SpotBugs plugin:

    • Help -> Eclipse Marketplace -> search for SpotBugs

  • To use SpotBugs Bugs:

    • Window -> Preferences -> Java -> SpotBugs -> slide "Minimum rank to report" all the way to the right (20)

    • Right click on a Java project in Package Explorer -> Spot Bugs -> Find Bugs

    • Problem markers will be displayed in source windows and in Window -> Show View -> Other -> SpotBugs -> Bug Explorer

      • If no bugs are being found, first make sure you have a bug by adding the following into a method: System.out.println("bug".substring(0)); then save the file and do Find Bugs

      • If that line doesn't show up as a bug, uninstall and reinstall SpotBugs using Help -> Installation Details

      • Scanner scan = new Scanner(System.in, "UTF-8");

Javadocs

  1. Create / format documentation

    1. In Eclipse: Source -> Generate Element Comment (Alt + Shift + J) when your cursor is on the line for every class, field, constructor, and method declaration

    2. Description

      1. A required component of every doc.

      2. The first sentence (end in a period). It should be a summary sentence, concise but complete.

      3. Optionally, include an additional <p> tag and a longer description.

    3. Block tags:

      1. At a minimum, include the following:

        1. For a class

          1. @author - your name

        2. For a method

          1. @param - a description of the parameter variable, if there is one

          2. @return - a description of the returned value, if the method returns something

    4. More comprehensive information: How to Write Doc Comments

  2. Create and access Javadoc file

    1. Project -> Generate Javadoc

      1. javadoc command is the path to your javadoc.exe. Probably in C:\Program Files\Java\jdk1.n.n_n\bin

    2. Find generated web page at workspace/projectname/doc/index.html

Sample doc comments in a class

/**

* Represents a student enrolled in the school.

* A student can be enrolled in many courses.

* @author Your Name

*/

public class Student {


/**

* The first and last name of this student.

*/

private String name;

/**

* Gets the first and last name of this Student.

* @return this Student's name.

*/

public String getName() {

return name;

}


/**

* Changes the name of this Student.

* This may involve a lengthy legal process.

* @param newName This Student's new name.

* Should include both first

* and last name.

*/

public void setName(String newName) {

name = newName;

}

}

Checkstyle

  • To install the Checkstyle plugin: Checkstyle Installation

  • To use: right click on the project in the Package Explorer, find Checkstyle, then Check code with Checkstyle

  • Style errors will appear in the Problems tab. Eclipse: Window -> Show View -> Problems

  • Tips:

    • After each change to your code, save the file and go through the process to check the style again

    • If you don't see Checkstyle when you right click your project, update Eclipse.

    • See Style focus for Eclipse tips related to indentation.

    • You may need to highlight a tab character, and do Edit -> Find/Replace to replace with 2 spaces.

Hour 1

Integration Project

  1. Adhere to Google Java Style (at least one substantial class with no CheckStyle warnings)

  2. Demonstrate best practices for documentation (your name in each file, comments to explain any non-obvious code, cite sources, at least one substantial class with a doc comment for the class and all fields, constructors, and methods)