Java Developer Tools

Audit - Rules - Program Complexity

Description
This group contains audit rules that look for places where the code might be too complex. Complex code is harder to understand and maintain. It also tends to contain more errors.

Rules:

Details

Block Depth

Summary
Methods should be kept fairly flat.

Description
This audit rule finds methods and constructors that have too many levels of nested blocks. A method with too many levels of nested blocks can be difficult to understand. The definition of "too many" can be set.

Example
If the limit is set to two, then the inner if statement in the following would be flagged as a violation:

    if (firstChoice == null) {
        if (secondChoice == null) {
            if (thirdChoice == null) {
                return null;
            }
        }
    }

Cyclomatic Complexity

Summary
Methods should be kept fairly simple.

Description
This audit rule finds methods, constructors and initializers that are too complex. The complexity is measured by the number of "if", "while", "do", "for", "?:", "catch" and "switch" statements (plus one) in the body of the member.

Example
If the maximum cyclomatic complexity has been configured to be 3, then the following method would be flagged as a violation because it has a cyclomatic complexity of 4:

    public int getHireYear()
    {
        EmploymentRecord employmentRecord = getEmploymentRecord();
        if (employmentRecord == null) return 0;
        HiringRecord hiringRecord = employmentRecord.getHiringRecord();
        if (hiringRecord == null) return 0;
        Calendar hireDate = hiringRecord.getHireDate();
        if (hireDate == null) return 0;
        return hireDate.getYear();
    }

File Length

Summary
Compilation units should not be too long.

Description
This audit rule finds compilation units that are longer than a specified number of lines.

Example
If the rule is configured to allow files of up to 40,000 lines, a file containing 100,000 lines of code would be flagged as a violation.

Large Number of Constructors

Summary
Types should not have too many constructors.

Description
This audit rule finds types that have more than the specified number of constructors. Types that exceed this number are likely to be too complex. Consider making some of the constructors more general.

Example
If the rule is configured to allow 3 constructors and a class with 6 constructors is found, that class will be flagged as a violation.

Large Number of Fields

Summary
Types should not have too many fields.

Description
This audit rule finds types that have more than the specified number of fields. Types that exceed this number are likely to be too complex. Consider splitting the class into multiple smaller classes.

Example
If the rule is configured to allow 6 fields and a class with 15 fields is found, that class will be flagged as a violation.

Large Number of Methods

Summary
Types should not have too many methods.

Description
This audit rule finds types that have more than the specified number of methods. Types that exceed this number are likely to be too complex. Consider splitting the class into multiple smaller classes.

Example
If the rule is configured to allow 20 methods and a class with 114 methods is found, that class will be flagged as a violation.

Large Number of Parameters

Summary
Methods should not have too many parameters.

Description
This audit rule finds methods that have more than the specified number of parameters. Methods that exceed this number are likely to be too complex. Consider moving some of the values and behavior associated with them into a separate class.

Example
If the rule is configured to allow 4 parameters and a method with 12 parameters is found, that method will be flagged as a violation.

Method Chain Length

Summary
Method invocations should not be chained into a long line.

Description
This audit rule looks for places where multiple method invocations are chained together into an overly long chain. There are two problems with long chains of method invocations. The most obvious is that they can make the code difficult to follow. An even more serious problem is that they often point out places where information has not been well encapsulated.

Example
    Color roofColor = party.getWorkAddress().getStructure().getRoof().getColor();

Source Length

Summary
Methods, constructors and initializers should be limited in length.

Description
This audit rule checks for methods, constructors and initializers that are more that the specified number of lines in length.

Type Depth

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.