top of page

Dipping Your Toes in the Functional Programming Sea


My journey with functional programming started not too long ago. As I was completing my onboarding process at Wix I was recommended a JS functional programming course.


From that moment on I keep on learning new ideas and approaches to software development.


In this short post I’ll share my insights and thoughts about the new and exciting world that I am discovering.




What is Functional Programming?


When I first heard the name functional programming I immediately thought about… well, functions. I use them, I like them, they like me, so am I a functional programmer? In short the answer is no (not yet), but the real answer is more complicated than that.


I will not bore you with the wikipedia definition. Instead, I’ll share my way of thinking about it.


Functional programming is a programming paradigm, meaning that it is a way of thinking about software construction.


There are many ideas and tools in functional programming, but at the base of it is writing more declarative code and reducing the amount of side effects and mutation of state in the application.


The paradigm is based on proven mathematical principles, which stand as guarantees that our code will behave in a certain way.


Let’s take a quick peek into these important principles.



I want you to understand my code


My biggest problem as a programmer is to understand what the hell the code that I am reading is meant to do. To understand the code that I wrote a year ago, 3 months ago and, let’s be honest, even a week ago. In a worse case scenario you come across that scary “legacy code” in a project that was written in the last century and no one has any clue about how that code is still working.


With Functional programming principles we can be more declarative, write more readable and maintainable code.


Let’s take some good old imperative code, for example - what does it do?



That is not too complicated but there are some global variables that we need to keep track of, and we need to keep in mind the imperative looping mechanism of the “for” loop.


And now a declarative version:



Here I am using the Ramda (ramdajs.com) JS functional library. A short explanation about the functions I have used from Rambda:



_.curry


A function that converts regular functions into super functions. The super function we get receives one argument at a time, running the original function upon receiving the argument.

“Curry” is really a super function. It is super useful in the funcional world.

This function lets you create a more specialized function from generic one.


In our example we use curry on an anonymous function. What we got back is the “isRichDog” function.

This function get’s one argument at a time, each time returning a new function, When it gets the last argument it executes.


So when we use “isRichDog(2)” we get back a new function which “saves” the value 2 as “minTreats” and will execute when it will get the “dog” argument.


This is a great way to create more specific function from generic ones



_.compose


“Compose”, like the name suggests, is a way to compose functions together. In our case we composed “filter” and “map” together to get a new function which is running those functions under the hood against the input it gets (the cute dogs).


It’s a very high level look at these important functions. I would recommend reading more about them in the resources section at the end of this article.


Now that we understand what “curry” and “compose” are, we can start seeing the benefits of using a more declarative style.


There is no need to keep track of the global variables that can change the state of our code. There is no need to keep track of looping - it’s all abstracted by functions like “map” and “filter”.


We can look at the findRichDogs function and understand easily what it does.


First we filter in all the dogs that have more than 2 treats, and after that we map that list to the name of the dogs.


And BOOM - we now have all the rich dogs in town!


It might look a little bit confusing if you never wrote any functional code before but with a little bit of practice this kind of code becomes much more readable than the imperative approach.





Oh no, my app crashed (Side Effects)


What are the side effects? Is it only mutating variables? Umm, no. Writing to a DB is a side effect. Making an HTTP request is a side effect. Writing to the screen is a side effect. Everything we do as programmers, in the end, is to create side effects (we are side effect monsters).


In functional programming the idea is not to remove side effects altogether (because then our program will simply do nothing), but to push them to the boundaries of our programs. We want to contain our side effects, which will help us handle and find bugs in our programs more easily.


Congratulations, you got hired to work in a bank and you come across this lovely piece of code:


You probably saw function calls like that before, ones changing values outside of its scope.


This is a simple example - but imagine this file has 3000 lines of code and you need to go over it all and find why there’s $300 missing from a user’s account. Good luck!


But with the help of functional programming rules we can reduce those side effects, for instance, by using function calls with the direct input and output:



We can see that we still have the side effect in the save function, but we reduced the overall side effects by making the pay and getMoney functions not to mutate a variable and instead return a new one with the changes we wanted applied.


Let’s hope that now we saved the next person 300$.



How will this help me?


From personal experience at my day-to-day job at Wix - functional programming concepts can be very handy. We try to keep our code declarative (oh boy this is not an easy task), which helps us understand our large codebase.


Side effects are always hard to manage, as we still get some nasty little bugs because of them. But as we recognise the places with side effects and contain them, we avoid those illusive bugs in the future.



So what’s next?


If you are convinced that there is a great benefit to writing more functional code, and you want to learn more, especially while using JS, I would recommend a great course by the awesome Kyle Simpson (getify): https://github.com/getify/Functional-Light-JS.


Or, if you are more of a video course person, there is also a course on Frontend Masters: https://frontendmasters.com/courses/functional-javascript-v3/


If you want to learn a new language to get into the Functional world I would recommend ELM: https://elm-lang.org/

Which is just wonderful at explaining functional concepts.


 

This post was written by Matan Cohen


 

For more engineering updates and insights:

bottom of page