• No results found

Tools for Examining Coding Standards

for Increasing Source Code Quality Mariusz JADACH and Bogumiáa HNATKOWSKA

2. Tools for Examining Coding Standards

Today, the coding conventions are very well known and widely used. Many different application tools have been created in order to help the programmers to apply the standards in practice. A few of such tools are described briefly in this section.

2.1. Checkstyle

Checkstyle is a quite well known tool for checking applied coding standard in Java language source code. The tool is developed in Java as an open source project. The last version of the tool (3.5) is free available from the project site in the Internet [5]. Check- style is a command line tool, but many different plug-ins were created to use the tool in popular Java IDEs [6]. For example, Checklipse and Eclipse Checkstyle are plug-ins for the Eclipse environment.

The Checkstyle tool validates Java source files according to given configuration. The configuration is a XML file, which includes the hierarchy of modules. The root module is “Checker”. Its submodule “TreeWalker” is especially important, because it contains the modules of the particular checks. These checks are applied to the corresponding fragments of the abstract syntax tree of the analyzed source file. Many of such checks are present within the tool. Each check may be customized by setting the values of its properties. The built-in checks are well described in [6].

Defining own checks is also possible in the Checkstyle tool. A Java class, which represents a new check must be provided by the user. It extends the Check class. The visitor design pattern is the basis of analyzing source files, so the method visitToken must be implemented. This method is invoked every time when the corresponding code fragment (token) occurs. In order to use the own check, it must be integrated with the configuration file by passing the name of the created class as the new module. More information about extending the tool and writing own checks is available in the documentation [6].

2.2. PMD Project

PMD is also developed as an open source project. It serves for validation of Java source. The base distribution of the tool supports the command line usage, but many plug-ins or extensions were created for popular Java IDEs (Eclipse, NetBeans, JBuilder, etc.) [7].

The checks of the source file are described in XML files, which are called rulesets. The built-in rules of the code are categorized and divided into a number of rulesets (e.g. basic, naming, braces, clone, etc.).

User’s own rulesets may be also created. These files may include references to the existing rules from other rulesets (built-in or user’s). Some properties of each rule (like message or priority) may be customized.

The possibility of creating own rules is also supported in the PMD tool. There are two ways of doing this. First of all, the user may write and deliver Java class (exten- ding AbstractRule). The method visit is invoked for the corresponding syntax elements of the code (exactly for the nodes of the abstract syntax tree). The method’s body is responsible for checking these elements according to the rule conditions. The visitor design pattern is also the basis of analysis by the PMD tool. The latter way of defining own rules is creating the XPath query expressions. Finally, the user’s rules must be integrated into some ruleset by writing new XML elements in the ruleset file. 2.3. ESQMS

A very interesting tool is described in the paper [8]. The Electric Power Research Institute (EPRI) in cooperation with Reasoning Systems built “a prototype toolset called EPRI Software Quality Measurement System (ESQMS) for measuring compliance of software with EPRI’s coding standards” [8].

Refine/C is a special reengineering workbench for C programming language, developed by Reasoning Systems [8]. This workbench is based on the other environ- ment called Software Refinery, built by Reasoning Systems too. Refine/C is the base of the ESQMS tool. The source code in C language is represented as the abstract syntax tree (AST) within the workbench. The rules, which correspond to the particular coding standards are defined and written using a special, very high level language called Refine [8].

The Refine language is complex and powerful. Mathematical operations, AST tree operations, transformations of the code and pattern-matching of AST are supplied [8]. Functions may also be defined. The language and the workbench was especially designed to build different tools for analyzing and transforming the C code. The rules, described in Refine language are similar to functions. A node of the AST tree (currently analyzed) is passed as their argument. The rule contains a set of precon- ditions and a set of postconditions. Each of the postconditions must be true in case of all the preconditions are true. Typically, the preconditions describe the requirements of the coding standard (e.g. the node must be an equality operator), and the postconditions describe the results of checking them (e.g. adding the tuple <node, message> into the violations set). Some automatic transformations of the code (through transformation of AST) are also possible. The postconditions are used to write the correct form of the analyzing code fragment. More detailed informations about the ESQMS and the Refine language may be found in the mentioned paper [8].

3. The Codespector Tool