preface

As a big front-end developer, after passing the initial development level, is there often a confusion: My code does meet the development requirements, but it always feels that there is a more “elegant” solution, always feels that there is some redundancy, always feels that the maintainability is not so strong, always feels that there are some bugs but I can not detect them in the practical operation of functional testing; So the question is, are there methodologies that have been developed to make our own testing more comprehensive and granular, so that our code can be visually improved both in terms of readability and maintainable? B: of course! Before I did not understand, also confused, after a period of learning, summed up the following points, please let me tell you:

This is the second article on this topic, followed by how to write high quality code (a), if it is helpful to you, you can point wave attention, we make progress together!

2. Super functional Programming

2.1 Meaning of Functional programming:

Functional programming is a “programming paradigm”. It is a style of constructing the structure and elements of computer programs. It treats computation as an evaluation of mathematical functions and avoids state change and data variability.

In short, functional programming, as a “programming paradigm,” is a “methodology” for how to write programs. It is a form of “structured programming”, where the idea is to “write operations as a series of nested function calls”.

Meaning:

Before we look at functional programming, it’s worth taking a look at the programming paradigm that’s been with us since we started learning code, and that’s a beginner’s friend: “Imperative programming.”

“Imperative programming “: detailed instructions to the machine on How to do something that you originally wanted to do. (Personally, this is the guiding principle behind our dumb code, and the reason for our poor code quality, reuse, and maintainability.)

Here I will use the most down-to-earth way to show the difference between the two in solving real problems, show the charm of functional programming, and open your code “reinventing” the first step!

Limitations of imperative programming:

// If I want the array after +3 for each element in the array;The idea of imperative programming is to know the code below://
let arr1 = [1.2.3.4.5];
let arr2 = [];
for (let i = 0; i<arr1.length; i++){ arr2.push(arr1[i]+3)}console.log(arr2) //[4, 5, 6, 7, 8]

// This code does not have any reusability, but only relies on a single command to achieve our single requirement; What if we're going to do both +3 and +5 for arr1 at the same time, you can't write the loop again, right?
// under the guidance of imperative programming, we can use functions to fulfill our requirements :(dumb code 2)
let arr1 = [1.2.3.4.5];
let changeArr = (addNum,arr) = >{
    let arr2 = [];
    for (let i = 0; i<arr.length; i++){ arr2.push(arr[i]+addNum) }return arr2
}
let arr2 = changeArr(3,arr1);
let arr3 = changeArr(5,arr1)
console.log(arr2,arr3) //[4, 5, 6, 7, 8],[6, 7, 8, 9, 10]

// This way, the logic structure is clearer than the previous one, a little more reusable, but also "not flexible enough!!"
// What if we need to add each element of the array to +1, each element to + 2, each element to *3+4 to get three arrays?Imperative programming ideology under the guidance of such a demand can not be elegant to solve the problem, at this time, rushed out a "handsome and elegant" to the explosion of the little brother, he called "functional programming".Copy the code

Magic of functional programming:

	// Here, no matter what we do to each item in the array, we only need to call the same function.
	let arr1 = [1.2.3.4.5];
    let changeArr = (arr,fn) = >{
        let arr2 = [];
        for (let i = 0; i<arr.length; i++){ arr2.push(fn(arr[i]))// you can do anything!! Do what you want
        }
        return arr2
    }
let arr2 = changeArr(arr1,(item) = >item*3+5);
let arr3 = changeArr(arr1,(item) = >item-3);
console.log(arr2,arr3) //[7, 10, 13, 16, 19]
Copy the code

The refinement of the core idea of functional programming

Obviously, the ultimate reuse of functional programming, when you’re dealing with the same problem, is the reuse of whatever you want. How do you do that?

Analysis:

Imperative programming, is to translate each step of the operation into logical code execution, he generally in terms of reusability, is to reuse the parameters passed in, but for logical changes, he is a little weak;

Functional programming, it is logic, we need to reuse the key steps of abstraction refinement, such as at the top of the key steps, for each element in an array, a operation, end up with a new array, we are operating a this time, as the incoming parameters, only need to any operation while doing any operation function as a parameter requirements can be realized, Make the function flexibility, reuse greatly enhanced!! Complete the transformation of the elegant realization of demand.

Well, this is the second section of the high quality code Learning notes, if it helped you, give a thumbs up. Later chapters will continue to cover in detail how to write high quality code, a wave of attention, together with progress