Allow development automation to continue refactoring-identify code flavors with static analysis tools-Develop

Source: Internet
Author: User
Tags checkstyle
Series content: This content is part of the series: Making development automation

Over the past few years, I've seen a lot of the source code for many projects, from beautifully designed to like tape-bound code. I have written new code and maintained the source code of other developers. I like to write new code, but I also like to use some of the existing code, to simplify it in some way or to extract duplicate code into a common class. In my early career, many people thought it would be inefficient not to write new code. Fortunately, in the late 1990s, Martin Fowler wrote the book of Refactoring (see Resources), which makes it possible to improve existing code without altering external behavior.

What I've always admired in this series is efficiency: How to reduce the redundancy of time-consuming processes and execute them more quickly. In the task of this article, I would like to promote this goal, and will discuss how to implement them more effectively. About this series

As developers, we are committed to automating processes for our users, but many developers are neglecting the opportunity to automate our own development processes. To this end, we have written a series of articles to automate the development of the software development process, which will show you when and how to successfully apply automation.

A typical approach to refactoring is to make small changes to existing code when introducing new code or changing methods. The challenge with this technique is that the developers of a development team have inconsistent application methods and can easily miss the opportunity to refactor. This is why I advocate using static analysis tools to identify coding violations. With these tools, you will be able to understand the code base in general and at the level of the class or method. Fortunately, in java™ programming, there are many open source static analysis tools that you can choose to download for free: Checkstyle, PMD, FindBugs, JavaNCSS, JDepend, and so on.

In this article, you will learn how to: Use Checkstyle metric cyclomatic complexity (cyclomatic complexity) and provide refactoring such as Replace Conditional with polymorphism to reduce Conditional complexity Code flavors use Checkstyle to evaluate code repetition rates and provide refactoring such as Pull up method to remove duplicate code to compute lines of source code using PMD (or JavaNCSS), and to provide refactoring such as Extract. To dilute the taste of large classes of code use Checkstyle (or JDepend) to determine the outgoing coupling of a class (efferent coupling) and provide refactoring such as Move method to remove too much import code flavor

I'll check each code flavor using the following generic format: Describes the flavor that can indicate the problem in the code the metric method for finding the flavor shows the tools that can measure the taste of the code to provide refactoring and patterns to fix the flavor of the code (in some cases)

Essentially, this approach provides a framework for finding and fixing the flavor of code throughout the code base. This allows you to better understand the more dangerous parts of the code base and then make the changes. Better still, I'll show you how to integrate this approach into automated builds. Does your code have any.

The so-called code flavor is actually just a hint that some content may be wrong. Like patterns, code flavors provide a common glossary that you can use to quickly identify potential problems of these types. It is difficult to really demonstrate the flavor of the code in the article, because it may include many lines of code, which excessively increases the length of the article. So I'll just show you some of the flavors, and then you can infer the rest of the code based on your experience with the taste of a particular code. Complexity of conditions

Taste: Complexity of conditions

Metric: Cyclomatic Complexity

Tools: Checkstyle, JavaNCSS, and PMD

Refactoring: Replace Conditional with polymorphism, Extract method flavor

Conditional complexity can appear in the source code in several different ways. An example of this code flavor is the presence of multiple conditional statements, such as if, while, or for statements. Another condition complexity is presented in the form of a switch statement, as shown in Listing 1: Listing 1. Use the switch statement to perform the conditional behavior 1 2 3 4 5 6 7 8 9 An (beertype) ("Switch") () {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.