This chapter covers
- Benefits and tenets of functional programming
- Functional features of the C# language
- Using records and pattern matching for type-driven programs
Functional programming (FP) is a programming paradigm: a different way of thinking about programs than the mainstream, imperative paradigm you’re probably used to. For this reason, learning to think functionally is challenging but also very enriching. My ambition is that after reading this book, you’ll never look at code with the same eyes as before!
The learning process can be a bumpy ride. You’re likely to go from frustration at concepts that seem obscure or useless to exhilaration when something clicks in your mind, and you’re able to replace a mess of imperative code with just a couple of lines of elegant, functional code.
This chapter will address some questions you may have as you start on this journey: What exactly is functional programming? Why should I care? Can I code functionally in C#? Is it worth the effort?
What exactly is functional programming? At a very high level, it’s a programming style that emphasizes functions while avoiding state mutation. This definition is already twofold, as it includes two fundamental concepts:
- Functions as first-class values
- Avoiding state mutation
Let’s see what these mean.