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 third article of this topic, here is the portal of the previous article: How to write high quality code (a), how to write high quality code (b) if it is helpful to you, you can point wave attention, we make progress together!

3. Pure functions

3.1 Definition of pure Function:

A function whose “return result” depends only on its “parameters” and which remains the same no matter how the external environment changes and has no side effects during execution is called a “pure function”.

3.2 Characteristics of pure functions:

1. Relatively independent - It's easy to move, refactor, and reorganize your code, making it more flexible and adaptable to future changes. (Execution results will not be affected by external changes) 2. There are no side effects during function execution. (Does not affect the external environment)Copy the code

3.3 Application Examples:

	// This is an impure function
	let adultAge = 18;
    let isAdult = (age) = > {
        return age>=adultAge
    }
	console.log(isAdult(20))//true
    AdultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge = adultAge
    // In development, we should write pure functions as far as possible, which is conducive to modularization of the code to improve the maintainability and flexibility of the code

    let isAdult = (age,adultAge) = > {
        return age>=adultAge
    }
	console.log(isAdult(20.18))//true
    // This kind of pure function does not depend on external variables, and does not affect external variables, input code in the world of independent youth, modular development world, very popular;
    // Improves code reusability, maintainability, and flexibility.
    // Guarantees that the same output will always have the same output
    
Copy the code

Looking at this, you might ask, what are the side effects of a function? I’ll introduce you in a minute.

4. Functional side effects

4.1 Meaning of side effects of functions

When a function is called, the called function has additional effects on the calling function in addition to returning the value of the function. For example, when a function is called inside the function being called, the value of a global variable is changed, the value of a variable declared outside the function is changed (usually through pointer arguments), and so on.

This side effect is obviously not conducive to the implementation of modular development ideas, in the development process, we can minimize the side effects of functions, control the uncertainty in the code, to improve the maintainability, reuse, flexibility of the code, so as to improve the quality of the code.

So how can we avoid the side effects of functions in real development? Next, I will introduce you:

4.2 How to Reduce Function Side Effects (Dependency Injection)

	// For example, the requirement is to get the current time in a string format concatenated with a greeting:
	// Impure functions generally have function side effects, such as:
	let getRespect = (say) = >{
		let cTime = new Date().toISOString();
		return cTime+say
	}
	console.log(getRespect('Good morning~')) / / the 2020-09-16 T13: no. 570 zgood morning ~
	// The same input does not produce the same output
    
    // So, in order to "control the uncertainty", we can use a small trick: dependency injection (extract the impure parts of the function as parameters)
    
  	let getRespect = (d,say) = >{
		let cTime = d.toISOString();
		return cTime+say
	}
	let d = new Date(a);console.log(getRespect(d,'Good morning~')) / / the 2020-09-16 T13: no. 570 zgood morning ~This ensures that the output of the function depends only on the input parameters, reducing the uncertainty of the code. At the same time, the impure code is moved to the smaller function, so that it is far away from the core code, easy to centralized management, but also let the core code behavior become predictable!Copy the code

4.3 Summary of common techniques to reduce side effects of functions

1. Use parameter operations in function entry, but do not modify it; 2. Do not modify variables outside the function inside the function; 3. The result of the operation is returned externally through a function.