Lab 05: Style Guides and You

Estimated reading time: 10 minutes
Estimated time to complete: 30–45 minutes
Prerequisites: Lab 03
Collaboration: permitted
Starter code: checkstyle-lab.zip (Do not import into Eclipse until you’ve installed the style checker as described in this lab.)

I’ve mentioned in class and on Piazza that many organizations impose a “style guide” on projects. There’s a variety of reasons for this requirement. Some style guidelines help prevent errors (for example, requiring curly braces, even when they’re optional, like for the body of an if statement). Some guidelines improve readability, like requiring spaces around operators inside of conditionals and the like. And some just impose aesthetic consistency across files, which is helpful when reading others’ code.

This last point cannot be overlooked: most programmers spend most of their time reading code, and often that code was written by someone else. Someday, you’ll have the delightful experience of reading a particularly awful method, and get grumpy about which terrible programmer wrote it. No comments! One letter variable names! Inconsistent indenting! What were they thinking?!?! And then realize that it was you that wrote it, only a few weeks ago.

To help get you in the habit of writing neatly formatted formatted code, we’re going to start using a style checker. “But Marc, I am an expert coder! Je suis une artiste of zee bits and zee bytes! Surely my aesthetic choices are excellent!” Perhaps. But you’re also in a 100-level COMPSCI class, so you’ll have to trust my judgment here that on average, conforming to a mandated style is going to improve the code quality of students in this course.

Future programming assignments may require you to submit code that conforms with a provided style; this requirement will be obvious, as the style checker will be enabled in the starter project you download.

Overview

In this lab, you’ll install a style checker for Eclipse. Then you’ll download a class that doesn’t pass the style checker, and modify it so that it does.

Goals

  • Install the ecipse-cs Checkstyle plugin.
  • Use this plugin to find style problems in a provided class file.
  • Fix these problems.

Installing the Eclipse Checkstyle Plugin (aka eclipse-cs)

We’ll use a fairly standard style checker called Checkstyle by way of an Eclipse plugin that bundles it.

Open the “Eclipse Marketplace” (under the “Help”) menu, and search for “checkstyle”. You are looking for the latest “Checkstyle Plug-in”, which at the time of this writing was version 6.19.1. You should see something like the following after searching:

checkstyle in the marketplace

Click the “Install” button next to the “Checkstyle Plug-in 6.19.1” plugin, and accept the terms of the license agreement (which should not affect your behavior unless you plan to edit and then distributed the plugin itself, which you likely do not). It is ok to “install software that contains unsigned content”; when prompted about this, press “OK”, and restart Eclipse when prompted.

Fixing style problems

Now that you have the Checkstyle plugin installed, download and import the starter code for this lab. You’ll see a new kind of warning in Eclipse’s problem list, the “Checkstyle problem”, and you’ll see that some of the code is now highlighted in yellow. Each such area indicates one (or more) style problems you’ll need to resolve.

eclipse with checkstyle enabled

For example, let’s look at the first line. Hover your mouse cursor over the magnifying glass on the left margin, or look in the list of problems at the bottom of the window:

  • ”‘{‘ is followed by whitespace.” To resolve this problem, remove the unnecessary space between { and null.
  • “‘static’ modifier out of order with the JLS suggestions.” To resolve this problem, reorder the variable modifiers to be in the correct order (static should precede final).

Most of the rest of the messages should be straightforward; the only other that seems obtuse to me is:

  • “‘method def’ child have incorrect indentation level 8, expected level should be 16.” As you might be able to tell, this has to do with indentation; in particular, it’s saying that the “child” (that is, the part beneath/below) a “method def”inition has incorrect indentation. Indent the method body appropriately to resolve this error.

Fix all of the style problems in this Eclipse project. When renaming variables or methods, consider making use of Eclipse’s “refactor” functionality. Right-click an identifier and select “Refactor → Rename…” to have Eclipse automatically rename all of the same uses of this identifier. Unlike a generic search-and-replace, this approach respects the syntax and scoping rules of Java, only renaming identifiers that are the same, rather than all occurrences of the string in your program.

Note that you’ll have to save the file for the style checker to evaluate the changes you’ve made.

What to submit

When you’re done, take a screenshot of your Eclipse window with both the fixed main method and the now-empty “Problem” list visible. Submit this screenshot on Gradescope.