Java Reference
In-Depth Information
switch (severity) {
case Severity.LOW:
days = 30;
break;
case Severity.MEDIUM:
days = 15;
break;
case Severity.HIGH:
days = 7;
break;
case Severity.URGENT:
days = 1;
break;
}
return days;
}
// Other code for the DefectUtil class goes here
}
The following are a few problems with the above approach in handling the severity of a defect:
•
Since a severity is represented as an integer constant, you can pass any integer value to the
getProjectedTurnaroundDays()
method, not just 0, 1, 2, and 3, which are the valid values
for the severity type. You may want to add a check inside this method so only valid severity
values can be passed to it. Otherwise, the method may throw an exception. However, that does
not solve the problem forever. You will need to update the code that checks for valid severity
values whenever you add new severity types.
•
If you change the value for a severity constant, you must recompile the code that uses it to
reflect the changes. When you compile the
DefectUtil
class,
Severity.LOW
is replaced with 0,
Severity.MEDIUM
is replaced with 1, and so on. If you change the value for the constant
LOW
in the
Severity
class to 10, you must recompile the
DefectUtil
class to reflect this change.
Otherwise, the
DefectUtil
class will still keep using the value 1.
When you save the value of the severity on disk, its corresponding integer value will be saved,
•
for example, 0, 1, 2, etc., not the string values
LOW
,
MEDIUM
,
HIGH
, etc. You must maintain a
separate map to convert from an integer value to its corresponding string representation for all
severity types.
•
When you print the severity value of a defect, it will print an integer, for example, 0, 1, 2, etc.
An integer value for a severity does not mean anything to the users.
•
Note that the severity types of the defects have a specific order. For example, a
LOW
severity
defect is given less priority than a
MEDIUM
severity defect. Since severity is being represented
by an arbitrary number, you must write code using hard-coded values to maintain the order of
the constants defined in the
Severity
class. Suppose you add another severity type of
VERY_HIGH
, which has less priority than
URGENT
and more priority than
HIGH
. Now you must
change the code that handles ordering of severity type because you have added one in the
middle of the existing severity types.
•
There is no automatic way (except by hard coding) that will let you list all severity types.