This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging
It is often said on the Internet that programmers eat youth, 996 007 emerge in endlessly, more and more inside roll, the Yangtze River after the wave pushes the wave before, more and more people join the army of the Internet. So how can you avoid inner rolls? Of course, the company needs you, can not leave you, you can safely end their iron rice bowl. As a programmer, regardless of solid programming knowledge (various language frameworks), it is also important to maintain good programming habits. Mr. Lu Xun once said, since we can’t beat then let’s join. I’m going to use my JavaScript skills to talk about how to write high-quality elegant code,
Example 1: When a condition needs to be judged multiple times
For example, if there is a name, if it is equal to zhang 3, Li 4 or Wang 5, we output that this person already exists, otherwise the output looks for no such person
Const nameArr = [" arr.includes(name) "] if(arr.includes(name)){console.log(" this includes ") else{console.log(" this includes ")}Copy the code
From the above code we can see that we need to define an extra array, which takes up too much heap memory, and the code is too short to show our technique, and the semantics are not clear. We can change it to something like this:
If (name = = = "* *" | | name = = = "bill" | | name = = = "detective") {the console. The log (" there have been a person ")} else {the console. The log (" no such person ")}Copy the code
Now our code is very clear, other people can read our code, increased awareness. And the code is much longer than before, showing the complexity of the logic. That’s when you say to someone, “It’s a long code.
Example 2: When you need to define multiple values
Const [name,age,address] = const [name,age,address]Copy the code
This kind of code is too short, the leader see also think you lazy! So we should define them separately, and we should keep our variable names concise
< span style = "margin-bottom: 0pt; margin-bottom: 0pt;Copy the code
This way, the amount of code you write and no one knows what the variable names mean except you can largely avoid optimizations. If the later maintenance, you can only come personally. (Maybe you don’t know either, but that’s ok. Deep knowledge is hard to understand.)
Example 3: When a value needs to be retrieved from another value
Select * from name where age = 1; select * from name where age = 2;
Const info = {" zhang SAN ": 18," bill ": 19," detective ":" 20 "} the console. The log (info [name] | | 'no such person')Copy the code
We can optimize it:
Switch (name) {case "three ": console.log(18) break; Case "1 ": console.log(19) break; Case "king 5 ": console.log(20) break; Default: console.log(' no this person ')}Copy the code
In fact, there is a better way to write:
If (name === "log "){console.log(18)}else if(name ===" log "){console.log(19)}else if(name === "log "){console.log(20)}else if(name ===" log "){console.log(20) }else{console.log(' no person ')}Copy the code
Example 4: Determine whether both values are true
For example, if two values are A and B, the output succeeds if both values are true; otherwise, the output fails
If (A & B) {the console. The log (" success ")} else {the console. The log (" failure ")}Copy the code
As you can see from the example above, even though we’re making A judgment about both A and B, it’s an implicit judgment, or it’s not obvious, and it’s hard to understand if someone else takes over, so we need to write it this way: make A judgment about A and B in turn
If (A){else if(B){console.log(" success ")}}else{console.log(" failure ")}Copy the code
Example 5: Determine whether at least one of two values is true
For example, if there are two values A and B, the output succeeds if at least one value is true; otherwise, the output fails
{if (A | | B). The console log (" success ")} else {the console. The log (" failure ")}Copy the code
{if (A) the console. The log (" success ")} else if (B) {the console. The log (" success ")} else {the console. The log (" failure ")}Copy the code
This makes the meaning of the code particularly clear
Example 6: Don’t use other types of methods or convert to other types to manipulate data. Good people write their own methods
For example, if the value of a string STR is abcdefg, you need to add “-” between each string and output the converted value
const str = "abcdefg"
console.log(str.split('').join("-"))
Copy the code
Here we use the split method to convert to an array, and the join method with “-“. It’s easy to get confused by this repeated jumping, so let’s try using the array method
const str = "abcdefg"
console.log(Array.prototype.join.call(str,"-"))
Copy the code
What??? Can’t a good programmer write his own methods? Why should he use them? That would be mediocre. We should write our own methods
const a = "abcdefg"
function test(params) {
const len = params.length
let b = ''
for(let i = 0 ; i < len; i++){
b = b+params[i]
if(i<len - 1){
b = b+"-"
}
}
return b
}
console.log(test(a))
Copy the code
In this way, we can use our own method to achieve the function, without the need to convert the array and concatenation into a string, also do not need to use the array method to complete the function. Let the leader think you have strong ability, the key is a lot of code, more distinctive, skilled. Are you a loser?