Functional Programming in C++

How to improve your C++ programs using functional techniques

Vanand Gasparyan
Computer Science Reader
6 min readNov 15, 2019

--

Functional programming is a programming paradigm, unfortunately, a not so popular one. Many of you (assuming you’re anywhere near C++) probably have heard that C++ is a multiparadigm language, but do you really feel that? If not (or you didn’t even know that), I’m really glad you’re here.

OOP (Object Oriented Programming) is yet another programming paradigm, a very popular one. It’s not so popular for no reason: it has lots of advantages, most of which are just perfect from the business perspective. As a result, it’s become so popular and demanding that most C++ language courses will jump to the OOP right after the language basics and totally ignore the functional aspect of it. “Why bother? They’re not going to ask that at the interview, better learn SOLID instead”. It’s embarrassing how long the articles about SOLID sometimes are. It’s embarrassing that DRY is a principle.

BTW, I’ve got a new one for acronym lovers: WHAT (Wash Your Hands After Toilet). I don’t see how this is less of a common sense than the DRY.

By no means, I refuse the effectiveness and usefulness of OOP, but it’s so boring. The only challenge there is to come up with good architecture. A really good one enables the company to hire a bunch of develosers who know SOLID and DRY by heart and that’s enough to enhance and maintain the code, something like this:

It is dumbed so down that for the majority of people from other OOP-perfect languages some of the letters in SOLID are not “principles to adopt” but “frameworks to use”. In my personal, very biased opinion this is no programming. I’m sincerely happy for those people who ended up being architects in the boring world of OOP: their job is truly challenging, very interesting and deserves recognition.

But if you’re a C++ programmer stuck in the land of boredom, don’t worry, there’s a way out. This book is your red pill.

For the record, this is not an OOP vs FP dilemma. There are great languages that are perfect for FP and, likewise, there are languages perfect for OOP, and the choice of the language very much depends on the problem. The first chapter of this book explains what exactly FP is good for and also, where it is not even applicable. But the best thing about C++ is that it supports both. Currently, it’s not the most comfortable language for FP out there, but that’s going to change soon with the ranges in C++20. And so, being a C++ expert you have the amazing opportunity to use the best of both worlds.

So, who is this book for? First of all, it’s a must for experienced enough C++ programmers. If you consider yourself a C++ enthusiast, this book will provide a big flow of fresh air. But even otherwise, FP is a very important aspect of the language. As Bjarne Stroustrup, the creator of the language said during a podcast:

A language like C++ is not for everybody. It is generated to be a sharp and effective tool for professionals… You don’t have people doing calculations without understanding math.

-Bjarne Stroustrup

C++ is like a swiss army knife, and if you don’t know (and never cared about) the FP side of it, your C++ expertise is questionable. And also the chances are you’re not very comfortable with Data Structures and Algorithms. I’m not saying there is a causality, more a correlation.

Douglas McIlroy

If you’re not familiar with FP and its concepts good enough you won’t have a clue where you could have used it and how much better it would’ve made your code and life. I’ve seen how people tend to overthink and complicate simple things just because OOP is the absolute truth (the only thing they know). To prove my point, I recommend reading the beginning of the 4.3 Function composition section of this book. It’s an interesting story about Donald Knuth and Douglas McIlroy, the father of the Unix pipeline.

If you’re a C++ beginner, you’ll probably get bored with this book and won’t finish it. I’d suggest you read this book right after you’ve read and understood this one: Effective STL — Scott Meyers.

If you’re more than a beginner, get this book right now.

Contents

The first couple of chapters are an introduction to the FP in general and an overview of some important language features and STL algorithms. Then we have chapters about some fundamental FP concepts and techniques, like currying, purity, laziness, monads.

And finally, we get to the ranges. As you should know, currently they’re not part of the standard library, but they’re already accepted to be in the C++20. However, the standard proposal was based on the range-v3 library by Eric Niebler, which is completely available and usable (for C++14/17). Knowing about the pipe operator only won’t be enough to use the ranges effectively and fluently: a proper FP mindset is necessary. Thus, the timing of this book couldn’t be better.

The Template metaprogramming chapter is a fresh, interesting view on one of the fundamental and valuable aspects of C++. People who appreciate the elegance in the theory will enjoy it. Unfortunately, TMP is somewhat scary to the beginners, and some of them tend to procrastinate on it with the hope that they’ll be fine without knowing it. And in some cases that might be true. I’ve personally met “C++ professionals” with years of experience and surprisingly little knowledge of TMP. That’s just wrong (see Stroustrup’s quote above).

The last two chapters, Functional design for concurrent systems and Testing and debugging, are crucial to totally appreciate FP. Not only they show valuable techniques to make your code concurrent and test it but they also highlight the huge advantage of the FP when it comes to these, especially over OOP. Nevertheless, some of the lessons from these chapters relate to OOP too. Most importantly, they’ll help you understand the practical and philosophical difference between FP and OOP, where their limits are and how they can be harmonized to get the most out of C++.

Final score

Useful for programmers: 10/10

Relevance: 10/10

Real-world examples: 10/10

Detailed and friendly: 10/10

Worth buying: 10/10

Must Watch: The link to the podcast with Bjarne Stroustrup (Bjarne Stroustrup: C++ | Artificial Intelligence (AI) Podcast).

--

--