Why use a pure function and pure function

What function is pure?

Pure function is defined:

1. If you call the function parameters are the same, always return the same result. It does not rely upon a program change in any state or function of the external data during execution, must rely only on its input parameters.

2. This function does not produce any observable side effects, such as network requests, data input and output devices or mutant (mutation).

This is a pure function. If a function in line with the above two requirements, it is a pure function. You've probably written a pure function in the past even unintentionally.

Before we study a function of a pure or impure, let's talk about the terrible "side effects."

 

What are the side effects can be observed?

A side effect can be observed that the function of any interaction inside the outside thereof. This may be modified within the function of external variables, or call another function within the function and so on.

Note: If the function is called pure pure function, no side effects is still a pure function.

Side effects from, but not limited to:

• Conduct an HTTP request

•    Mutating data

• output data to the screen or console

• DOM query / operation

•    Math.random()

• Get current time

Side effect itself is not poison, some time is often required. However, to maintain a pure function, it can not contain any side effects. Of course, not all functions need to be a pure function. I will discuss this situation later.

But first, let's look at an example of some of the contrast of the pure and impure functions ......

 

Examples of pure function

The following is a calculation of the after-tax price of product (UK tax rate is 20%) of pure examples of functions:

function priceAfterTax(productPrice) { return (productPrice * 0.20) + productPrice;}

 

It meets the definition of what we call the two pure function. Is not dependent on any external input, external does not change any data, no side effects.

Even if you run this function with the same input operation 100,000,000 it still produces the same result.

 

Non-pure function

We have seen examples of pure function, and now look at an example of pure non-JavaScript Functions (Impure function) of:

There are tax = 20;

function calculateTax(productPrice) {

    return (productPrice * (tax/100)) + productPrice;

}

Pause for a moment and see if you can see why this function is impure.

Wherein the function depends on the results tax external variables, but not pure function dependent on external variables. It does not satisfy the first requirement definition, and therefore this function is impure.

 

Why pure function is very important in JavaScript?

Pure function is widely used in functional programming. And high-quality libraries such as ReactJS and Redux, etc. need to use a pure function.

However, pure function can also be used in the normal use of JavaScript development, not necessarily limited to die in a programming paradigm. You can mix pure and impure functions, Not a problem.

Not all functions need to be pure. For example, the button press event handler can not fit a pure function of the DOM. However, this event handler can call other pure function to process, thereby reducing the number of items in impure function.

 

Testability and reconstruction

Another reason is to test the use of pure function and remodeling.

One of the main benefits of using pure functions is that they can be directly measured. If you pass the same parameters, they will always produce the same results.

At the same time pure function also makes maintenance and refactoring code easier. You can safely reconstruct a pure function, you do not have to worry about messing did not notice side effects caused by the end of the entire application debugging hell. (Translation: if the project is full of side effects, then the logic between functions / modules may be coupled intertwined with each other, may be due to complex and difficult to rely on reconstruction in post new logic, more commonly develop in order to meet demand and continuous introduction the new side effects to the original logic resulting code is getting worse.)

Proper use of pure function can produce more high-quality code. And also a cleaner encoding.

 

Guess you like

Origin www.cnblogs.com/ranyonsue/p/11444992.html