GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing docs.pmd-code.org — Design | PMD Source Code Analyzer
Release date: 30-January-2026
DesignRules that help you discover design issues.
Table of ContentsAbstractClassWithoutAnyMethodSince: PMD 4.2 Priority: High (1) If an abstract class does not provide any methods, it may be acting as a simple data container that is not meant to be instantiated. In this case, it is probably better to use a private or protected constructor in order to prevent instantiation than make the class misleadingly abstract. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: AvoidCatchingGenericExceptionDeprecated The rule has been moved to another ruleset. Use instead: AvoidCatchingGenericException Deprecated Since: PMD 4.2.6 Priority: Medium (3) Avoid catching generic exceptions in try-catch blocks. Catching overly broad exception types makes it difficult to understand what can actually go wrong in your code and can hide real problems. Why these exceptions should not be caught:
Better approaches:
This rule is defined by the following XPath expression: Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: AvoidDeeplyNestedIfStmtsSince: PMD 1.0 Priority: Medium (3) Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.AvoidDeeplyNestedIfStmtsRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: AvoidRethrowingExceptionSince: PMD 3.8 Priority: Medium (3) Catch blocks that merely rethrow a caught exception only add to code size and runtime complexity. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.AvoidRethrowingExceptionRule Example(s): Use this rule by referencing it: AvoidThrowingNewInstanceOfSameExceptionSince: PMD 4.2.5 Priority: Medium (3) Catch blocks that merely rethrow a caught exception wrapped inside a new instance of the same type only add to code size and runtime complexity. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: AvoidThrowingNullPointerExceptionSince: PMD 1.8 Priority: High (1) Avoid throwing NullPointerExceptions manually. These are confusing because most people will assume that the virtual machine threw it. To avoid a method being called with a null parameter, you may consider using an IllegalArgumentException instead, making it clearly seen as a programmer-initiated exception. However, there are better ways to handle this:
To implement that, you are encouraged to use Your parameter validation could thus look like the following: This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.AvoidThrowingNullPointerExceptionRule Example(s): Use this rule by referencing it: AvoidThrowingRawExceptionTypesSince: PMD 1.8 Priority: High (1) Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable, Exception, or Error, use a subclassed exception or error instead. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: AvoidUncheckedExceptionsInSignaturesSince: PMD 6.13.0 Priority: Medium (3) Reports unchecked exceptions in the This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: ClassWithOnlyPrivateConstructorsShouldBeFinalSince: PMD 4.1 Priority: High (1) Reports classes that may be made final because they cannot be extended from outside their compilation unit anyway. This is because all their constructors are private, so a subclass could not call the super constructor. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.ClassWithOnlyPrivateConstructorsShouldBeFinalRule Example(s): Use this rule by referencing it: CognitiveComplexitySince: PMD 6.35.0 Priority: Medium (3) Methods that are highly complex are difficult to read and more costly to maintain. If you include too much decisional logic within a single method, you make its behavior hard to understand and more difficult to modify. Cognitive complexity is a measure of how difficult it is for humans to read and understand a method. Code that contains a break in the control flow is more complex, whereas the use of language shorthands doesn’t increase the level of complexity. Nested control flows can make a method more difficult to understand, with each additional nesting of the control flow leading to an increase in cognitive complexity. Information about Cognitive complexity can be found in the original paper here: https://www.sonarsource.com/docs/CognitiveComplexity.pdf By default, this rule reports methods with a complexity of 15 or more. Reported methods should be broken down into less complex components. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.CognitiveComplexityRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: CollapsibleIfStatementsSince: PMD 3.1 Priority: Medium (3) Reports nested ‘if’ statements that can be merged together by joining their
conditions with a boolean This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: CouplingBetweenObjectsSince: PMD 1.04 Priority: Medium (3) This rule counts the number of unique attributes, local variables, and return types within an object. A number higher than the specified threshold can indicate a high degree of coupling. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.CouplingBetweenObjectsRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: CyclomaticComplexitySince: PMD 1.03 Priority: Medium (3) The complexity of methods directly affects maintenance costs and readability. Concentrating too much decisional logic in a single method makes its behaviour hard to read and change. Cyclomatic complexity assesses the complexity of a method by counting the number of decision points in a method,
plus one for the method entry. Decision points are places where the control flow jumps to another place in the
program. As such, they include all control flow statements, such as 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. By default, this rule reports methods with a complexity >= 10. Additionally, classes with many methods of moderate complexity get reported as well once the total of their methods’ complexities reaches 80, even if none of the methods was directly reported. Reported methods should be broken down into several smaller methods. Reported classes should probably be broken down into subcomponents. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.CyclomaticComplexityRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: DataClassSince: PMD 6.0.0 Priority: Medium (3) Data Classes are simple data holders, which reveal most of their state, and without complex functionality. The lack of functionality may indicate that their behaviour is defined elsewhere, which is a sign of poor data-behaviour proximity. By directly exposing their internals, Data Classes break encapsulation, and therefore reduce the system’s maintainability and understandability. Moreover, classes tend to strongly rely on their data representation, which makes for a brittle design. Refactoring a Data Class should focus on restoring a good data-behaviour proximity. In most cases, that means moving the operations defined on the data back into the class. In some other cases it may make sense to remove entirely the class and move the data into the former client classes. The rule uses metrics to implement its detection strategy. The violation message gives information about the values of these metrics:
The rule identifies a god class by looking for classes which have all of the following properties:
This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.DataClassRule Example(s): Use this rule by referencing it: DoNotExtendJavaLangErrorSince: PMD 4.0 Priority: Medium (3) Errors are system exceptions. Do not extend them. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: ExceptionAsFlowControlSince: PMD 1.8 Priority: Medium (3) This rule reports exceptions thrown and caught in an enclosing try statement.
This use of exceptions as a form of This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.ExceptionAsFlowControlRule Example(s): Use this rule by referencing it: ExcessiveImportsSince: PMD 1.04 Priority: Medium (3) A high number of imports can indicate a high degree of coupling within an object. This rule counts the number of unique imports and reports a violation if the count is above the user-specified threshold. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.ExcessiveImportsRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: ExcessiveParameterListSince: PMD 0.9 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.java.rule.design.ExcessiveParameterListRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: ExcessivePublicCountSince: PMD 1.04 Priority: Medium (3) Classes with large numbers of public methods and attributes require disproportionate testing efforts since combinational side effects grow rapidly and increase risk. Refactoring these classes into smaller ones not only increases testability and reliability but also allows new variations to be developed easily. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.ExcessivePublicCountRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: FinalFieldCouldBeStaticSince: PMD 1.1 Priority: Medium (3) If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object at runtime. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.FinalFieldCouldBeStaticRule Example(s): Use this rule by referencing it: GodClassSince: PMD 5.0 Priority: Medium (3) The God Class rule detects the God Class design flaw using metrics. God classes do too many things, are very big and overly complex. They should be split apart to be more object-oriented. The rule uses the detection strategy described in "Object-Oriented Metrics in Practice". The violations are reported against the entire class. The rule uses metrics to implement its detection strategy. The violation message gives information about the values of these metrics:
The rule identifies a god class by looking for classes which have all of the following properties:
See also the reference: Michele Lanza and Radu Marinescu. Object-Oriented Metrics in Practice: Using Software Metrics to Characterize, Evaluate, and Improve the Design of Object-Oriented Systems. Springer, Berlin, 1 edition, October 2006. Page 80. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.GodClassRule Use this rule by referencing it: ImmutableFieldSince: PMD 2.0 Priority: Medium (3) Reports non-final fields whose value never changes once object initialization ends, and hence may be marked final. Note that this rule does not enforce that the field value be deeply immutable itself. An object can still have mutable state, even if all its member fields are declared final. This is referred to as shallow immutability. For more information on mutability, see Effective Java, 3rd Edition, Item 17: Minimize mutability. Limitations: We can only check private fields for now. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.ImmutableFieldRule Example(s): Use this rule by referencing it: InvalidJavaBeanSince: PMD 6.52.0 Priority: Medium (3) Identifies beans, that don’t follow the JavaBeans API specification. Each non-static field should have both a getter and a setter method. If the field is just used internally and is not
a bean property, then the field should be marked as The rule verifies that the type of the field is the same as the result type of the getter. And that this type matches the type used in the setter. The rule also checks, that there is a no-arg or default constructor available. Optionally the rule also verifies, that the bean implements In order to avoid many false positives in classes that are not beans, the rule needs to be explicitly
enabled by configuring the property This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.InvalidJavaBeanRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: LawOfDemeterSince: PMD 5.0 Priority: Medium (3) The law of Demeter is a simple rule that says "only talk to friends". It forbids fetching data from "too far away", for some definition of distance, in order to reduce coupling between classes or objects of different levels of abstraction. The rule uses a notion of "degree", that quantifies how "far" an object is. Expressions with too high degree can only be used in certain ways. The degree of an expression is defined inductively:
Intuitively, the more you call getters, the more the degree increases. Eventually
the degree reaches the report threshold (property Be aware that this rule is prone to many false-positives and low-priority warnings.
You can increase the
See also the references:
This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.LawOfDemeterRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: LogicInversionSince: PMD 5.0 Priority: Medium (3) Use opposite operator instead of negating the whole expression with a logic complement operator. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: LoosePackageCouplingSince: PMD 5.0 Priority: Medium (3) Avoid using classes from the configured package hierarchy outside of the package hierarchy, except when using one of the configured allowed classes. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.LoosePackageCouplingRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: MutableStaticStateSince: PMD 6.35.0 Priority: Medium (3) Non-private static fields should be made constants (or immutable references) by declaring them final. Non-private non-final static fields break encapsulation and can lead to hard to find bugs, since these fields can be modified from anywhere within the program. Callers can trivially access and modify non-private non-final static fields. Neither accesses nor modifications can be guarded against, and newly set values cannot be validated. If you are using this rule, then you don’t need this
rule This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: NcssCountSince: PMD 6.0.0 Priority: Medium (3) This rule uses the NCSS (Non-Commenting Source Statements) metric to determine the number of lines
of code in a class, method or constructor. NCSS ignores comments, blank lines, and only counts actual
statements. For more details on the calculation, see the documentation
This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.NcssCountRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: NPathComplexitySince: PMD 3.9 Priority: Medium (3) The NPath complexity of a method is the number of acyclic execution paths through that method.
While cyclomatic complexity counts the number of decision points in a method, NPath counts the number of
full paths from the beginning to the end of the block of the method. That metric grows exponentially, as
it multiplies the complexity of statements in the same block. For more details on the calculation, see the
documentation 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.java.rule.design.NPathComplexityRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: PublicMemberInNonPublicTypeSince: PMD 7.21.0 Priority: Medium (3) A non-public type should not declare its own members as public, as their visibility is effectively limited to private, package-private, or protected, making the use of the public modifier misleading. Declaring members as public within a non-public type creates confusion and can lead to unintended consequences if the type is later made public, as this would expose all its public members. However, it is acceptable for a non-public type to inherit public members from a superclass, as this is part of the superclass’s design. To avoid such issues, these members should be declared as protected, package-private, or even private, as appropriate. Note that altering the visibility of such members might unintentionally affect the API of a public subtype. Specifically, a public subtype of a package-private supertype inherits all public methods of the supertype. Converting these methods to private removes them from the subtype as well, which can inadvertently change the subtype’s public API. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: SignatureDeclareThrowsExceptionSince: PMD 1.2 Priority: Medium (3) A method/constructor shouldn’t explicitly throw the generic java.lang.Exception, since it is unclear which exceptions that can be thrown from the methods. It might be difficult to document and understand such vague interfaces. Use either a class derived from RuntimeException or a checked exception. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.SignatureDeclareThrowsExceptionRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: SimplifiedTernarySince: PMD 5.4.0 Priority: Medium (3) Reports ternary expression with the form These expressions can be simplified as follows:
This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: SimplifyBooleanExpressionsSince: PMD 1.05 Priority: Medium (3) Avoid unnecessary comparisons in boolean expressions, they serve no purpose and impacts readability. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: SimplifyBooleanReturnsSince: PMD 0.9 Priority: Medium (3) Avoid unnecessary if-then-else statements when returning a boolean. The result of the conditional test can be returned instead. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.SimplifyBooleanReturnsRule Example(s): Use this rule by referencing it: SimplifyConditionalSince: PMD 3.1 Priority: Medium (3) No need to check for null before an instanceof; the instanceof keyword returns false when given a null argument. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.SimplifyConditionalRule Example(s): Use this rule by referencing it: SingularFieldSince: PMD 3.1 Priority: Medium (3) Reports fields which may be converted to a local variable. This is so because in every method where the field is used, it is assigned before it is first read. Hence, the value that the field had before the method call may not be observed, so it might as well not be stored in the enclosing object. Limitations:
This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.SingularFieldRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: SwitchDensitySince: PMD 1.02 Priority: Medium (3) A high ratio of statements to labels in a switch statement implies that the switch statement is overloaded. Consider moving the statements into new methods or creating subclasses based on the switch variable. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.SwitchDensityRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: TooManyFieldsSince: PMD 3.0 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 XPath expression: Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: TooManyMethodsSince: PMD 4.2 Priority: Medium (3) A class 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: This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: UselessOverridingMethodSince: PMD 3.3 Priority: Medium (3) The overriding method merely calls the same method defined in a superclass. This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.UselessOverridingMethodRule Example(s): This rule has the following properties:
Use this rule with the default properties by just referencing it: Use this rule and customize it: UseObjectForClearerAPISince: PMD 4.2.6 Priority: Medium (3) When you write a public method, you should be thinking in terms of an API. If your method is public, it means other class will use it, therefore, you want (or need) to offer a comprehensive and evolutive API. If you pass a lot of information as a simple series of Strings, you may think of using an Object to represent all those information. You’ll get a simpler API (such as doWork(Workload workload), rather than a tedious series of Strings) and more importantly, if you need at some point to pass extra data, you’ll be able to do so by simply modifying or extending Workload without any modification to your API. This rule is defined by the following XPath expression: Example(s): Use this rule by referencing it: UseUtilityClassSince: PMD 0.3 Priority: Medium (3) For classes that only have static methods, consider making them utility classes. Note that this doesn’t apply to abstract classes, since their subclasses may well include non-static methods. Also, if you want this class to be a utility class, remember to add a private constructor to prevent instantiation. (Note, that this use was known before PMD 5.1.0 as UseSingleton). This rule is defined by the following Java class: net.sourceforge.pmd.lang.java.rule.design.UseUtilityClassRule Example(s): Use this rule by referencing it: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |