Consider the following code segment...
public static UserStatus getEnum(int code) {
switch (code) {
case 0:
return PENDING;
case 1:
return ACTIVE;
case 2:
return SUSPENDED;
case 3:
return DELETED;
case 4:
return LOGIN_DISABLED;
default:
return null;
}
}
Now number 3 and 4 in cases(case 3 and case 4) are detected as magic numbers by SONAR.
To avoid that issue I changed my code segment as follows...
public static UserStatus getEnum(int code) {
final int Pending=0;
final int Active=1;
final int Suspended=2;
final int Deleted= 3;
final int Login_details=4;
switch (code) {
case Pending:
return PENDING;
case Active:
return ACTIVE;
case Suspended:
return SUSPENDED;
case Deleted:
return DELETED;
case Login_details:
return LOGIN_DISABLED;
default:
return null;
}
}
Is this a good way to solve the magic number issue in this kind of scenario ?.