Allow the development of automation: the continuing reconfiguration

2008-08-13 Author: Paul Duvall Source: IBM


This article includes:
Duplicate the conditions of the complexity of the code length method (category)
Reconstruction of too much import as soon as possible ... ... and want to keep other reference Taste and Reconstruction

Reconstruction is recognized to improve the existing code a good way. However, through a consistent and repeatable way to find it necessary reconstruction of the code? This month's automation allows developers to set out how to use static analysis tools to identify the necessary code Taste Reconstruction and gave examples of how to improve the bad taste of the code.
In the past few years, I have seen a lot of projects substantial source code, from the exquisite design to like to use adhesive tape to bind together the code. I wrote a new code of other developers to safeguard the source code. I enjoy the preparation of the new code, but also enjoy the use of some of the existing code, in some way to simplify or to duplicate the code extracted to a public class. In my early working life, many people think if we do not prepare a new code would not have good efficiency.

Me in this series has been praised is efficiency: how to reduce redundancy and time-consuming process, more quickly to implement them. Mission in this article, I like the praise this goal, and will address how to more effectively enforce them.

Reconstruction of a typical method is the introduction of the new code or change the method of the existing code to make small changes. The skills challenge is a development team of application developers are inconsistent, and very easy to miss the opportunity to remodeling. This is what I promote the use of static analysis tools to identify the reason code violation. With these tools, you will be able to know a whole code base, and in a class or method level. Fortunately, in the Java ™ programming, you can choose to download the free open-source a lot of static analysis tools: CheckStyle, PMD, FindBugs, JavaNCSS, JDepend and so on.

In this article, you will learn how to:

The use of circle CheckStyle complexity metrics (cyclomatic complexity), and to provide, such as Replace Conditional with Polymorphism like reconstruction, in order to reduce code complexity conditions Taste CheckStyle assessment of the use of the code repetition rate, and to provide, such as Pull Up Method, such as re - conformation, in order to remove duplicate code using PMD (or JavaNCSS) calculation of the source code line, and to provide, such as Extract Method, such as remodeling, in order to dilute the largest category of code using the Taste CheckStyle (or JDepend) to identify a category of mass the coupling degree (efferent coupling), and the like, such as Move Method of Reconstruction, in order to get rid of excessive import Taste my code will use the following common format to examine each type of code Taste:

Description can be directed out of the code inside the Taste of the problem definition can be found in the Taste of the measurement method can measure the code to display Taste of tools used to repair code Taste of Reconstruction and mode (in some cases)
In essence, this method provides a find and repair the entire code library code Taste of a framework. So you can better understand that the code base in a more dangerous part, and then make a change. Even better, I will show you how to integrate this approach to automatic construction.

Conditions of the complexity of Taste: the conditions of complexity

Metric: Circle complexity

Instrument: CheckStyle, JavaNCSS and PMD

Remodeling: Replace Conditional with Polymorphism, Extract Method

Taste

Conditions of complexity can be a few different ways to appear in the source code. This code is an example of Taste contains a number of conditional statements, such as if, while or for statement. Another condition is based on the complexity of the form of switch statement to show, such as the one shown in the list:

List 1. The use of switch statements to implement the conditions of behavior ...
switch (beerType) (
case LAGER:
System.out.println ( "Ingredients are ...");
...
break;
case BROWNALE:
System.out.println ( "Ingredients are ...");
...
break;
case PORTER
System.out.println ( "Ingredients are ...");
...
break;
case STOUT:
System.out.println ( "Ingredients are ...");
...
break;
case PALELAGER:
System.out.println ( "Ingredients are ...");
...
break;
...
default:
System.out.println ( "INVALID.");
...
break;
)
...


switch statement itself is not wrong. However, when a statement contains too many choices and code, it might imply that there is reconstruction of the code required.

Metric

To determine the conditions of the complexity of the code Taste, the methodology for determining the required circle complexity. Circle complexity is a measurement method, by Thomas McCabe in 1975, the definition of. Cyclomatic complexity degrees (Cyclomatic Complexity Number, CCN) One way to measure the quantity of a particular path. Whether there is a way in the number of paths, which start from one start CNN. A condition of each structure, such as if, switch, while and for statements, have been assigned a value of 1 and abnormal path. One way to show that the total CCN its complexity. A lot of people think when CCN for 10 or more than 10, it shows that the method is too complicated.

Tools

CheckStyle, JavaNCSS, as well as the PMD measurement is the complexity of the open source circle instrument. 2 to display the list using the XML definition of the rules document CheckStyle a code snippet. CyclomaticComplexity module defines a method of maximizing the CCN.

List 2. Configuration CheckStyle, complexity for the search coil 10 or greater than the 10 Ways <module name="CyclomaticComplexity">
<property name="max" value="10"/>
</ module>


2 CheckStyle with a list of rules file, a list of the 3 examples to demonstrate how Gant will CheckStyle as part of an automated build to run. (See What is Gant? Sidebar):

List 3. Use Gant script to perform CheckStyle check target (findHighCcn: "Finds method with a high cyclomatic complexity number") (
Ant.mkdir (dir: "target / reports")
Ant.taskdef (name: "checkstyle",
classname: "com.puppycrawl.tools.checkstyle.CheckStyleTask",
classpathref: "build.classpath")
Ant.checkstyle (shortFilenames: "true", config: "config / checkstyle / cs_checks.xml",
failOnViolation: "false", failureProperty: "checks.failed", classpathref: "libdir") (
formatter (type: "xml", tofile: "target / reports / checkstyle_report.xml")
formatter (type: "html", tofile: "target / reports / checkstyle_report.html")
fileset (dir: "src") (
include (name :"**/*. java ")
)
)
)


3 in the list to create a Gant script in Figure 1 display CheckStyle report. The part of the following instructions to a method of complexity CheckStyle circle irregularities.

Figure 1. CheckStyle high CCN report is based on a method to indicate the failure of

Reconstruction

Figure 2 for the use of UML express the Replace Conditional with Polymorphism Reconstruction:

Figure 2. Polymorphic substitution with conditional statements

Click here to view a complete map.

In Figure 2, I:

Created a BeerType called the Java interface defines a generic showIngredients () method for each implementation BeerType created a category in order to enable the article to maintain brevity, I only each category to provide a method of implementation. Clearly, the creation of an interface method may be more than one. Remodeling can make the code easier to maintain, such as Replace Conditional with Polymorphism and Extract Method (due to be discussed later in this article).

Duplicate code Taste: duplicate code

Metric: code repetition rate

Instrument: CheckStyle, PMD

Reconstruction: Extract Method, Pull Up Method, Form Template Method, Substitute Algorithm

Taste

Duplicate code in the code library probably happen quietly. Sometimes, copy and paste some code than the act to another type of generalization is more easy. Copy and paste method but there is a problem that it forced more than copy the code, and required maintenance. And when copying a minor code changes caused by inconsistent behavior can occur when they are not readily aware of the problem, depending on which method in the implementation of the act. 4 is a turn off a list of database connection code sample code, the same code appears in two ways:

List 4. Duplicate code public Collection findAllStates (String sql) (
...
try (
if (resultSet! = null) (
resultSet.close ();
)
if (stmt! = null) (
stmt.close ();
)
if (conn! = null) (
conn.close ();
)
catch (SQLException se) (
throw new RuntimeException (se);
)
)
...
)
...
public int create (String sql, Beer beer) (
...
try (
if (resultSet! = null) (
resultSet.close ();
)
if (stmt! = null) (
stmt.close ();
)
if (conn! = null) (
conn.close ();
)
catch (SQLException se) (
throw new RuntimeException (se);
)
)
...
)


Metric

Search duplicate code in the code measurement method is the class library of internal and other types of duplicate code between the search. There is no instrument, the Inter-Class on the duplicate of the more difficult to assess. Copy the code because usually happen some minor changes, so not only to measure exactly the same code, but also to measure similar to the code, both are important.

Tools

PMD's Copy / Paste Detector (CPD) and CheckStyle of these two open source tools can be used throughout the Java code library search is similar to the code. 5 in the list of configuration files CheckStyle example demonstrated how to use StrictDuplicateCode modules:

List 5. CheckStyle the use of at least 10 firms to find duplicate code <module name="StrictDuplicateCode">
<property name="min" value="10"/>
</ module>


5 min list of property set will be marked CheckStyle duplicate the smallest number of rows for inspection. Under such circumstances, it will only indicate that at least 10 firms have similar or duplicate the code block.

Figure 3 show an automatic build run, the list of modules installed 5 results:

Figure 3. CheckStyle report high Spend duplicate code instructions

Reconstruction

6 in the list, I spent 4 in the list of duplicate code, the use of the Pull Up Method remodeling to reduce duplicate degrees - will act to extract from the larger to an abstract class methods:

List 6. Pull Up Method
...
) Finally (
closeDbConnection (rs, stmt, conn);
)
...


Duplicate code is not unexpected. I will never recommend a team to work hard to achieve what no duplicate such goals, this is unrealistic. However, to ensure that the code library does not duplicate the code number of this goal is achievable. Such as PMD's CPD or CheckStyle such a static analysis tool, you can the whole process of analysis as part of automated build, continuous analysis, duplicate code high region.

Long method (category)
Taste: Long Ways (category)

Metric: number of source lines of code (SLOC)

Tools: PMD, JavaNCSS, CheckStyle

Reconstruction: Extract Method, Replace Temp with Query, Introduce Parameter Object, Preserve Whole Object, Replace Method with Method Object

Taste

I have been trying to insist on a way to limit the type of experience in law firms at 20 or 20 lines or less. Of course, this principle is also possible there will be exceptions, but if my way more than 20 lines, I would be even more careful to understand it. Under normal circumstances, long methods and conditions are closely related to complexity. The largest category of Ways and between long have contact inevitable. I can give you a 2200 line display method, which are required to maintain me at a project found. I will contain the entire 25,000 lines of code to print out the class, let me inside of my colleagues to identify errors. So to speak, when I put the code to print out rolled down the corridor, they have agreed to my views on the.

7 Highlight the list part of a long way to show code examples Taste a small portion:

List of 7. Taste a long way to code public void saveLedgerInformation () (
...
try (
if (ledger.getId ()! = null & & filename == null) (
getLedgerService (). saveLedger (ledger);
) Else (
accessFiles (). decompressFiles (files, filenames);
)
if (! files.get (0). equals (upload)) (
upload = files.get (0);
filename = filenames.get (0);
)
if (invalidFiles.isUnsupported (filename)) (
setError (fileName, message.getMessage ());
) Else (
LedgerFile entryFile = accessFiles (). Add (upload, filename);
if (fileType! = null & & FileType.valueOf (fileType)! = null) (
entryFile.setFileType (FileType.valueOf (fileType));
)
getFileManagementService (). saveLedger (ledger, entryFile);
if (! FileStatus.OPENED.equals (entryFile.getFileStatus ())) (
getFileManagementService (). importLedgerDetails (ledger);
)
if (uncompressedFiles.size ()> 1) (
Helper.saveMessage (getText ( "ledger.file"));
)

if (user.getLastName ()! = null) (
SearchInfo searchInfo = ServiceLocator.getSearchInfo ();
searchInfo.setLedgerInfo (null);
isValid = false;
setDefaultValues ();
resetSearchInfo ();
if (searchInfoValid & & ledger! = null) (
isValid = true;
)
)

) Catch (InvalidDataFileException e) (
ResultType result = e.getResultType ();
for (ValidationMessage message: result.getMessages ()) (
setError (fileName, message.getMessage ());
)
ledger.setEntryFile (null);

) ...


Metric

In the past few years, SLOC metric methods have been mistaken for a symbol of high efficiency. Even though we all know, is not necessarily the number of rows, the better. But when it comes to complexity, SLOC However, a useful measure. A method (or type) the more the number of rows, the future maintenance of its code may be more difficult.

Tools

8 in the list of scripts for the long method (categories) to find the SLOC metric methods:

List 8. Identification of the class is too large and methods Gant script target (findLongMethods: "runs static code analysis") (
Ant.mkdir (dir: "target / reports")
Ant.taskdef (name: "pmd", classname: "net.sourceforge.pmd.ant.PMDTask",
classpathref: "build.classpath")
Ant.pmd (shortFilenames: "true") (
codeSizeRules.each (rfile ->
ruleset (rfile)
)
formatter (type: "xml", tofile: "target / reports / pmd_report.xml")
formatter (type: "html", tofile: "target / reports / pmd_report.html")
fileset (dir: "src") (
include (name :"**/*. java ")
)
)
)


I also use the Ant API visit Gant to execute Ant tasks. 8 in the list, I call PMD static analysis tool to search the code library Ways length. PMD (together with JavaNCSS with CheckStyle) can also be used to search a long way to broad categories, as well as other code Taste.

Reconstruction

9 display list using the Extract Method reconfiguration to reduce the long list of 7 Ways code Taste of a case in point. 7 Ways to be a list of acts to extract a list of 9 of the code, I will be able to get a list of 7 saveLedgerInformation () method call the new isUserValid () method:

The list 9. Extract Method Reconstruction private boolean isUserValid (User user) (
boolean isValid = false;
if (user.getLastName ()! = null) (
SearchInfo searchInfo = ServiceLocator.getSearchInfo ();
searchInfo.setLedgerInfo (null);
setDefaultValues ();
resetSearchInfo ();
if (searchInfoValid & & ledger! = null) (
isValid = true;
)
)
return isValid;
)


Typically, a long ways and categories also suggests that other code Taste exist, such as the conditions of complexity and duplicate code. Therefore, a long way to find these and other categories also can repair the problem.

Taste import too much: too much into

Metric: reports coupling (one for each type of fan-out (fan-out))

Instrument: CheckStyle

Remodeling: Move Method, Extract Class

Taste

Too much into a category shows that too much reliance on other classes. You will notice that, because of a class with a lot of other types of coupling too closely, change the category will have to cause a lot of other types of changes at this time on the existence of this category of the Taste of this code. List of more than 10 import is an example:

A list of 10. A class of more than import import com.integratebutton.search.SiteQuery;
import com.integratebutton.search.OptionsQuery;
import com.integratebutton.search.UserQuery;
import com.integratebutton.search.VisitsQuery;
import com.integratebutton.search.SiteQuery;
import com.integratebutton.search.DateQuery;
import com.integratebutton.search.EvaluationQuery;
import com.integratebutton.search.RangeQuery
import com.integratebutton.search.BuildingQuery;
import com.integratebutton.search.IPQuery;
import com.integratebutton.search.SiteDTO;
import com.integratebutton.search.UrlParams;
import com.integratebutton.search.SiteUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
...


Metric

Duty to find the class with too many one way is through the spread coupling measurement method, but also refers to fan-out complexity. Fan-out complexity analysis to be dependent on the category of a class assignment for each one.

Tools

Display a list of 11 used to set the maximum fanout CheckStyle complex Degrees examples:

A list of 11. CheckStyle used to set the maximum fan-out complexity <module name="ClassFanOutComplexity">
<property name="max" value="10"/>
</ module>


Reconstruction

Repair because of too much import caused by tight coupling There are many ways. For a list of 10 such as the code is available, including Move Method Reconstruction Reconstruction: The Ways * Query from a separate category to move to the Java interface, and define all the Query type to be a common method of implementation. Factory method and then use the model, so that coupling with the associated interface.

Gant automatically through the use of CheckStyle Ant build script execution mission, I can search the code library, search too much dependent on other types of classes. When modify these types of code, it can achieve a specific remodeling (such as Move Method) and specific design patterns in order to gradually improve the maintainability.

Reconstruction ... want ... as soon as possible and want frequent continuous integration (Continuous Integration, CI) is often integrated change. As the same manner as a typical implementation, whenever the project's version control repository to make a change when running on independent machines on the CI server will automatically trigger an automatic build. In order to ensure that the list of 3 and 8 in the list of scripts on the database can make changes in line to run, you need to configure a Hudsona such as CI server (see references). Hudson is a WAR file of the form, you can put it into any Java Web container.

3 because of the list and a list of 8 examples of the use of Gant, the following brief me on the Hudson CI server configuration to run Gant script steps:

Hudson at the dashboard to install the Gant plug-Hudson: First of all, select Manage Hudson, and then select Manage Plugins, then select Available tab. Tab at the Gant plug-ins check box is selected, and then click Install button.


Restart the Web container (for example, Tomcat).


Choose Manage Hudson, then select Configure System. At Gant installation part, type a unique name and Groovy installed on machines running Hudson on location. Save the changes.


Back to dashboard (Hudson select the link), select an existing Hudson Job, and then select Configure, then click Add build step button, select Invoke Gant script option.
Configuration Hudson, make it run automatically prepared using Gant build script. Once the methods and conditions such as long this kind of code complexity Taste was introduced into the code base, you will be immediately and their associated measurement methods of feedback.

Other Taste and Reconstruction, not all are regulated by Taste measure. However, static analysis tools can reveal more than the flavor of my model as these. Table 1 lists the other flavor of the code, tools, and the possible reconstruction of examples:

Table 1. Other instrument Taste Taste and Reconstruction Reconstruction die code PMD Remove Code
Temporary field PMD Inline Temp
Inconsistent / restrained (uncommunicative) the name of CheckStyle, PMD Rename Method, Rename Field
Long list of parameters PMD Replace Parameter with Method, Preserve Whole Object, Introduce Parameter Object

This article provides a code with a Taste of the mode of measurement methods, this measurement method can be configured as static analysis tools by automatically marking. You can use or not to use specific design patterns for reconfigurable. This provides you with a consistent, repeatable way to find and repair the framework code Taste. I firmly believe that the examples in this article will also help you use static analysis tool to search this article does not relate to code Taste.

References refer to this article you can study at developerWorks Web site on the world's original English text.
Automation allows developers (Paul Duvall, developerWorks): Read the entire article series. "Continued examination" (August 2006), as well as "use Eclipse plug-ins enhance the quality of code" (January 2007) of the topic and is closely related to this article.
Smells to Refactorings: a table that lists the specific code of Taste recommend reconstruction.
Refactoring: Improving the Design of Existing Code (Martin Fowler, Addison-Wesley Professional, 1999 years): With regard to improving the existing code base the design of the basic books.
Alpha List of Refactorings: Martin Fowler prepared a list of remodeling.
Refactoring to Patterns (Joshua Kereviesky, Addison-Wesley Professional, 2004 years): The design pattern applies to remodeling to improve the code. I was listening to Josh at Agile 2005 on Denver's speech will only have a big print out of the class of ideas.
"The pursuit of code quality: Monitoring loop complexity" (Andrew Glover, IBM developerWorks, 2006年3月): Introduction When the code complexity is too high at the time of how to proceed.
"The pursuit of code quality: Using code metric reconstruction" (Andrew Glover, IBM developerWorks, 2006 years 5 months): Using code metrics and Extract Method pattern has destination remodeling.
"Continuous Integration: Improving Software Quality and Reducing Risk" (Paul Duvall, etc., Addison-Wesley Signature Series, 2007 years): Chapter 7 (continued checks) covered in this article involved a lot of tools.
"Using Gant to build software" (Andrew Glover, developerWorks, 2008 years 5 months): reading tutorial, learn how to build software and flexible use of Gant.
"(Ant to Gant) automagically" (Andrew Glover, The Disco Blog, 2008 years 4 months): Based on the existing Ant script to generate Gant script.
"Gant with Hudson in 5 steps" (Andrew Glover, The Disco Blog, 2008 years 5 months): Configure Hudson continuous integration server to run Gant build script.
"The pursuit of code quality: the quality of the software architecture of the code" (Andrew Glover, IBM developerWorks, 2006 years 4 months): Use coupling metrics to support the system architecture.
Browse technology bookstore to see about the above topic and other technical subject books.
developerWorks Java Technology Zone: Here a few of the 100 articles about Java programming.
Access to products and technologies
Gant: download Gant, beginning with a predictable, repeatable way to build software.
CheckStyle: download CheckStyle, collect metrics and to better assess the code Taste.
PMD: download PMD, collect metrics and to better assess the code Taste.
Hudson: a free and open source continuous integration server.
Discussion
Improve Your Code Quality Forum: developerWorks regular contributor Andrew Glover as a consultant at this forum of experts to contribute his wealth of experience, the main concern to improve the code quality issues.
Accelerate the development of space: developerWorks regular contributor Andrew Glover maintained a one-stop portal, including developer testing, continuous integration, code metrics, etc. and reconstruction.
Visit the developerWorks blog and add developerWorks community.