JOY TO THE WORLD

Have you ever gate-crashed a birthday party? Have you ever attended a birthday party where you didn’t know the person who was being celebrated? Let’s imagine everyone was going around sharing why…

Smartphone

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




Pure JavaScript Functions

Writing Pure Functions is the bees knees right now. If you’ve used ReactJS you should be familiar with this concept because react encourages you to write the majority of your components as Pure Functions. Functional programming is just one of the subsets of declarative programming. Declarative programming could be a pretty complicated topic but essentially it’s just a way to make your code more readable to humans, as opposed to imperative programming which could look like a big list of machine instructions. On the surface declarative programming is a simpler cleaner way of writing code that other coders can quickly reason about. Declarative programming cuts straight to the chase and tells the computer what to do. Imperative programming takes the long route by explaining to the computer step-by-step what to do. What makes JavaScript awesome is that we can use it both in a declarative and imperative way, depending on the problem we’re trying to solve.

Characteristics of A Pure Function:

If any of that is confusing these following code examples should clear things up a bit.

Let’s write a function called double which takes in an array of numbers and returns a new array after doubling every item in that array. So if our input is [1,2,3] we would expect our function to return [2,4,6]

Imperative

Declarative

Let’s do one more example to really drive the point home. Let’s write a function called add which takes in an array and returns the result of adding up every item in the array. So if our input is [1,2,3] we would expect our function to return 6

Imperative

Man that’s hideous. Why do you we put ourselves through something like that when we could just do this:

Declarative

Just as important as other humans being able to read our code, writing Pure Functions forces us to think in a more isolated modular manner. We wouldn’t make an API call, modify the response and pass that response to another function all in one function because that goes against the fundamentals of Pure Functions. Since we avoid doing such things, we’re better able to pinpoint bugs and errors in our code. Imagine 90% of your code base written as Pure Functions? Not only do you have to separate out concerns but finding bugs and errors becomes infinitely simpler.

Add a comment

Related posts:

Eco Organizing Tips when Decluttering and Cleaning

Someone asked me if being green in organizing was simply another excuse to hold onto trash and keep clutter. ABSOLUTELY NOT! But, for some people, especially hoarders, it might be a challenge to let…

Osteoporosis Review 2020

Osteoporosis is a common condition, especially in postmenopausal women, but it often remains undetected until after fracture occurs. This article discusses its diagnosis, fracture risk assessment and…

La violencia es un esquema piramidal

Algunas personas consideran que la violencia es una herramienta válida para disciplinar y educar. Para inducir a las personas a tener ciertos comportamientos deseables para la sociedad y alejarlos de…