Java is one of the world’s most popular and most taught programming languages. Most, if not all Computer Science students, will have had a course on Java sometime during their academic career, often even during their introductory courses. Next to using Java as a means to teach Introduction to Programming, Object Oriented Programming, Data Structures or Mobile Development, it is also important to teach good Java practices and code quality. Perhaps especially for such a frequently occurring programming language such as Java, a programming language that is pre-eminently used for larger software projects in which good team work, adherence to a strict style guide and good coding practices are crucial.
In this guide, I will cover ways to autograde Java code quality and eliminate common bad practices. Not only will I cover two very useful tools for this, Checkstyle and PMD, I will also discuss how you can design your own style guides to fit your courses’ learning objectives and how to customize the feedback messages generated by these tools to be more understandable and meaningful for your students.
Java Code Quality tests in CodeGrade
One of the types of tests offered in CodeGrade AutoTest is the Code Quality step, a way to provide instant feedback on the style, quality and structure of code. This step makes it really easy to set up default quality checks without any configuration, but allows for fully custom tools and style guides too. The feedback generated in this step is not only shown in the AutoTest tab, but also directly on the lines of code, making it very intuitive for students to use and understand their code quality feedback!

You can use any custom tool or linter in a Code Quality step, but also use one that is built-in. For Java, we offer Checkstyle and PMD. These two code analysis tools (or linters) are used for different purposes, but are both very powerful (and even more so when combined). After briefly reviewing both tools, I will show how you can use them in a basic Code Quality test, which we will further customize in the following chapters.
Checkstyle
Checkstyle is a static code analysis tool for Java that is built-in to CodeGrade. It is a traditional linter: it checks if the code style and presentation conform to a style guide, but it does not check or confirm code correctness or completeness.
Amongst other things, Checkstyle can detect:
- Use of naming conventions (variables, methods, etc.)
- Completeness of documentation (think Javadoc)
- Use of vertical and horizontal whitespace
- Conventions for parameters, headers and imports
While at first glance it may seem somewhat unnecessary to teach, it is very important for students to learn working with a style guide, as this is very common practice in their later career and in group projects. Furthermore, enforcing a style guide in beginner courses helps students develop with a solid code style following the conventions.
PMD
PMD (probably but unofficially standing for Programming Mistake Detector) is a cross-language source code analyzer that is built-in to CodeGrade. It not only works with Java, but also with JavaScript, XML and PL/SQL. In comparison to Checkstyle, PMD actually detects bad coding practices and potential bugs, but does not assess style.
Amongst other things, PMD can be used to detect:
- Inefficient code
- Bad programming habits
- Overcomplicated expressions that can be simplified
- Potential bugs
- Dead code
- Duplicate code (via the included Copy-Paste-Detector, which also works for Python, C-languages, Ruby, PHP and many other languages)
PMD is often a little bit more advanced than Checkstyle and very useful in more advanced courses where bad practices are harder to spot by eye. Furthermore, it has rules for many very specific bad practices, allowing you to make custom style guides specifically for an OOP course, a multithreading course or another advanced course. Moreover, PMD has an option to create totally custom rules, which is outside the scope of this guide, but can make it very powerful for any advanced Java course.

%20(800%20x%20525%20px)%20(800%20x%20525%20px)%20(11).png)
%20(800%20x%20525%20px)%20(800%20x%20525%20px)%20(10).png)
%20(800%20x%20525%20px)%20(800%20x%20525%20px)%20(9).png)