patterncsharpMinor
Returning a description based on a minimum and a maximum value
Viewed 0 times
maximumdescriptionminimumvaluereturningbasedand
Problem
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
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 fiv
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 fiv
Solution
Perhaps instead of this convoluted branching, you could simply handle the three cases: whether there is a maximum and a non-zero minimum, a maximum and a minimum of 0 or there is no maximum.
With this method, adding extra numbers is very simple. I've additionally chosen to split the method into two methods, one for no maximum, and one for a maximum. I feel it shows the different use-cases better, but if it is too much work to refactor all uses of it, this will work instead:
private static Dictionary humanReadableNumbers = new Dictionary()
{
{0, "zero"},
{1, "one"},
{2, "two"},
//etc...
};
public static string Execute(int minimum)
{
return string.Format("{0} or more", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(humanReadableNumbers[minimum]));
}
public static string Execute(int minimum, int maximum)
{
return minimum == 0 ? string.Format("Less than {0}", humanReadableNumbers[maximum]) : string.Format("From {0} to {1}", humanReadableNumbers[minimum], humanReadableNumbers[maximum-1]);
}With this method, adding extra numbers is very simple. I've additionally chosen to split the method into two methods, one for no maximum, and one for a maximum. I feel it shows the different use-cases better, but if it is too much work to refactor all uses of it, this will work instead:
public static string Execute(int minimum, int? maximum)
{
if(maximum.HasValue())
{
return string.Format("{0} or more", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(humanReadableNumbers[minimum]))
}
else
{
return minimum == 0 ?
string.Format("Less than {0}", humanReadableNumbers[maximum]) :
string.Format("From {0} to {1}", humanReadableNumbers[minimum], humanReadableNumbers[maximum-1]);
}
}Code Snippets
private static Dictionary<int, string> humanReadableNumbers = new Dictionary<int,string>()
{
{0, "zero"},
{1, "one"},
{2, "two"},
//etc...
};
public static string Execute(int minimum)
{
return string.Format("{0} or more", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(humanReadableNumbers[minimum]));
}
public static string Execute(int minimum, int maximum)
{
return minimum == 0 ? string.Format("Less than {0}", humanReadableNumbers[maximum]) : string.Format("From {0} to {1}", humanReadableNumbers[minimum], humanReadableNumbers[maximum-1]);
}public static string Execute(int minimum, int? maximum)
{
if(maximum.HasValue())
{
return string.Format("{0} or more", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(humanReadableNumbers[minimum]))
}
else
{
return minimum == 0 ?
string.Format("Less than {0}", humanReadableNumbers[maximum]) :
string.Format("From {0} to {1}", humanReadableNumbers[minimum], humanReadableNumbers[maximum-1]);
}
}Context
StackExchange Code Review Q#91277, answer score: 5
Revisions (0)
No revisions yet.