“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
1. Strictly follow the coding standards of the company
If you are still in the learning phase, you can start by following the big factory code specification
Netease Netease Concave-Bump LABS Jd.com Airbnb Alibaba
2. Limit the scope of variables
Use let and const instead of var to avoid contamination of global variables.
Limit the number of parameters passed in
As the business needs of our company increase, the number of parameters passed in May also increase, and the complexity of the function increases geometrically due to too many parameter combinations.
So we generally limit the maximum number of arguments to three. If there are more than three, we can simply package them in objects and pass them in.
4. Simplify conditional expressions
Just to give you a small example, there are a lot of usage scenarios that you can work on yourself.
Business logic: Student number (NUM) >200 and school (school) is Denver primary school students are girls.
function isGirl(num,school){
if(num>200&&school==="Gold Nuggets Elementary School") {return true;
}else{
return false; }}Copy the code
simplified
function isGirl(num,school){
return num>200&&school==="Gold Nuggets Elementary School";
}
Copy the code
A function does only one thing
A function only needs to care about one thing
Business logic: Determine whether the company made a profit or a loss this year
/ * in teamwork, other members have a demand in its business need to know whether the profit of this year, if you only see isProfit () function of literal meaning you use words (did not inform the function have send email to the boss's behavior) will lead to the puzzling email quota is used up, and sending repeated mails more serious accident for the boss. * /
function isProfit(sum,cost){
let result = sum-cost;
if(result>0){
(x)send true to Boss by email;// Don't take illicit goods
return true;
}else{
(x)send false to Boss by email;// Don't take illicit goods
return false; }}Copy the code
If the company has indeed judged whether it will be profitable this year and informs the boss of the need, this can be modified
// Send a message to the boss
function sendEmail(result){
send result to user by email;
}
// Determine whether it is profitable
function isProfit(sum,cost){
return sum-cost>0;
}
// Judge whether it is profitable this year and inform the boss
function isProfitService(sum,cost){
let result = isProfit(sum,cost);
sendEmail(result);
return result;
}
Copy the code
Core ideas:
- Function names should be consistent with the behavior
- Two things must be done, and the function should be named with XXX +Service() to remind the function that there might be two events
Six, early return
Overuse of if… else if… Else nesting greatly increases the difficulty of reading the code and reduces the maintenance efficiency of the project
Do you look tired? Is it logical?
function start(num){
if(num<50) {if(n ! = =0) {if(n ! = =1) {return start(num-1) + start(num-2);
}else{
return 1; }}else{
return 0; }}else{
return "Game Over"; }}Copy the code
Using early return can solve this problem to some extent
function start(num){
if(num>=50) {return "Game Over";
}
if(num===0) {return 0;
}
if(num===1) {return 1;
}
return start(num-1) + start(num-2);
}
Copy the code
❤️ Thank you all
If this post is helpful to you, give it a thumbs-up. Your thumbs-up are my motivation.