A mistake that many junior developers make is to think that they’re writing code for the computer to understand, but computers don’t understand anything. They process. And computers can process some amazing messes without a hiccup. No, the entity that has to understand the code is the poor programmer (often yourself, weeks or months later) who has to maintain the code.
One of the very easiest things that a programmer can do to help make his code more readable and easier to understand is to adhere to some standard best practices and a rigid format. But let’s be honest…. that’s really hard to do when you’re in a hurry (and we’re always in a hurry). Eclipse offers limited support for formatting, but it doesn’t go far enough. That’s where a couple of my favorite tools come in: Checkstyle and PMD. These tools will force you into submission and remind you to correct your bad habits. So, what are they, and how do they work?
PMD (slogan: Don’t shoot the messenger) and Checkstyle are two fantastic customizable tools that both offer Eclipse plugins to help out a developer who might otherwise, um, forget to keep things in check. Some definitions are in order. PMD is a tool that checks your code for possible bugs, overcomplicated methods, duplicate code – that kind of thing. Why it’s called PMD remains a bit of a mystery. Checkstyle makes sure that your code adheres to a particular format, via means of a configuration file.
I have to be honest here. The first time I was introduced to these plugins I wanted to throw them out within 10 minutes. I absolutely loathed them. How dare it tell me that my method was “too complex”? How annoying to be reminded to enter a space after that bracket! But over time I came to appreciate the consistency of my code. It was nice to have that not-so-subtle reminder that I ought to consider refactoring to make my method a bit cleaner, a bit shorter.
Having used these tools now for several months, I really miss them when using an IDE that doesn’t have them installed. I highly recommend them to every Java developer. They need a little customizing so that they enforce the rules you or your organization care about, but the payoff of having nice, clean, professional-looking code is well worth the initial pain of having a tool force you to squash all your bad habits. So install them, try them out. You’ll thank yourself when a few weeks later you have to understand what you wrote.