I need to return a "human readable" description based on a minimum and a maximum value. Note that the minimum is included in the condition, but the maximum is excluded -- so for instance if the parameters are 6 and 9, the valid values are 6, 7 and 8, but not 9. The maximum value is also nullable.
I started out with nested switch
statements, but I quickly abandoned them because they were unreadable and would be hard to maintain. Instead I kept the original switch
, but moved each nested switch to its own method.
Note that the code currently supports only a handful of cases, but these are the ones currently needed. Though it is possible others will need to be added in the future and thus I'd like to have something that would be easy to maintain.
The code as featured below doesn't feel "mature" to me, but I don't see another solution right now. Perhaps the Strategy Pattern could be a solution, but it feels a bit overkill for this purpose.
internal class InterpretationRetriever
{
public static string Execute(int minimum, int? maximum)
{
switch (minimum)
{
case 0:
return MinimumIsZero(maximum);
case 1:
return MinimumIsOne(maximum);
case 3:
return MinimumIsThree(maximum);
case 5:
return MinimumIsFive(maximum);
case 6:
return MinimumIsSix(maximum);
case 9:
return MinimumIsNine(maximum);
case 10:
return MinimumIsTen(maximum);
default:
return null;
}
}
private static string MinimumIsZero(int? maximum)
{
if (!maximum.HasValue)
{
return "Zero or more";
}
switch (maximum)
{
case 1:
return "Less than one";
case 3:
return "Less than three";
case 5:
return "Less than five";
default:
return null;
}
}
private static string MinimumIsOne(int? maximum)
{
if (!maximum.HasValue)
{
return "One or more";
}
return null;
}
private static string MinimumIsThree(int? maximum)
{
if (!maximum.HasValue)
return null;
switch (maximum)
{
case 6:
return "From three to five";
default:
return null;
}
}
private static string MinimumIsFive(int? maximum)
{
if (!maximum.HasValue)
return null;
switch (maximum)
{
case 10:
return "From five to nine";
default:
return null;
}
}
private static string MinimumIsSix(int? maximum)
{
if (!maximum.HasValue)
return null;
switch (maximum)
{
case 9:
return "From three to eight";
default:
return null;
}
}
private static string MinimumIsNine(int? maximum)
{
if (!maximum.HasValue)
{
return "Nine or more";
}
return null;
}
private static string MinimumIsTen(int? maximum)
{
if (!maximum.HasValue)
{
return "Ten or more";
}
return null;
}
}
MinimumIsSix(int? maximum)
thecase
9 should return"From six to eight"
. \$\endgroup\$ – BCdotWEB May 20 '15 at 13:53