1

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 ??

lschin
  • 6,745
  • 2
  • 38
  • 52
Pawan
  • 31,545
  • 102
  • 256
  • 434

1 Answers1

2

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?

Community
  • 1
  • 1
lschin
  • 6,745
  • 2
  • 38
  • 52