A class with empty methods and/or the methods are declared abstract, should be abstract:

Classes with only empty class declarations, as in:

    Class MyUtilityClass
    {
    ClassMethod m1() { }
    ClassMethod m2() [ abstract ] { /* ... */ }
    }

are often "utility classes", which have no other purpose than to offer a set of methods for other classes to use.

Such classes can be made abstract; if they are not, it is possible to instantiate them using:

You should avoid legacy versions of if, else and for, and use the most modern format which uses curly braces. Below is explained the reason.

A long parameter list can indicate that a new structure should be created to wrap the numerous parameters or that the class is doing too many things.

Noncompliant Code Example

The following code snippet illustrates this rule with a maximum number of 10 parameters:

A long property list can indicate that a new structure should be created to wrap the numerous properties or that the class is doing too many things.

Noncompliant Code Example

The following code snippet illustrates this rule with a maximum number of 10 properties:

This check retrieves the number of lines in a given method and checks that it is less than, or equal to, a given threshold. Note that the opening and closing braces are taken into account, but the method declaration is not.

The default value is 50.

Consider this code example:

    if (condition) {
        return value1
    } else {
        do something
        do somethingElse
        etc
        etc
    }

Since the if block exits unconditionally, the else may be pruned and the code then becomes:

It is common to see code such as this:

    try {
        // very large block of code here
    } catch (e) {
        // handle exception
    }

This, however, is often a misdesign: in all the statements inside the try block, it frequently happens that only one or two actually throw an exception.

It is therefore advised, both for readability and understandability, to keep try blocks as small and self-contained as possible.

This rule checks that a given class has a number of methods which is less than, or equal to, a configurable threshold. A class with too many methods is often an indication that this class needs to be refactored into several, smaller classes.

Note that class methods are not accounted for.

This rule reports any method or class method whose number of statements is above a certain threshold (50 by default; it is configurable).

The more statements a method has, the harder it is to read, test and modify. Consider splitting this method into smaller methods.

This rule checks for the nesting depth of methods.

Methods which are too deeply nested are usually an indication that one, or several, of the below problems apply:

  • the method tries to do too many things;
  • objects used by this method lack isolation;
  • the code in the method fails to account for early exit conditions.

The first of those conditions above is the most frequent one; the suggested fix is therefore to split the logic of this particular method into several methods.

Pages