FP is Fun and Practical

Gábor Manner
Emarsys Craftlab
Published in
4 min readFeb 22, 2023

--

I have come to a point in my life where I felt I had to share my thoughts and experience with Functional Programming (FP).

When people meet functional programming for the first time, they usually find it weird or think it’s an exotic style of coding, which is way too complicated. And imagine its enthusiasts and prophets as some weirdos, math-maniacs or just a bunch of snobbish, elitist freaks.

Well… I can understand and there was a time when I felt something similar, but now I couldn’t agree less. FP is just not the same what we are used to. Most of us learned to code imperatively (in the form of OOP), that is how we think and build up our codes. But Functional Programming is just another state of mind, a different approach on how to control and structure programs. It is not more difficult but just as difficult as imperative or procedural — in another way :) And you don’t have to be an academic to see its benefits.

There are a lot of sources where you can learn FP and even some that tell you this is the future or the only true way, or the most reasonable choice for developing. And often it seems that developers are split into two camps. The “FPists” and the “OOPists”. But I don’t want to strengthen any of those camps, and also

I don’t want to

  • teach you how to program functionally.
  • convince you that FP is basically better than anything else.

But I’d like to

  • introduce it to you, if you have never met,
  • or make you friends if you have met, but it looked terrifying or difficult,
  • share my love of FP and the reasons why,
  • and try to show some practical examples as well.

Sorry, but I can’t do it completely objectively. So it will be a strange love story about how I learned to stop worrying and love the FP.

TLDR

I’ll start with some intro and explanation about functional (and declarative) programming, and some pre-summary of the things I am about to tell you in the later episodes. Stay tuned and come back next week for newer posts.

What is functional programming?

In brief — and maybe by a not-so-professionally written definition — functional programming is a programming paradigm where you build your programs by applying and composing functions without explicitly describing the flow control — avoiding shared state, mutating data and side-effects. It is a particular kind of declarative programming.

Functional versus OOP

Functional programming is often defined in opposition to object-oriented programming — or its broader set: imperative programming — , and it is suggested that you have to choose between them. That is misleading as these approaches are not mutually exclusive, and while you make your application based on OOP you still can use the functional approach in certain parts to solve different problems in it.

I have to admit it, the other way around is rare. Hardcore functional dudes like to stay pure. ;)

Which one is the best?

Many of us was taught that OOP

  • is the weapon of choice to fight against the so-called “spaghetti”, the poorly written, badly structured code.
  • And it is the best and most logical way to build up your applications as it models the real world.

Then you find some articles or meet some FP-folks who state that functional (or declarative programming)

  • helps you to write code in a logic which is more close to human thinking (even more if they are mathematicians),
  • and your code is more reliable,
  • easier to maintain and refactor or even test it,
  • changes are often easier to implement,
  • and they are more resistant to bugs,
  • and it improves the quality and predictability of your code.

But where’s the truth?

So which one is the winner? Let’s stay rational. Neither of them is the silver bullet. They all have their advantages, drawbacks and limits. I think that the winner is the open-minded, passionate developer who dives into each paradigm and has the (infinite) time and energy to master their knowledge in either way, and has the possibility and judgement to choose and apply the right tool to solve their particular problem.

But I’m just here now to promote functional programming, so let’s see..

Declarative and Functional

In these articles I often use functional and declarative terms as well. But please note, as Functional Programming is a subset of Declarative Programming, everything which is applicable to Functional is also valid to Declarative, but it is not necessarily true vice-versa.

Why I like FP

Talk less do more

I’m an introvert. I don’t like to talk too much (okay, there are some exceptions eg. movies, music, books, some science topics and bad puns), and I really-really hate to say or listen to unnecessary (needless?) things or say out loud the self-evident (or the ones I think are self-evident). They make me nervous inside. And I like to code and solve problems. And if I have an idea I like to see it working (or failing) in less time and by the least words as possible. and FP helps you to talk less and do more:

  • with writing less boilerplate,
  • telling what to do and not telling how to do it step by step,
  • without the necessity of introducing temp variables,
  • without the need of breaking out from code blocks and making logical jumps from one part of code to another,
  • and without multi-level horizontal pyramids built by conditional statements.

Other key points

  • Less unexpected behaviours and hard to follow processes by neglecting inner state changes (easier to debug).
  • Immutability, thread safety, easier to implement and handle parallelism and concurrency.
  • Avoiding side effects, or keep and handle them in their place, and — maybe even more important — in their time!
  • Lazy evaluation.
  • Composing and combining functions. Functions as values.
  • It gives you another way of thinking and point of view (even if you’d rather stay in imperative programming).
  • And I really like For Comprehension :)
  • And of course, Options!

So come with me, let’s see how to talk to this tiny freaky creature!

--

--