乐趣区

编程技巧重构ifelse-ifelse

前言

新入职的公司,前人留下来一个项目,里面充斥着大量的 if…else…,则倒是其次,主要连注释写的都很少。面对这样的已经上线的代码,我并没有想去重构他因为成本太高,只好鞭策自己不要写出这种代码

面对的问题?

有时候,我们可能面对这样的业务逻辑。(公司项目的业务逻辑),如果是回答过题目通过,如果回答过题目没有通过,如果没有回答过题目。如果不使用特定的模式,可能会写出下面这样的代码。一坨一坨的 if…else 看着非常不舒服,并且难以维护。

/**
 * 初始化函数
 * if...else if... 的情况较为简单
 * @return undefined
 */
function init () {
    // 是否回答过题目 1- 回答过, 通过 2- 回答过, 没有通过 3- 没有回答过
    let isAnswer
    // 是否是老用户 1- 老用户 2- 新用户
    let isOldUser

    if (isAnswer === 1) {// ...} else if (isAnswer === 2) {// ...} else if (isAnswer === 3) {// ...}

    if (isOldUser === 1) {// ...} else if (isOldUser === 2) {// ...}
}
/**
 * 初始化函数
 * if...else if... 嵌套的情况
 * @return undefined
 */
function init () {if (isAnswer === 1) {if (isOldUser === 1) {// ...} else if (isOldUser === 2) {// ...}
    } else if (isAnswer === 2) {if (isOldUser === 1) {// ...} else if (isOldUser === 2) {// ...}
    } else if (isAnswer === 3) {if (isOldUser === 1) {// ...} else if (isOldUser === 2) {// ...}
    }
}

解决办法 1: 查找表, 职责链查找表

虽然可能看着是治标不治本,其实不然,init 函数的复杂度大大的降低了。我们已经把控制流程的复杂逻辑,拆分到 determineAction 函数中

// 可以解决 if...else if... 简单的问题
const rules = {isAnswer1 () {return code},
    isAnswer2 () {return code},
    isAnswer3 () {return code}
}

function determineAction (type) {if (isAnswer === 1) {return 'isAnswer1'} else if (isAnswer === 2) {return 'isAnswer2'} else if (isAnswer === 3) {return 'isAnswer3'}
}

function init () {let key = determineAction(isAnswer)
    return rules[key]
}
// 面对 if...else if...else 嵌套的复杂情况

const rules = [
    {match (an, old) {if (an === 1) {return true}
        },

        action (an, old) {if (old === 1) {// ...} else if (old === 2) {// ...}
        }
    },
    {match (an, old) {if (an === 2) {return true}
        },

        action (an, old) {if (old === 1) {// ...} else if (old === 2) {// ...}
        }
    },
    {match (an, old) {if (an === 3) {return true}
        },

        action (an, old) {if (old === 1) {// ...} else if (old === 2) {// ...}
        }
    }
]

function init (an, old) {for (let i = 0; i < rules.length; i++) {
        // 如果返回 true
        if (rules[i].match(an, old)) {rules[i].action(an, old)
        }
    }
}

init(isAnswer, isOldUser)

⬆️上面复杂的情况,也可以吧 action 的判断抽离出来但是可能要写出三个抽离的函数,因为 an 值有三种不同的情况


解决办法 2: 面向切面的编程 (AOP)

为 Function 的原型链,扩展 after 语法,如果满足要求直接在函数内运算并返回结果。如果不满足条件返回 ’next’ 调用职责链的下一个节点。所谓的 Function.prototype.after 就是在本函数执行前执行 after 添加的函数

// 可以解决 if...else if... 简单的问题
Function.prototype.after = function (nextFn) {
    let self = this
    return function (...rest) {let code = self(...rest)
        if (code === 'next') {return nextFn(...rest)
        }
        return code
    }
}

// 重构原函数

function isAnswer1 (type) {if (type === 1) {return code}
    return 'next'
}

function isAnswer2 () {if (type === 2) {return code}
    return 'next'
}

function isAnswer3 () {if (type === 3) {return code}
    return 'next'
}

let isAnswerFn = isAnswer1.after(isAnswer2).after(isAnswer3)

isAnswerFn(isAnswer)
// 面对 if...else if...else 嵌套的复杂情况

function isAnswer1 (an, old) {if (an === 1) {return isOldUserFn1(an, old)
    }
    return 'next'
}

function isAnswer2 (an, old) {if (an === 2) {return isOldUserFn2(an, old)
    }
    return 'next'
}

function isAnswer3 (an, old) {if (an === 3) {return isOldUserFn3(an, old)
    }
    return 'next'
}

/**
 * isAnswer == 1 isOldUser == 1 的情况
 */
function isAnswer1IsOldUser1 (an, old) {if (old === 1) {return code}
    return 'next'
}

/**
 * isAnswer == 1 isOldUser == 2 的情况
 */
function isAnswer1IsOldUser2 (an, old) {if (old === 2) {return code}
    return 'next'
}

/**
 * isAnswer == 2 isOldUser == 1 的情况
 */
function isAnswer2IsOldUser1 (an, old) {if (old === 1) {return code}
    return 'next'
}

/**
 * isAnswer == 2 isOldUser == 2 的情况
 */
function isAnswer2IsOldUser2 (an, old) {if (old === 2) {return code}
    return 'next'
}

/**
 * isAnswer == 3 isOldUser == 1 的情况
 */
function isAnswer3IsOldUser1 (an, old) {if (old === 1) {return code}
    return 'next'
}

/**
 * isAnswer == 3 isOldUser == 2 的情况
 */
function isAnswer3IsOldUser2 (an, old) {if (old === 2) {return code}
    return 'next'
}

let isAnswerFn = isAnswer1.after(isAnswer2).after(isAnswer3)

// 三条职责链
let isOldUserFn1 = isAnswer1IsOldUser1.after(isAnswer1IsOldUser2)
let isOldUserFn2 = isAnswer2IsOldUser1.after(isAnswer2IsOldUser2)
let isOldUserFn3 = isAnswer3IsOldUser1.after(isAnswer3IsOldUser2)

isAnswerFn(isAnswer, isOldUser)

解决办法 3: 函数式编程

利用 ramda 等函数式编程库解决这种问题, ???? 链接:http://ramda.cn/docs/#cond

import R from 'ramda'

var fn = R.cond([[R.equals(0),   R.always('water freezes at 0°C')],
  [R.equals(100), R.always('water boils at 100°C')],
  [R.T,           temp => 'nothing special happens at' + temp + '°C']
]);

fn(0); //=> 'water freezes at 0°C'
fn(50); //=> 'nothing special happens at 50°C'
fn(100); //=> 'water boils at 100°C'
退出移动版