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

Alfredo, Games Solutions Architect

diciembre 18, 2019

Functional Programming has been getting more and more attention over the last few years. But what is it 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?

Three years ago we decided to upgrade the architecture of our Poker product, and we considered functional programming as an option to move forward. We needed to understand the reality behind all the hype, to determine whether it was the right move or not, both from a technical and from a business perspective.

There was plenty we liked about it, but we all agreed on four very significant aspects:

  • Immutability orientation
  • High level of abstraction
  • Much easier parallelism
  • Simpler and faster testing

In classic object oriented programming, instances are generally stateful and can be accessed and modified by concurrent threads. This is what is known as shared mutable state, which is widely regarded as the root of all evil. It can (and will) lead to unexpected state, unpredictable side effects, and unanticipated errors. Keeping shared mutable state under control is not easy; large applications tend to get more and more complex as they grow.

Functional programming is heavily oriented to immutability, which means that by default object instances cannot change and functions don’t have side effects — they simply calculate values (as opposed to OOP, where objects are normally mutable and methods almost always introduce side effects). Of course we need side effects for our software to be able to do something other than calculate values, but what tends to happen is that with FP, mutable state and side effects are well delimited and reduced to the bare minimum, and the rest are pure functions offering different views of your data.

Yes, you can have immutability and pure functions with OOP, particularly now that object oriented languages are quickly adopting functional principles. But it does not come naturally — you have to carefully (and repeatedly) enforce it, making your code complex and verbose. In the end, what typically happens is that in OOP projects you end up having plenty of mutable state and side effects, and maybe a few pure functions here and there. Functional languages are built with immutability in mind and this and other principles do not need to be artificially enforced.

In our next article we will take a closer look at abstraction, parallelism and testing.

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.