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

Alfredo, Games Solutions Architect

febrero 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

Contacta con nosotros

Si tienes alguna pregunta, simplemente completa el formulario y nos pondremos en contacto contigo.

Responsable: The Workshop Technologies Ltd. Finalidad: Prestar los servicios ofrecidos a través de la web o atender otros tipos de relaciones que puedan surgir con The Workshop Technologies Ltd como consecuencia de las solicitudes, gestiones o tramites que el usuario realice mediante la web. Legitimación: Consentimiento del interesado según lo dispuesto en el Reglamento (UE) 2016/679 y la LOPDGDD 3/2018. Destinatarios: Fichero interno automatizado de The Workshop Technologies Ltd y terceros para el desarrollo, mantenimiento y control de la relación jurídica que se establezca cuando exista autorización legal por el usuario para hacerlo. Derechos: Acceso, rectificación, cesión, oposición y supresión. Información adicional: Puede obtener toda la Información adicional y detallada que precise sobre el tratamiento y protección de sus datos personales en el enlace Política de Privacidad.

Somos de

¿Te gusta lo que ves?

¡Únete a tus compatriotas o añade una nueva bandera!

¡Gracias!

Tu mensaje fue enviado. Nos pondremos en contacto contigo en breve.