Copy
Keeping tabs on code quality - evolutions in code quality metrics

Introduction

Keeping tabs on code qualityFew would argue that code quality is not important. High code quality and good coding practices makes code more reliable and easier to maintain. Agreed coding standards can make code more readable and easier to understand. And measuring code coverage is a great way to identify untested code. In this edition of the Java Power Tools Newsletter, we will take a look at different sorts of tools related to code quality, how they have evolved, and where they can fit into today's development process.

In this article, we will be looking at different approaches and tools aimed at improving and sustaining high code quality and the respect of coding standards and best practices. These include:

  • Manual code reviews
  • Automated code quality metrics in the build process
  • Code quality metrics in the IDE
  • Code quality metrics on the Continuous Integration server
  • Cross-project code quality metrics
Manual code reviews - the hard way

Arguably the most effective way to review code is to get someone with a critical eye, a lot of experience, and a solid understanding of the problem domain, to take a friendly look at your code. Human code reviews are a great way to spread knowledge about the code base, to spot design and architecture flaws as well as bugs, to point out unclear or unreadable code, and to pick up on code that does not comply to local coding standards. Indeed, many significant problems can only be identified by a human eye.

Pair programming can be one variation of this. Another attempt at structuring this approach is the Formal Code Review. The main problem with formal, structured code reviews is that they are time-consuming and require considerable discipline and structure to undertake in a sustainable manner.

Tooling can help code reviews in two ways. Web-based code review tools such as gerrit (online code review for git) and Crucible (Atlassian's online code review tool), among others, can be used to make the code review process a little more lightweight and less formal.

The other way tools can help with code reviews is a little more subtle. Static analysis tools such as Checkstyle and PMD help spot issues related to coding standards, naming conventions, and many poor coding practices. If these issues can be eliminated before they get to the code review stage, human reviewers can focus on more significant problems without being distracted by more basic issues such as poor coding layout or inconsistent variable names.

Snapshot views - Measuring code quality in your build

Code quality analysis tools have been around for quite a while. Static analysis tools like Checkstyle and PMD help enforce coding standards (which play an essential role in keeping your code readable and clear) and best practices. FindBugs tries to spot potential coding errors or bad practices. And code coverage tools like Cobertura and Clover help isolate untested code.

These tools were around before Maven, but Maven was the first build tool to make it trivial to integrate them into your automated build process. Indeed, Maven plugins for all of these tools really give you no excuse not to use them.

Maven also introduced the Maven Site Plugin, which was revolutionary in it's day. The Maven Site Plugin allows you to generate a host of code quality metrics reports in a central place, with very little configuration. It is frequently used in open source projects as a one-stop-shop for technical information about a project. The various code quality plugins for the Maven site make it easy to report on overall code quality or code coverage metrics, and also to drill down into the code level to see details of the actual issues.

A code coverage report in the Maven Site

The Maven site plugin effectively generates a snapshot view of code quality metrics for your project at a given point in time. However, as we will see, better tools exist now that build on Maven's powerful code quality reporting features, and provide additional capabilities.

Instant Gratification - code quality in your IDE

Reporting on code quality issues on a project-wide level is great from a team perspective - it sends a strong message that code quality is important, and helps ensure that people are taking code quality seriously. Project-level reports can also be used very effectively in code review sessions.

However, for a developer fixing code quality issues, HTML reports are inefficient. Developers need instant feedback when their code violates coding conventions or best practices. Fortunately, plugins for Checkstyle, PMD, Findbugs and so forth exist for Eclipse and other popular IDEs. Modern IDEs nowadays also have their own build-in code inspection features. These features are all very useful. The trick, however, is to synchronize the coding standards that are reported on across the project with those reported to developers in their IDE.

Time machine - code quality metrics over time

Another limitation of the Maven Site Plugin as a tool for reporting on code quality metrics is that it is too passive. If you are serious about code quality metrics, you should tell people about it. If a coding standard is worth having, it is worth enforcing. If you are serious about good coding practices, you should not let violations of these practices pass without comment.

Continuous Integration servers such as Hudson, TeamCity and Bamboo all have plugins that can report on the code quality metrics data generated by Maven. Create a special build job dedicated to code quality metrics. This lets you not only report on code quality metrics, but actively notify developers if standards are not satisfied. The Hudson violations plugin and Hudson Cobertura plugin, for example, both let you define acceptable thresholds for your code quality metrics, and can be configured to break the build if these thresholds are not met. In fact, this is an excellent strategy. Tools like Checkstyle are highly configurable, and it is more effective to keep the number of issues down to zero, with a fine-tuned rule set, than to keep track of new issues among hundreds of existing ones.

Placing code quality reports on your CI server also gives you a historical vision of how your code quality metrics evolve over time.

Measuring code coverage using the Hudson Cobertura plugin
Global vision - all your metric are belong to us!

None of the tools we've seen so far really give you a vision of code quality both over time and across projects. Sonar does this. Sonar gives you both a broad vision of detailed and aggregate code quality metrics, and the ability to drill down to the source code level. Sonar works with any Maven project, using the Maven code quality reporting plugins - however, it centralizes the rules on the Sonar server, which makes it easier to standardize rules across your projects.

Sonar can also act as a central reference point for code quality rules. You can define a definitive Checkstyle ruleset in Sonar, and then refer to it in both your Maven build (to generate metrics that will be used by your CI server), and in your IDE configuration.

Sonar gives a cross-project vision of code quality

However, for all its merits, Sonar is still a passive reporting tool. It does not replace a dedicated code quality metrics build on your CI server, or proper IDE integration.

Conclusion - get proactive about code quality!

Code quality metrics are an important part of maintaining a healthy project, and reducing technical debt. While static analysis tools that measure code quality metrics and identify best practice coding issues cannot replace a good human reviewer, they can make manual reviews a lot easier. Integrate code quality metrics reporting into your CI builds - this allows better project-wide reporting, and also makes your code quality metrics much more proactive. Finally, Sonar gives you comprehensive cross-project, aggregate, and in-depth reporting on a broad range of metrics.

Training news - Upcoming 3-day TDD workshops and New Online Courses

3-day TDD/BDD workshops

Come get up to speed on the latest techniques in Test-Driven Development, Behaviour-Driven Development and Automated Acceptance Testing!

In response to popular demand, the TDD/BDD workshops have been extended to a 3-day format, and include more time for numerous labs, group programming exercises, and live coding demonstrations.

You will learn about vital modern development best practices such as Test-Driven Development, Behaviour-Driven Development, and Automated Acceptance Tests, and how to apply them in your own projects. You will gain practical experience with the latest in open source testing tools, including JUnit 4.8, Mockito, automated web testing using Page Objects with Selenium 2/Web Driver, and JOLT-Award winner easyb.

You will also learn about the relationship between Automated Acceptance Tests (ATDD) and Test-Driven Development (TDD). You will also learn how to refactor code to make it easier to maintain and to test, and how to use code coverage and code quality tools such as Cobertura to isolate poor quality or poorly-tested code.

A general outline of the course program is shown here:

  • A Gentle Introduction to Test-driven Development and Behaviour-Driven Development
  • Getting started with TDD
  • JUnit Kung Fu - Fixtures, Hamcrest asserts, Exception testing, Timeouts, Parameterized tests, Rule annotations, writing your own Hamcrest matchers...
  • Continuous Testing in Eclipse with Infinitest
  • Using Mocks and Stubs for state-based and interaction-based testing with Mockito
  • Refactoring best practices
  • Using test coverage metrics to improve your testing process
  • Testing Spring applications
  • Testing Servlet and Portlet-based applications
  • Testing a web interface with Selenium 2.0/WebDriver
  • Test-Driving Legacy Code
  • Writing more expressive unit tests in Groovy
  • Automating your Acceptance Tests - Acceptance Test Driven Development
  • Behaviour-Driven Development (BDD) and Acceptance-Test Driven Development (ATDD) with easyb

Over the first 4 months of 2011, we currently have public sessions planned for Sydney, Melbourne, Wellington, Canberra and Brisbane. And of course we can also run tailored versions of the course onsite.

These workshops are becoming increasingly popular, so it's worthwhile booking ahead.

New online courses

In November 2010, Wakaleo Consulting is launching two new and innovative online courses:

  • Fundamentals of Test-Driven Development in Java: This course covers both fundamental TDD/BDD practices, tips and tricks, and also how to apply them effectively with the latest Java tools, including JUnit 4.8, Hamcrest, Infinitest and Mockito.
  • Automated web testing with Selenium 2/Web Driver: Automated web testing is a hard and often frustrating task. However, modern tools and techniques such as Selenium 2 and Page Objects make it much easier to write efficient, robust reusable and maintainable web tests.

To get familiar with TDD practices, or even just to get everyone in your team onto the same page, start off with the Fundamentals of Test-Driven Development in Java workshop. This course take you through essential TDD techniques and strategies, and shows you how to make the most of JUnit's features to write your tests more efficiently and more expressively. The basic course program for this module is outlined here:

  • A gentle Introduction to Test-driven Development and Behaviour-Driven Development
  • Getting started with TDD
  • Continuous Testing in Eclipse with Infinitest
  • JUnit Kung Fu - Fixtures, Hamcrest asserts, Exception testing, Timeouts, Parameterized tests, Rule annotations, writing your own Hamcrest matchers...
  • Using Mocks and Stubs for state-based and interaction-based testing with Mockito
  • Refactoring best practices
  • Test-Driving Legacy Code

If you work with web applications, even complex AJAX ones, you will want to continue on with the Automated web testing with Selenium 2/Web Driver: workshop. This module teaches students how to write clean and robust automated web tests for any sort of web application with the latest generation of open source testing tools. The basic course program is outlined here:

  • Introduction to automated web testing
  • Introducing Selenium 2/WebDriver
  • First steps with WebDriver
  • Identifying page elements
  • Testing with Page Objects and Page Components
  • Testing AJAX applications
  • Working with Page Objects in other languages (Groovy,...)

Both courses are delivered in two, half-day sessions. You can access the labs from any machine - a Windows installer will be provided to set up a dedicated Eclipse environment for the labs (for other O/Ss, just ask). To ensure that as many people as possible can benefit from the workshops, we will be running the sessions approximately once a month, from 12 to 4 on Tuesdays and Thursdays in two time zones: US West Coast (San Francisco) time and Australian EST (Sydney) time:

Fundamentals of Test-Driven Development in Java
North and South America
November 2 and 4 12-4pm US West Coast Time $595 USD
December 7 and 9 12-4pm US West Coast Time $595 USD
Australia/New Zealand/Asia
November 2 and 4 12-4pm Sydney Time $595 USD
December 7 and 9 12-4pm Sydney Time $595 USD
Automated web testing with Selenium 2/Web Driver
North and South America
November 9 and 11 12-4pm US West Coast Time $795 USD
December 14 and 16 12-4pm US West Coast Time $795 USD
Australia/New Zealand/Asia
November 9 and 11 12-4pm Sydney Time $795 USD
December 14 and 16 12-4pm Sydney Time $795 USD

Java Power Tools Bootcamps
There are still places available on the following workshops - book early to avoid disappointment!
London - 24-28 January 2011
Wellington - 4-8 April 2011
Sydney - 9-13 May 2011
Melbourne - 23-27 May 2011

TDD Workshops
Learn more about the most effective ways to design and test your applications at the new Testing and TDD for Java Developers workshop!
Sydney - 17-19 January 2011
Melbourne - 23-25 February 2011
Wellington - 2-4 March 2011
Sydney - 16-18 March 2011
Canberra - 30 Mar-1 Apr 2011
Brisbane - 18-20 April 2011

Online workshops
Can't make a public course? Check out our online TDD/BDD and Automated Web Testing workshops, run in two different time zones (San Francisco and Sydney):
- Fundamentals of TDD in Java
  - November 2,4
  - December 7,9
- Automated Web Testing with Selenium 2/WebDriver
  - November 9,11
  - December 14,16
Unsubscribe <<Email Address>> from this list.

Copyright (C) 2010 Wakaleo Consulting All rights reserved.

Forward this email to a friend
Update your profile
Email Marketing Powered by Mailchimp