While I really like the concept of functional programming and F# is definitely on my list of practical useful languages to learn, this article is clearly written by someone who doesn't know C# very well.
Take the "To hell with interfaces" example.
public interface ISortAlgorithm
{
List Sort(List values);
}
public class QuickSort : ISortAlgorithm
{
public List Sort(List values)
{
// Do QuickSort
return values;
}
}
public class MergeSort : ISortAlgorithm
{
public List Sort(List values)
{
// Do MergeSort
return values;
}
}
public void DoSomething(ISortAlgorithm sortAlgorithm,
List values)
{
var sorted = sortAlgorithm.Sort(values);
}
public void Main()
{
var values = new List { 9, 1, 5, 7 };
DoSomething(new QuickSort(), values);
}
No, no, no, no, nope.
public static class MergeSort
{
public static List Sort(List values)
{
// Do MergeSort
return values;
}
}
public void DoSomething(Func, List>
sortAlgorithm, List values)
{
var sorted = sortAlgorithm(values);
}
public void Main()
{
var values = new List { 9, 1, 5, 7 };
DoSomething(QuickSort.Sort, values);
}
No interface necessary.
Or immutability:
"public struct Customer
{
public string Name { get; }
public string Address { get; }
public Customer(string name, string address)
{
Name = name;
Address = address;
}
}
So far so good, but unless someone knows C# very well one could have easily gotten this wrong."
Really?? This is beginner stuff.
I 100% agree that null sucks, functions should not need to be in classes and immutability is great, but this kind of strawman doesn't help his point.