Redirecting to https://www.makingdatamistakes.com/a-little-functional-programming-goes-a-long-way/ - click if you are not redirected.


Note: You will be redirected to the original article. A local copy is included below for convenience.

This article is part of a wider series on ‘how to make your software team much more effective’, and focuses on programming style, design and architecture.

Our goal as programmers is to write small pieces of code that combine together to do something useful:

In other words, we want our code to be modular, so it’s simple and reusable. Pretty much everyone agrees on this as a goal. We hope that this will enable teams of people to build large, complex systems quickly and correctly, and to modify them as requirements change.

Both object-oriented and functional programming styles offer this promise, but their approach to modularity is very different.

Side note: If you don’t know the difference between object-oriented programming and functional programming, then you should first learn about both techniques and get some practice with them before you can form a rich opinion about which to use and when. (See resources at the bottom of this article).

Prefer functional over object-oriented programming

After playing with both object-oriented and functional approaches over many years, I lean towards a functional style, though I’m not a zealot. Most of my coding nowadays is in languages that make it hard to write purely functional code (Python, Javascript, C#), but a little goes a long way. More concretely, this is what I mean, and why:

https://xkcd.com/1270/

The goal is to write smaller, simpler, de-coupled functions that you can flexibly compose.

If you’re interested but not yet convinced, and would like to learn more about functional programming:

To sum it up:

Our goal as programmers is to write small pieces of code that combine together to do something useful. Both object-oriented and functional programming are abstractions designed to help us. I’m pretty sure there are problem domains that are a better fit for one or the other, and certainly some languages pretty much make the decision for you.

But if you have the choice, write pure functions because they are easier to test, and testability is the single best indicator of whether something’s a good idea. And avoid classes, because they are complicated and harder to flexibly compose.

Resources:

What is object-oriented programming?

What is functional programming?