逻辑判断优化
此文是拜读了我司最近更新的优化逻辑判断代码所记下的笔记还有自己的一些看法
优化嵌套层级
例:
function supply(fruit,num){ const redFruit = ['apple','cherry'] if(fruit){ if(redFruit.includes(fruit)){ console.log('红色水果') if(num>10){ console.log('数量大于10的红果') } } }else{ throw new Error() } }
|
判断非状态,直接return
function supply(fruit,num){ const redFruit = ['apple','cherry'] if(!fruit)throw new Error() if(!redFruit.includes(fruit)){ return } console.log('红色水果') if(num>10){ console.log('数量大于10') } }
|
多分支 多层if-else
- 使用
switch-case
function pick(color) { switch (color) { case 'red': return ['apple', 'strawberry']; case 'yellow': return ['banana', 'pineapple']; case 'purple': return ['grape', 'plum']; default: return []; } }
|
- 巧妙使用
object
的key属性 首先定义枚举,然后参数当成key
const fruitColor = { red: ['apple', 'strawberry'], yellow: ['banana', 'pineapple'], purple: ['grape', 'plum'], }; function pick(color) { return fruitColor[color] || []; }
|
- 使用
Map
数据结构简化
const fruitColor = new Map() .set('red', ['apple', 'strawberry']) .set('yellow', ['banana', 'pineapple']) .set('purple', ['grape', 'plum']);
function pick(color) { return fruitColor.get(color) || []; }
|
- 利用
filter
简化
const fruits = [ { name: 'apple', color: 'red' }, { name: 'strawberry', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'pineapple', color: 'yellow' }, { name: 'grape', color: 'purple' }, { name: 'plum', color: 'purple' } ];
function pick(color) { return fruits.filter(f => f.color === color); }
|
多条件判断
或语句
如果if里面判断的或条件太多,就很难维护
if (fruit === 'apple' || fruit === 'strawberry' || fruit === 'cherry' || fruit === 'cranberries' )
|
这种情况优化使用includes
const redFruit = ['apple','strawberry','cherry'] function supply(fruit){ if(redFruit.includes(fruit)){ } }
|
const fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'grape', color: 'purple' } ];
const isAnyRed = fruits.filter(f => f.color == 'red');
|
且语句
判断数组里面所有的水果是否都是红色
const fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'grape', color: 'purple' } ];
function match() { const isAllRed = fruits.every(f => f.color === 'red');
console.log(isAllRed); }
|
优化嵌套地狱
if语句
if(isOrderOperator && order.payStatus === 5 && prePaymentStatus === 'PAID'){ renderText = '· 您已验收成功,请及时支付' } else if (isOrderOperator) { if (prePaymentStatus) { if (isAuditor) { renderText = '· 支付申请已提交,请及时审核' }else{ renderText = TipText[prePaymentStatus] } }else{ renderText = '· 您已验收成功,请及时支付' } }else if(prePaymentStatus){ renderText = ITipText[prePaymentStatus] }else{ renderText = `· 需要采购单位采购经办人(${purchaserName})发起支付` }
|
策略模式改进
const renderPaymentText = ()=>{ let renderText = null const TextMap = new Map([ [{ isOrderOperator:true, hasPrePaymentStatus:true, isAuditor:false }, ()=>{renderText = TipText[prePaymentStatus]} ], [{ isOrderOperator:true, hasPrePaymentStatus:true, isAuditor:true }, ()=>{renderText = '· 支付申请已提交,请及时审核'} ], [{ isOrderOperator:true, hasPrePaymentStatus:false, isAuditor:false }, ()=>{renderText = '· 您已验收成功,请及时支付'} ], [{ isOrderOperator:false, hasPrePaymentStatus:true, isAuditor:false }, ()=>{renderText = ITipText[prePaymentStatus]} ], [{ isOrderOperator:false, hasPrePaymentStatus:false, isAuditor:false }, ()=>{renderText = `· 需要采购单位采购经办人(${purchaserName})发起支付`} ] ]) const run = (isOrderOperator, hasPrePaymentStatus, isAuditor) => { let action = [...TextMap].find(([key, value]) => (key.isOrderOperator === isOrderOperator && key.hasPrePaymentStatus === hasPrePaymentStatus && key.isAuditor === isAuditor)) action[1].call(this) }
run(isOrderOperator, hasPrePaymentStatus, isAuditor) if(isOrderOperator && payStatus === 5 && prePaymentStatus === 'PAID'){ renderText = '· 您已验收成功,请及时支付' } return renderText }
|
条件简化版策略模式
const renderPaymentText = ()=>{ let renderText = null const TextMap = { 'true-true-false': `${OrderOperatorText(auditorName,PrepaymentDetail,reason)[prePaymentStatus]}`, 'true-true-true':'· 支付申请已提交,请及时审核', 'true-false-false':'· 您已验收成功,请及时支付', 'false-true-false':`${IOrderOperatorText(purchaserName)[prePaymentStatus]}`, 'false-false-false':`· 需要采购单位采购经办人(${purchaserName})发起支付` } renderText = TextMap[`${isOrderOperator}-${hasPrePaymentStatus}-${isAuditor}`] if(isOrderOperator && isSplitpay && prePaymentStatus === 'PAID'){ renderText = '· 您已验收成功,请及时支付' } return renderText }
|
性能优化
待编写