Let’s start with a question:
Is it worth it?
Like almost everything in software development, It depends… I really like the functional programming paradigm, then, probably there’s some bias here. But let’s trying to be reasonable, right?
The first major point is: why to use C#? — If you want to use functional programming there’s a huge probability that F# fits better than C# in a .NET environment.
There’s a little problem here, a lot of .NET developers have no idea what is F#, I genuinely think that this is a huge mistake, but it’s topic for another article. So, let’s keep it at C# and this bring us to the next question:
Do you like System.Linq?
The answer probably will be: YES!
Because System.Linq is awesome and guess what, it works with one of the core functional concepts: High Order Functions. There’s also a lot more things related to System.Linq, like extension methods, IEnumerables and so on.
Backing to the original question, well, C# contains some native features related to functional paradigm, so, it’s fair to say, that you can do great things by using it.
Let’s code a simple example, we want to iterate an IEnumerable
to show each one of its elements:
Our core feature was implemented successfully, but is that the best code that we can create? — I don’t think so.
Let’s remove the hard coded values and make it a parameter, also, we can use IEnumerable
interface with generics, it makes a lot more sense:
The static
modifier it’s just for make it callable by Main
method directly. This new implementation is better than the previous one, but there’s still some hard code here, can you see it?
It’s called hard coded behavior, as developer I have to say that it’s almost so toxic as hard coded values.
But, what it exactly means?
Well, when we think about code, specially object oriented code, variables are understood as values or attributes and methods as behaviors. So, hard coded behavior occurs when you call a method hard coded, like, Console.WriteLine
in our code.
Okay, but is it possible to pass a function as parameter? — Sure, just like System.Linq
do.
As I said before, there’s a lot of functional programming support in C# and one of these is the reference type called delegate
. It’s used to encapsulate a function/method, it’s like a variable that can work with functions instead of values.
Delegates are a topic apart, so if you don’t know anything about it, just check this link out.
Fortunately, there’s two generic delegates built-in C#. They are Action
and Func
, both of them can work with generic types.
The only difference between them are the fact that Action
is used to encapsulate methods with no return (void
methods), for all other type of methods the Func
delegate is used.
Let’s change our code a little bit, just replace the hard coded call to a generic one:
Congratulations! You just did your first High Order Function!
This method is a lot more generic than the previous one, now we need to change just the Iterate
call if we want a different operation.
For instance, if we want to write the value squared instead of the original one:
As you may notice, delegates
work with named and anonymous functions, in the above code, we changed the Iterate
call by passing a lambda expression as parameter.
The icing on the cake is make it an extension method, so, it’ll works like a Linq method.
If you don’t know what an extension method is, check this link.
In order to do that, we need to create a new static
class and use the this
keyword before the first parameter:
Now we can use it as an IEnumerable
method:
This is a very simple application of functional programming using C#, but the main point is: knowing functional programming will extends your toolbox, so you probably gonna be a better developer with this skills.
It is more than another language, it is a lot more related to the way of thinking and solving problems than the programming tools you will use.
And please, you shouldn’t try to use your functional programming skills in each single piece of code you develop.
Use it to improve your code and your solutions, not to make them more complex.
And remember:
With great power comes great responsibility
See you in the next post!
Top comments (0)