Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Maintain Better Coding Standards with Ease Using Checkstyle

Coding standards are a time-honored and widely respected programming best practice. However, they are not always easy to put into action. Find out how Checkstyle can help you define and enforce a set of coding standards and promote good and consistent programming habits across your team.


advertisement
riting consistently clear, high quality, maintainable code is an art that requires a certain amount of discipline at the best of times. Coding standards lay out rules and recommendations about the way code should be written and also enshrine good coding habits. Recently, coding standards have enjoyed renewed importance and visibility in software development, as they have been promoted as a key best practice of agile development.

Checkstyle is an open-source tool that can help enforce coding standards and best practices. It can be used as a standalone tool to generate reports on overall code quality, and can also be incorporated into the developer's work environment, providing real-time visual feedback about the code being written.

Coding Standards
Coding standards are a key best practice in many development processes, and for good reason. These standards codify (no pun intended) time-honored traditions and conventions, as well as best practices in the art of software writing. Some recommendations simply define a standard way to layout code or to name classes or variables, while others are designed to make code more reliable or better performing.



However, as Andrew S. Tanenbaum, professor of Computer Science at Vrije Universiteit, Amsterdam, and author of the Minix operating system, said, "The nice thing about standards is that there are so many to choose from." Different companies, teams, and individual developers have developed different programming practices and habits over time. The problems start when developers who use different programming styles and conventions have to work together on a common code base.

Indeed, many standards, such as indentation and naming conventions, are fairly arbitrary things. Should the curly brackets go directly after the instruction, as recommended by the Sun coding conventions?

while (i < 10) { i++; }

Or should they be on a new line, as used by many C++ developers?

while (i < 10) { i++; }

There is no real objective reason to prefer one style over the other: the important thing is to have a well defined and accepted style within your project. Recognized coding standards help to harmonize the way a team or company works together. Once team members are familiar with an established set of coding standards, they will think less about details such as code layout and variable naming conventions and can concentrate better on the real work of coding. New project teams will lose less time at the start of a project making decisions of earth-shattering importance such as where to put the curly bracket after the if statement.

The consistent use of standards also encourages a feeling of 'esprit de corps' within a team or company. And code which is laid out consistently and which follows well-known conventions is easier to read, understand, and maintain.

Sun has defined a set of coding conventions for the Java language that are widely accepted in the industry. Many companies and open source projects also publish their own set of coding conventions, often variations of the Sun conventions. A few typical coding standards are given here:

  • Comments:
    • Write Javadoc comments for all classes, methods, and variables.
  • Naming conventions:
    • Class names should be nouns, in mixed case with the first letter of each internal word capitalized (MyClass).
    • Variable names should be nouns, in mixed case with a lowercase first letter, and with the first letter of each internal word in upper case (myVariable).
    • Constants should be in all uppercase with words separated by underscore (MY_CONSTANT_VALUE).
  • Indentation:
    • Spaces should be preferred to tabs for indenting purposes.
  • Declarations:
    • One declaration per line, with comments, for example:

      int class; // The child's class, from 1 to 8 int age; // The child's age

      rather than:

      int class, age;

  • Statements:
    • Opening braces in compound statements should be at the end of the line that begins the compound statement; the closing brace should begin a line and be indented to the beginning of the compound statement, for example:

      while (i < 10) { i++; }

  • Best practices:
    • Use the final keyword for variables and parameters that will not need to be modified.
    • Don't declare variables within loops
It is a good idea to discuss the conventions to be applied within your company or project with all members of the development team. Each rule should be explained and justified. What is the underlying goal of the rule? Is the rule to be applied systematically, or can there be exceptions? As with many best practices, coding standards must be well-understood by the whole team to be effective.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap