Adopting Functional Programing with three small principles

Functional Programming can be a game changer for most old time programmers, maybe adopting one concept or two can be the baby steps you’re needing right now

Juan Pablo Rodríguez (ornitie)
To Journal, To Dev
4 min readNov 17, 2020

--

Functional Programming is the new wave of hype coming our way, it actually has been here for a long time now. Programming languages like Haskell and Scala were already pretty strong within their own communities, but other languages like Javascript are taking it to the mainstream community.

Photo by Taras Shypka on Unsplash

Just like every type of migration in software, this should also be done slowly and progressively. So, in this short article, I’m gonna bring you three basic and simple concepts that may help you start migrating to functional or, at least, try to convince you to.

Declarative Programming

So, this is a bit of a stretch, because declarative programming is the door to the functional paradigm. You can summarize declarative programming in a simple concept:

The code should describe what you’re doing instead of how or why you’re doing it

Think about that new desk you bought online because now you have to work from home and you want to upgrade your home office. Well, it is likely that your new desk comes with a manual that tells you how to assemble it. But you do not want (or need) a manual that tells you how each part was manufactured or why does it goes the way it does, you just need to put it together and start working. If you want some extra information on your desk you’ll look for it yourself.

Same thing goes for code, you write code and it should be a set of instructions to a achieve a result (literally the definition of an algorithm). If you want to look up the implementation underneath, you can, but you should not have to in order to understand the code.

Pure Functions

So, once you understand the purpose behind all of this, you know that Functional Programming allows you to be more declarative. Now, to get into the technical details we’ll start with something that I believe most of us have done without realizing it, pure functions.

So the definition of a pure function is a function that, given the same parameters, will always return the same output without changing the overall state of the application. What does this mean? Think about this implementation:

This code sorts a list and returns the first element in Python. Can you spot the problem with this? Well, simply, if you call this function, it will alter the list you’re passing as a parameter, this is not pure as it will alter the overall state of the app and make a mess while debugging as you’ll have to trace who called who and in which order to understand what’s happening.

Now consider this counter example:

This Python script does very similar to the one above with one big difference, the sorted method does not alter the original list, so you can call this function n number of times with that list and you’ll have the same output and the same list all the time, now you don’t have to worry about modifying things outside of your scope.

Immutability

So, now we understand that modifying things outside our scope is bad because it makes tracing much harder. Now, a language that understood this recently (or not so recently, now) is JavaScript with the revolutionary ES6. Modern JS is much cleaner and we’ll see a concept they introduced regarding immutability.

Immutability is the idea that you should not reassign variables, as it mutates them and we return to the same problem than before.

Lets consider a pipeline (a concept that I may explain in a future article), in a few words, a pipeline is taking an object and transform it or enrich it through a series of steps (doesn’t sound very functional when we mention transforming, but give me a chance).

This does not feel functional, you’re taking an order and some products as a parameter and modifying the initial object. Think about how will you debug this, you’ll have to get into each function and see each step to see how the order is being transformed and jump back and forth these two functions, this clearly is not very declarative. So, how do we fix this?

This doesn’t seem much of an improvement, actually, we added constants (ES6 new feature) that will just make a mess, right? Well, not really, as with this constants now you can divide the output of each function into a different variable and you can actually pass the output of one function as a parameter to the next one (that’s a pipeline), now, while debugging you just have to keep an eye on these constants and your life will feel easier.

Final thoughts

So Functional Programming is a deep world, it has many wild concepts and many interesting ones (many of those to overlap). But this is just the appetizer, maybe in a future article I’ll write about those deeper concepts like Currying, Higher Order Functions, First-class Functions, etc. But, for now, I hope this helped you a little bit in some very basic but fundamental concepts on functional programming.

--

--

Juan Pablo Rodríguez (ornitie)
To Journal, To Dev

Desarrollador de Software, con hambre de aprender, con gusto por escribir y con ansias de programar.