A pragmatic look at functional programming: abstraction, parallelism and testing (Part 2-3)

TWS Avatar

Alfredo, Games Solutions Architect

In the previous article we covered the immutability orientation. While investigating the advantages of functional programming languages, we also learnt that it offers a much higher level of abstraction. It requires a radical paradigm shift and it can be shocking at first. But hey, moving from imperative to object oriented programming had a learning curve too, and it totally paid off. Writing functions that might take functions as parameters and return different functions isn’t exactly trivial, but once you are a bit fluent with these higher order functions, composition, monads, functors, etc. you will be able to do much more with less. You will find yourself coding in a much more declarative fashion, that is, declaring what you want rather than specifying exactly how to achieve it.

Turns out that once the majority of your code is made of pure functions operating on immutable objects, concurrency poses no risk and parallelism is almost trivial, as opposed to shared mutable state that requires careful synchronisation of critical blocks and explicit forking and joining of threads.

As for testing, in the usual OOP scenario where you have mutable objects that affect each other, you need to mock everything around the object you are testing, set its initial state, set expectations on the mocks, invoke some methods on your object, verify that its final state is correct and that all mock expectations where met, plus make sure that no unexpected side effects were produced. But when you are testing a pure function you only need to feed it some values and verify the result.

In the next article we will share our experience developing a project entirely in Scala.

About the author
Alfredo is our Games Solutions Architect. With more than twenty years of experience in software development, he has worked in a variety of sectors such as localization, data mining and video streaming. He is now passionate about online gaming development and a strong advocate for functional programming.

Share this article

Next Articles

Create a new workspace from scratch using NX
January 14, 2020

Create a new workspace from scratch using NX

Have you ever been in the position of starting a new app from scratch? Choosing the right set of tools, libraries or frameworks can be a pain...
Creating a Navigation Your Users Will Love
January 05, 2020

Creating a Navigation Your Users Will Love

Navigation is the backbone of all software, website and app designs.
A pragmatic look at functional programming: The immutability orientation (Part 1)
December 18, 2019

A pragmatic look at functional programming: The immutability orientation (Part 1)

What is functional programming exactly? Is it just a bunch of new programming languages? Is it a different way of coding? Can it really make a difference to our business?