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

Alfredo, Games Solutions Architect

February 14, 2020

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

Contact Us

If you have any questions, just fill in the form and we’ll get back to you.

Controller: The Workshop Technologies Ltd. Purpose: To provide the services offered through the website or to handle other types of relationships that may arise with The Workshop Technologies Ltd as a result of the requests, procedures, or formalities performed by the user through the website. Legal basis: Consent of the data subject as provided in Regulation (EU) 2016/679 and the LOPDGDD 3/2018. Recipients: Internal automated file of The Workshop Technologies Ltd and third parties for the development, maintenance, and control of the legal relationship established when there is legal authorization by the user to do so. Rights: Access, rectification, transfer, opposition, and deletion. Additional information: You can obtain all the additional and detailed information you need about the processing and protection of your personal data at the link Privacy Policy.

We Come From

Like what you see?

Join your comrades or add a new flag!

Thanks!

Your message was sent. We will get back to you shortly.