# Design

Rules that help you discover design issues.
Table of Contents

## CyclomaticComplexity

Since: PMD 5.1

Priority: Medium (3)

Complexity directly affects maintenance costs is determined by the number of decision points in a method plus one for the method entry. The decision points include ‘if’, ‘while’, ‘for’, and ‘case labels’ calls. Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote moderate complexity, 8-10 denote high complexity, and 11+ is very high complexity.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.CyclomaticComplexityRule

Example(s):

    -- Cyclomatic Complexity of 25
    CREATE OR REPLACE PACKAGE BODY pkg_pmd_working_sequence  AS
    1 PROCEDURE ty_logger  IS BEGIN
    2        IF true
             THEN
                  DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    3        IF true
             THEN
                  DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    4            IF true
                 THEN
                      DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    5            ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
    6        ELSIF false
             THEN
                DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
    7            IF true
                 THEN
                      DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    8            ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
             ELSE
                DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
    9            IF true
                 THEN
                      DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    10           ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
             END IF;
    11         ELSIF false
             THEN
                DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
    12       IF true
             THEN
                  DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    13           IF true
                 THEN
                      DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    14           ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
    15       ELSIF false
             THEN
    16           IF true
                 THEN
                      DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    17           ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
                 DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
             ELSE
                 DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
             END IF;
         ELSE
            DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
    18       IF true
             THEN
                  DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    19           IF true
                 THEN
                      DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    20           ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
    21       ELSIF false
             THEN
                DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
    22           IF true
                 THEN
                    DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    23           ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
                 ELSE
                 DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
    24           IF true
                 THEN
                      DBMS_OUTPUT.PUT_LINE('IF/THEN l_Integer='||l_integer);
    25           ELSIF false
                 THEN
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 ELSE
                    DBMS_OUTPUT.PUT_LINE('ELSIF l_Integer='||l_integer);
                 END IF;
             END IF;
         END IF;
    END;

    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
reportLevel | 10 | Cyclomatic Complexity reporting threshold
showClassesComplexity | true | Add class average violations to the report
showMethodsComplexity | true | Add method average violations to the report

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/CyclomaticComplexity" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/CyclomaticComplexity">
        <properties>
            <property name="reportLevel" value="10" />
            <property name="showClassesComplexity" value="true" />
            <property name="showMethodsComplexity" value="true" />
        </properties>
    </rule>

## ExcessiveMethodLength

Deprecated

Since: PMD 5.1

Priority: Medium (3)

When methods are excessively long this usually indicates that the method is doing more than its name/signature might suggest. They also become challenging for others to digest since excessive scrolling causes readers to lose focus. Try to reduce the method length by creating helper methods and removing any copy/pasted code.

Deprecated: This rule is deprecated since PMD 7.19.0 and will be removed with PMD 8.0.0. Use `NcssCount` to find big methods.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.ExcessiveMethodLengthRule

Example(s):

    CREATE OR REPLACE
    PROCEDURE doSomething BEGIN
        DBMS_OUTPUT.PUT_LINE("Hello world!");
        DBMS_OUTPUT.PUT_LINE("Hello world!");
            -- 98 copies omitted for brevity.
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 100 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/ExcessiveMethodLength" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/ExcessiveMethodLength">
        <properties>
            <property name="minimum" value="100" />
        </properties>
    </rule>

## ExcessiveObjectLength

Deprecated

Since: PMD 5.1

Priority: Medium (3)

Excessive object line lengths are usually indications that the object may be burdened with excessive responsibilities that could be provided by other objects. In breaking these methods apart the code becomes more manageable and ripe for reuse.

Deprecated: This rule is deprecated since PMD 7.19.0 and will be removed with PMD 8.0.0. Use `NcssCount` to find big objects.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.ExcessiveObjectLengthRule

Example(s):

    CREATE OR REPLACE
    PACKAGE BODY Foo AS
        PROCEDURE bar1 IS BEGIN
        -- 1000 lines of code
        END bar1;
        PROCEDURE bar2 IS BEGIN
        -- 1000 lines of code
        END bar2;
        PROCEDURE bar3 IS BEGIN
        -- 1000 lines of code
        END bar3;

        PROCEDURE barN IS BEGIN
        -- 1000 lines of code
        END barn;
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 1000 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/ExcessiveObjectLength" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/ExcessiveObjectLength">
        <properties>
            <property name="minimum" value="1000" />
        </properties>
    </rule>

## ExcessivePackageBodyLength

Deprecated

Since: PMD 5.1

Priority: Medium (3)

Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more manageable and ripe for reuse.

Deprecated: This rule is deprecated since PMD 7.19.0 and will be removed with PMD 8.0.0. Use `NcssCount` to find big objects.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.ExcessivePackageBodyLengthRule

Example(s):

    CREATE OR REPLACE
    PACKAGE BODY Foo AS
        PROCEDURE bar1 IS BEGIN
        -- 1000 lines of code
        END bar1;
        PROCEDURE bar2 IS BEGIN
        -- 1000 lines of code
        END bar2;
        PROCEDURE bar3 IS BEGIN
        -- 1000 lines of code
        END bar3;

        PROCEDURE barN IS BEGIN
        -- 1000 lines of code
        END barn;
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 1000 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/ExcessivePackageBodyLength" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/ExcessivePackageBodyLength">
        <properties>
            <property name="minimum" value="1000" />
        </properties>
    </rule>

## ExcessivePackageSpecificationLength

Deprecated

Since: PMD 5.1

Priority: Medium (3)

Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more manageable and ripe for reuse.

Deprecated: This rule is deprecated since PMD 7.19.0 and will be removed with PMD 8.0.0. Use `NcssCount` to find big objects.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.ExcessivePackageSpecificationLengthRule

Example(s):

    CREATE OR REPLACE
    PACKAGE Foo AS
        PROCEDURE bar1;
        PROCEDURE bar2;
        PROCEDURE bar3;

        ...

        PROCEDURE barN;
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 1000 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/ExcessivePackageSpecificationLength" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/ExcessivePackageSpecificationLength">
        <properties>
            <property name="minimum" value="1000" />
        </properties>
    </rule>

## ExcessiveParameterList

Since: PMD 5.1

Priority: Medium (3)

Methods with numerous parameters are a challenge to maintain, especially if most of them share the same datatype. These situations usually denote the need for new objects to wrap the numerous parameters.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.ExcessiveParameterListRule

Example(s):

    CREATE OR REPLACE
    PROCEDURE addPerson(        -- too many arguments liable to be mixed up
        birthYear pls_integer, birthMonth pls_integer, birthDate pls_integer, height pls_integer, weight pls_integer, ssn pls_integer) {

        . . . .
    END ADDPERSON;

    CREATE OR REPLACE
    PROCEDURE addPerson(        -- preferred approach
        birthdate DATE, measurements BodyMeasurements , ssn INTEGER) BEGIN

        . . . .
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 10 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/ExcessiveParameterList" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/ExcessiveParameterList">
        <properties>
            <property name="minimum" value="10" />
        </properties>
    </rule>

## ExcessiveTypeLength

Deprecated

Since: PMD 5.1

Priority: Medium (3)

Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more manageable and ripe for reuse.

Deprecated: This rule is deprecated since PMD 7.19.0 and will be removed with PMD 8.0.0. Use `NcssCount` to find big objects.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.ExcessiveTypeLengthRule

Example(s):

    CREATE OR REPLACE
    TYPE BODY Foo AS
        MEMBER PROCEDURE bar1 IS BEGIN
        -- 1000 lines of code
        END bar1;
        MEMBER PROCEDURE bar2 IS BEGIN
        -- 1000 lines of code
        END bar2;
        MEMBER PROCEDURE bar3 IS BEGIN
        -- 1000 lines of code
        END bar3;

        MEMBER PROCEDURE barN IS BEGIN
        -- 1000 lines of code
        END barn;
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 1000 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/ExcessiveTypeLength" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/ExcessiveTypeLength">
        <properties>
            <property name="minimum" value="1000" />
        </properties>
    </rule>

## NcssCount

Since: PMD 7.19.0

Priority: Medium (3)

This rule uses the NCSS (Non-Commenting Source Statements) metric to determine the number of lines of code in a plsql type or method. NCSS ignores comments, blank lines, and only counts actual statements. For more details on the calculation, see the documentation `NCSS`.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.NcssCountRule

Example(s):

    DECLARE                                     -- total Ncss: 14
        PROCEDURE bigMethod IS                  -- +1
            x NUMBER;                           -- +1
            y NUMBER := 2;                      -- +1
            a BOOLEAN := FALSE;                 -- +1
            b BOOLEAN := TRUE;                  -- +1
        BEGIN
            IF (a OR b) THEN                    -- +1
                LOOP                            -- +1
                    x := x + 2;                 -- +1
                    EXIT WHEN x >= 12;          -- +1
                END LOOP;

                DBMS_OUTPUT.PUT_LINE('done');   -- +1
            ELSE                                -- +1
                DBMS_OUTPUT.PUT_LINE('false');  -- +1
            END IF;
        EXCEPTION
            WHEN PROGRAM_ERROR THEN DBMS_OUTPUT.PUT_LINE('Error Occurred'); -- +1
        END bigMethod;
    BEGIN
        bigMethod();                            -- +1
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
methodReportLevel | 40 | NCSS reporting threshold for methods
objectReportLevel | 500 | NCSS reporting threshold for objects

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/NcssCount" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/NcssCount">
        <properties>
            <property name="methodReportLevel" value="40" />
            <property name="objectReportLevel" value="500" />
        </properties>
    </rule>

## NcssMethodCount

Deprecated

Since: PMD 5.1

Priority: Medium (3)

This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

Deprecated: This rule is deprecated since PMD 7.19.0 and will be removed with PMD 8.0.0. Use `NcssCount` instead.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.NcssMethodCountRule

Example(s):

    CREATE OR REPLACE PACKAGE BODY AS
     FUNCTION method RETURN INTEGER IS
     BEGIN
       RETURN 1;
     END;
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 100 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/NcssMethodCount" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/NcssMethodCount">
        <properties>
            <property name="minimum" value="100" />
        </properties>
    </rule>

## NcssObjectCount

Deprecated

Since: PMD 5.1

Priority: Medium (3)

This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given Oracle object. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

Deprecated: This rule is deprecated since PMD 7.19.0 and will be removed with PMD 8.0.0. Use `NcssCount` instead.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.NcssObjectCountRule

Example(s):

    CREATE OR REPLACE PACKAGE pkg_ IS
     PROCEDURE Foo IS
     BEGIN
     --this class only has 6 NCSS lines
         super();
         super();
     END Foo;
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 1500 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/NcssObjectCount" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/NcssObjectCount">
        <properties>
            <property name="minimum" value="1500" />
        </properties>
    </rule>

## NPathComplexity

Since: PMD 5.1

Priority: Medium (3)

The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity and increase readability.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.NPathComplexityRule

Example(s):

    CREATE OR REPLACE
    PROCEDURE bar AS BEGIN  -- this is something more complex than it needs to be,
        if (y) THEN -- it should be broken down into smaller methods or functions
            for j IN 0 .. j-1 LOOP
                if (j > r) THEN
                    doSomething;
                    while (f < 5 ) LOOP
                        anotherThing;
                        f := f - 27;
                        END LOOP;
                else
                    tryThis();
                END IF;
            END LOOP;
        END IF;
        if ( r - n > 45) THEN
            while (doMagic) LOOP
                findRabbits;
            END LOOP;
        END IF;
        BEGIN
            doSomethingDangerous();
        EXCEPTION WHEN FooException THEN
            makeAmends;
            BEGIN
                dontDoItAgain;
            EXCEPTION
            WHEN OTHERS THEN
                log_problem;
            END;
        END;
    END;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
minimum | 200 | Threshold at or above which a node is reported

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/NPathComplexity" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/NPathComplexity">
        <properties>
            <property name="minimum" value="200" />
        </properties>
    </rule>

## TooManyFields

Since: PMD 5.1

Priority: Medium (3)

Classes that have too many fields can become unwieldy and could be redesigned to have fewer fields, possibly through grouping related fields in new objects. For example, a class with individual city/state/zip fields could park them within a single Address field.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.plsql.rule.design.TooManyFieldsRule

Example(s):

    CREATE OR REPLACE PACKAGE pkg_too_many_fields AS
        C_CHAR_A CONSTANT CHAR(1 CHAR) := 'A';
        C_CHAR_B CONSTANT CHAR(1 CHAR) := 'B';
        ...
        C_CHAR_Z CONSTANT CHAR(1 CHAR) := 'Z';
    END pkg_too_many_fields;

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
maxfields | 15 | Max allowable fields

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/TooManyFields" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/TooManyFields">
        <properties>
            <property name="maxfields" value="15" />
        </properties>
    </rule>

## TooManyMethods

Since: PMD 5.1

Priority: Medium (3)

A package or type with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

This rule is defined by the following XPath expression:

    //node()
         [ (
            local-name(.) = 'PackageSpecification'
            or
            local-name(.) = 'TypeSpecification'
           )
           and
          (
          count(/descendant::ProgramUnit[
                                             not (
                                                    starts-with(@Name,'get')
                                                    or
                                                    starts-with(@Name,'set')
                                                    or
                                                    starts-with(@Name,'is')
                                                )
                                           ]
               )
          +
          count(/descendant::TypeMethod[
                                             not (
                                                    starts-with(@Name,'get')
                                                    or
                                                    starts-with(@Name,'set')
                                                    or
                                                    starts-with(@Name,'is')
                                                )
                                           ]
               )
          ) > $maxmethods
         ]

This rule has the following properties:

Name | Default Value | Description
--- | --- | ---
maxmethods | 1 | The method count reporting threshold

Use this rule with the default properties by just referencing it:

    <rule ref="category/plsql/design.xml/TooManyMethods" />

Use this rule and customize it:

    <rule ref="category/plsql/design.xml/TooManyMethods">
        <properties>
            <property name="maxmethods" value="1" />
        </properties>
    </rule>