1.利用Array.includes
不好的写法:
const func = name => {
if (name === 'Jerry' || name === 'Tom' || name === 'Speike') {
// do somathing
}
}
利用Array.includes
重构:
const names = [
'Jerry',
'Tom',
'Speike'
];
const func = name => {
if (names.includes(name)) {
// do somathing
}
}
2.使用对象代替多个if
或switch
不好的写法:
const getRace = name => {
switch(name) {
case 'Jerry':
return 'mouse';
breake;
case 'Tom':
return 'cat';
breake;
case 'Speike':
return 'dog';
breake;
default:
return 'animal';
}
}
使用对象重构:
const raceObj = {
Jerry: 'mouse',
Tom: 'cat',
Speike: 'dog'
}
const getRace = name => {
return raceObj[name] || 'animal';
}
3.提前return
,而不是嵌套判断
不好的写法:
const getAnimalInfo = animal => {
let animalInfo;
if (animal) {
if (animal.name) {
if (animal.age) {
animalInfo = `Name: ${animal.name},age: ${animal.age}`;
} else {
animalInfo = 'No age';
}
} else {
animalInfo = 'No name';
}
} else {
animalInfo = 'No animal';
}
return animalInfo;
}
重构:
const getAnimalInfo = animal => {
if (!animal) return 'No animal';
if (!animal.name) return 'No name';
if (!animal.age) return 'No age';
return `Name: ${animal.name},age: ${animal.age}`;
}