I'm playing around with the concept of functional programming.
I have flattened the nested if statements and made by code more verbose.
The refactored code looks overly complicated.
What could be a simpler approach?
Nested Ifs
public static int Main(string[] args)
{
int computer = 1;
string brand = "PC";
if (computer < 2)
{
if (brand != "Apple")
{
// perform complex calculation 1
Console.WriteLine("you don't own a Mac");
return 0;
}
else
{
// perform complex calculation 2
Console.WriteLine("you own less than 2 Macs");
return 1;
}
}
else
{
// perform complex calculation 3
Console.WriteLine("you own two or pcs");
return 2;
}
}
Flattened Main
public static int Main(string[] args)
{
int computer = 1;
string brand = "PC";
return CalculatedResult = DetermineComputer(computer, brand);
}
Extension Methods
public static class extensions
{
public static int ComplexCalc1(this bool val)
{
Console.WriteLine("you don't own a Mac");
return 0;
}
public static int ComplexCalc2(this bool val)
{
Console.WriteLine("you own less than 2 Macs");
return 1;
}
public static int ComplexCalc3(this bool val)
{
Console.WriteLine("you own greater than two pcs");
return 2;
}
public static int ComplexCalc4(this bool val)
{
Console.WriteLine("Condition not accounted for");
return -1;
}
public static bool IsLessThanTwo(int computer) => computer < 2;
public static bool IsApple(string brand) => brand == "Apple";
public static (Func<bool, int> func, bool result) IsLessThanTwoAndNotApple(this int computer, string brand)
=> (ComplexCalc1, IsLessThanTwo(computer) && (IsApple(brand) == false));
public static (Func<bool, int> func, bool result) IsLessThanTwoAndIsApple(this int computer, string brand)
=> (ComplexCalc2, IsLessThanTwo(computer) && IsApple(brand));
public static (Func<bool, int> func, bool result) Unaccountedfor(this int computer, string brand)
=> (ComplexCalc4, true);
public static (Func<bool, int> func, bool result) IsGreaterThanTwoAndIsNotApple(this int computer, string brand)
=> (ComplexCalc3, (IsLessThanTwo(computer) == false) && (IsApple(brand) == false));
}
Calculation
public static int DetermineComputer(int computer, string brand)
{
var methodlist = new List<(Func<bool, int>, bool)>
{
computer.IsLessThanTwoAndNotApple(brand),
computer.IsLessThanTwoAndIsApple(brand),
computer.IsGreaterThanTwoAndIsNotApple(brand),
computer.Unaccountedfor(brand)
};
var tupleResult = methodlist.Where(x => x.Item2 == true).Select(x =>
new {
functor = x.Item1,
result = x.Item2
}).First();
return tupleResult.functor(tupleResult.result);
}
Unit Tests
[DataRow(3, "Apple", -1)]
[DataRow(2, "Apple", -1)]
[DataRow(1, "Apple", 1)]
[DataRow(0, "Apple", 1)]
[DataRow(3, "PC", 2)]
[DataRow(2, "PC", 2)]
[DataRow(1, "PC", 0)]
[DataRow(0, "PC", 0)]
[DataTestMethod]
public void NestedIfs_Demo(int first, string second, int expected)
{
int computer = first;
string brand = second;
var CalculatedResult = DetermineComputer(computer, brand);
Assert.AreEqual(expected, CalculatedResult);
}
Because community members review edit
as a reason, which won't apply once you surpassed 2k rep.) \$\endgroup\$