【发布时间】:2017-12-13 18:18:51
【问题描述】:
我在开发代码中包含一些类似块的代码时遇到了一个问题。我的问题是:在函数之间共享逻辑的最佳方法是什么?
例子:
以下函数包含相同的 if/else 逻辑。我们如何重构这段代码以获得更简洁和可维护的代码?
// pseudo code...
const hirer = 'woman';
const getPositions = () => {
if (hirer === 'woman') {
getPositionsFromWomen();
// do other stufs here...
} else if (hirer === 'man') {
getPositionFromMen();
// do other stufs here...
}
// maybe other stufs here...
}
const hire = (hirer) => {
if (hirer === 'woman') {
increaseWomenHiringRate(hirer);
// do other stufs here...
} else if (hirer === 'man') {
increaseMenHiringRate(hirer);
// do other stufs here...
}
setPositionClosed();
}
【问题讨论】:
标签: javascript design-patterns coding-style