this is endless

this is endless. this perpetual sadness corners me to a quieting quiver to a chill that’s artful and comforting finally i become a winter night in june.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




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 read a recommendation of JS functional programming course.

From that moment and up until now i keep learning new ideas and approaches to software development.

In this short post I will share my insights and thoughts about the new and exiting world that I am discovering.

When I first heard the name functional programming I immediately thought about… well, function. 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 to think 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.

This paradigm is bases on proven mathematical principles, which give us guarantees about the way that our code behaves.

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

My biggest problem as a programmer is to understand what the hell the code that I am reading is meant to do ,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 that you come across that scary “legacy code” in a project that was written in the last century and no one has a 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 for example some old good imperative code , 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 JS functional library :

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 functional world.

This function lets you create a more specialised 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 function under the hood against the input he gets (the cute dogs).

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

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

Now 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 , they are abstracted away by function like “map” and “filter”.

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

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

And BOOM we got all the rich dogs in town!

It might look 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.

What are side effects , are they 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 effects , everything we do as programers in the end is to create side effects(we are side effects monsters).

In functional programming the idea is not to remove side effects all together (because than our program will simply do nothing) but to push them to the boundaries of our programs. we want to contain our side effect which will help us to 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, changes values outside of it’s scope.

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

With the help of functional programming rules we can reduce those side effects , the most basic one is to use function calls with direct input and direct output .

We can see that we still have side effect in the save function , but we reduced the overall side effects by making the “pay” , “getMoney” to not 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$.

From personal experience from 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 help us understand our large code base.

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.

If you are convinced that there is a great benefit to write more functional code, and you want to learn more, especially while using JS, I would recommend a great great course of the awesome Kyle Simpson(getify). find more of his content in the link below:

Or, if you are more of a video course person there is also a course on Frontend Masters in the link below:

If you want to learn a new language to get into the Functional world I would recommend ELM:

Which is just wonderful in explaining functional concepts .

Add a comment

Related posts:

Arizona Driver Busted After Using Skeleton Passenger In Carpool Lane

It was quite a sight to see, when an officer from Arizona pulled over one man driving down the road with a plastic skeleton sitting in his passenger seat. The 62 year old man driving was illegally…

Hackers are destroying the NFT market

The recent growth of hacks on NFT projects has some believing there is a much more comprehensive network involved in all of the drama. Two web3 security firms issued reports recently focusing on the…