if-else 语句优化
多条件判断嵌套
单一 if 语句使用 &&
/ ||
进行连接
if (.. && .. && .. && .. )
缺点是会有非常长条件判断块,并且要考虑每个条件中运算符的优先级
通常会写成多重 if-else 语句嵌套
if ( a === true) {
if ( b === true) {
if ( c === true) {
// ...
} else {
// ...
}
} else {
// ...
}
} else {
// ...
}
卫语句
do…while(false) 把多重嵌套的 if 写成顺序的 if
do {
if (a === false) {
// ...
break;
}
if (b === false) {
// ...
break;
if (c === false) {
// ...
break;
}
// ...
} while (false);
if…else 并列判断
function formatDay(strDay) {
if(strDay === 'Mon') return 'Monday';
else if(strDay === 'Tue') return 'Tuesday';
else if(strDay === 'Web') return 'Wednesday';
else if(strDay === 'Thu') return 'Thursday';
else if(strDay === 'Fri') return 'Friday';
else if(strDay === 'Sat') return 'Saturday';
else if(strDay === 'Sun') return 'Sunday';
else return 'Unknow';
}
function calc(command, num1, num2) {
if(command === 'add') return num1 + num2;
else if(command === 'sub') return num1 - num2;
else if(command === 'mul') return num1 * num2;
else if(command === 'div') return num1 / num2;
else return 0;
}
表驱动
const mapDayFormat = {
'Mon': 'Monday',
'Tue': 'Tuesday',
'Wed': 'Wednesday',
'Thu': 'Thursday',
'Fri': 'Friday',
'Sat': 'Saturday',
'Sun': 'Sunday',
'Other' : 'Unknow',
}
function formatDay(strDay) {
if(!mapDayFormat[strDay]) strDay = 'Other';
return mapDayFormat[strDay];
}
const mapCalculate = {
add(num1, num2) {
return num1 + num2;
},
sub(num1, num2) {
return num1 - num2;
},
mul(num1, num2) {
return num1 * num2;
},
div(num1, num2) {
return num1 / num2;
},
other() {
return 0;
}
}
function calc(command, num1, num2) {
if(!mapCalculate[command]) command = 'other';
return mapCalculate[command](num1, num2);
}
随着指令的增多,表也会变得冗长,可读性也会下降。表会把指令和对应的方法存放在一起,对其新增/修改指令会出现一些麻烦。
策略模式
class Calculator {
constructor() {
this.strategy = null;
}
setStrategy(strategy) {
this.strategy = strategy;
}
calculateResult(num1, num2) {
return this.strategy.execute(num1, num2);
}
}
class Add {
execute(num1, num2) {
return num1 + num2;
}
}
class Sub {
execute(num1, num2) {
return num1 - num2;
}
}
class Mul {
execute(num1, num2) {
return num1 * num2;
}
}
class Div {
execute(num1, num2) {
return num1 / num2;
}
}
const calc = new Calculator();
calc.setStrategy(new Add());
console.log(calc.calculateResult(1,2));
calc.setStrategy(new Sub());
console.log(calc.calculateResult(9,5));
条件合并,函数提炼
function getuserInfoContent(userInfo) {
if (userInfo.name == '') {
return 'Invalid data received';
}
if (userInfo.id <= 0) {
return 'Invalid data received';
}
if (userInfo.status == '') {
return 'Invalid data received';
}
if (!userInfo.isActivated) {
return 'User status is not normal';
}
if (userInfo.status != 'valid') {
return 'User status is not normal';
}
return 'Welcome' + userInfo.name;
}
// 条件合并,函数提炼
function getUserInfoContent(userInfo) {
if(!isDataValid(userInfo)) {
return 'Invalid data received';
}
if(!isUserStatusNormal(userInfo)) {
return 'User status is not normal';
}
return 'Welcome' + userInfo.name;
}
function isDataValid(userInfo) {
if(userInfo.name == '' || userInfo.id <= 0 || useriNfo.status == '') {
return false;
}
return true;
}
function isUserStatusNormal(userInfo) {
if(!userInfo.isActivated || userInfo.status != 'valid') {
return false;
}
return true;
}
函数优化
函数多参数
function multipleParams(isNew, aParam, bParam, cParam, dParam) {
if(isNew) {
console.log(aParam, bParam, cParam, dParam);
} else {
console.log(aParam, bParam, cParam);
}
}
const a = 1,
b = 'a',
c = false,
d = 2.3,
isNewVersion = true;
multipleParams(isNewVersion, a, b, c, d);
多个参数可能出传参时顺序写反的问题
使用对象进行优化
function multipleParams(params) {
const {isNew, aParam, bParam, cParam, dParam} = params;
if(isNew) {
console.log(aParam, bParam, cParam, dParam);
} else {
console.log(aParam, bParam, cParam);
}
}
multipleParams({
isNew: false,
a: 1,
b: 'b',
c: true,
d: 3.2
})