Search code examples
javapmd

Resolving PMD Error


I am using PMD for checking Coding Standards

I am confused about the following below point where PMD is showing Error

  1. A method should have only one exit point, and that should be the last statement in the method

Currently inside my Method it is as

public boolean validate()
{
if (length == 4) {
return true;
if (length == 2) {
return false;
else
return false ;
return true ;
}

Is my code is wrong ?? and please tell me how can we chage this in case its wrong ??


Solution

  • Example :

    public boolean foo(int i) {
        if (i > 0) {
            return true;   // Multiple exit points
        }
        return false; // ~ ~ ~
    }
    
    public boolean bar(int i) {
        boolean bool = false;
        if (i > 0) {
            bool = true;
        }
        return bool;   // Single exit points
    }
    

    See OnlyOneReturn.

    See question Programming preference - use else ifs with multiple return statements?