The writing order of methods/functions in the code

Cause

Recently, I was watching Clean Code ("The Way to Clean Code "). When I saw the format of Chapter 5, I just noticed a point that has been confused before- the writing order of methods/functions .
So, I was ready to record it.

Vertical approach

When writing code, we are accustomed to dividing it into different methods/functions. On the one hand, it abstracts different levels of code and its meaning, and on the other hand, it helps to improve the maintainability of the code. To put it bluntly, this is also the experience summed up by the predecessors, and this is what I want to learn from Clean Code .

For different methods/functions, there must be mutual calls. Then, in what order should these methods/functions be written? The advice given by
Clean Code is that closely related codes should be close to each other !
What are the benefits of doing this?
It's easier to read and has a clear view without having to move your head and eyeballs more!

Example:

function printCurrentMonth() {
    
    
	const month = getCurrentMonth();
	console.log(`现在是 ${
      
      month} 月份`);
}

function getCurrentMonth() {
    
    
	return new Date().getMonth() + 1;
}

Vertical order

So, do we just need to put the relevant code as close as possible? of course not!

The recommended approach is to show the order of function call dependencies from top to bottom. In other words, the function that is called should be placed below the function that executes the call.

as follows:

function A() {
    
    
	B();
	C();
}

function B() {
    
    
	console.log("这是 B");
}

function C() {
    
    
	console.log("这是 C");
}

function D() {
    
    
	E();
}

function E() {
    
    
	console.log("这是 E");
}

Vertical sequence of multi-level calls

Although most of the previous code was written in this way, it was not specifically followed, and the reason for it was not very clear. After reading this part of the Clean Code , I did understand the benefits of writing this way!

However, there is indeed an answer to the question, and Clean Code does not clearly point it out, that is, if there are functions with multi-level calls, what should the order be?

as follows:

function A() {
    
    
	B();
	C();
}

function B() {
    
    
	D();
}

For the above code, I should in the end Bthe following to write Cit or to write Dit?

So, I rummaged in Clean Code , found some examples from the author, and found the answer: write first D.

as follows:

function A() {
    
    
	B();
	C();
}

function B() {
    
    
	D();
}

function D() {
    
    
	console.log("这是 D");
}

function C() {
    
    
	console.log("这是 C");
}

to sum up

  • Closely related code should be close to each other
  • Show the order of function call dependency from top to bottom, the called function should be placed below the calling function
  • If there are multiple levels of calls, first display all the details of a single called function

Guess you like

Origin blog.csdn.net/qq_37164975/article/details/106590510