我正在尝试重构if..else语句。
我的代码:
let condition = 'hi';
if(condition === 'hi'){
commonFunction('hi');
console.log('hi is called');
}else if(condition === 'bye'){
commonFunction('bye');
console.log('bye is called');
}else if(condition.includes('happy')){
commonFunction('happy');
console.log('happy is called');
}else if(condition === 'greeting'){
commonFunction('greeting');
console.log('greeting is called');
}
重构代码:
if(condition === 'hi'){
hi();
}else if(condition === 'bye'){
bye();
}else if(condition.includes('happy')){
happy();
}else if(condition === 'greeting'){
greeting();
}
function hi(){
commonFunction('hi');
console.log('hi is called');
}
function bye(){
commonFunction('bye');
console.log('bye is called');
}
function happy(){
commonFunction('happy');
console.log('happy is called');
}
function greeting(){
commonFunction('greeting');
console.log('greeting is called');
}
最好通过像重构代码这样的条件来声明每个函数吗?
或者,如何构造类并通过构造函数调用commonFunction? (我认为switch..case没有用,因为我有一个条件包含include())
答案 0 :(得分:0)
对此,可以做的一件事是使用object
作为condition
在keys
中声明函数。
let objFunc = {
'hi': function() {
commonFunction('hi');
console.log('hi is called');
},
'happy': function() {
commonFunction('happy');
console.log('happy is called');
},
'bye': function() {
commonFunction('bye');
console.log('bye is called');
},
'greeting': function() {
commonFunction('greeting');
console.log('greeting is called');
},
};
objFunc[condition]();
答案 1 :(得分:0)
一种选择是使用包含函数的 object ,而不要使用多个独立的函数。然后,只需使用属性查找即可。
但是,对于此确切的代码,您可以使用return new ArrayList<String>(suggestions);
来检查访问了哪个属性,而不是具有多个属性(因为所有属性/功能都具有共同的功能),因此甚至可以变干。 :
Proxy
这非常干燥,但是这样做很奇怪-在大多数情况下,您将使用对象代替:
const condition = 'hi';
const fns = new Proxy({}, { get: (_, prop) => {
console.log(prop + ' is called');
}});
const props = ['hi', 'bye', 'greeting'];
if (condition.includes('happy')) {
fns.happy();
} else if (fns[condition]) {
fns[condition]();
}
答案 2 :(得分:0)
在您的原始代码中,您编写了多个if else语句,只是为了使用不同的预定义参数调用相同的函数,因此,您无需通过自己的方式进行重构, 我认为这要容易得多,而且代码少。
let predefinedArgs = {
hi: 'hi',
bye: 'bye'
...
}
let condition = 'hi'
commonFunction(predefinedArgs[`condition`]);
答案 3 :(得分:0)
您有一组要调用其公共功能的条件。最好的方法是简单地测试您是否可以处理该条件(在这里可以使用includes
)。然后调用该函数。
const allowed = ['hi', 'bye', 'happy', 'greeting']
let condition = 'happy'
if (allowed.includes(condition)) { // do you know how to deal with condition?
commonFunction(condition) // if so call the function
} else {
console.log(`unknown condition ${condition}`) // otherwise log an error
}
// not sure what commonFunction does, so this is just for the example
function commonFunction(condition) {
console.log(`${condition} is called`);
}
答案 4 :(得分:0)
我建议将所有这些if-else
移到commonFunction
本身(如果它再次不包含相同的if-else
,则需要更复杂的重构):
const commonFunction = (condition) => {
let f = ['hi', 'bye', 'happy', 'greeting'].find(e => condition.includes(e))
console.log(f + ' is called');
if (!f) return;
// do something
// return the result if needed
}
commonFunction('greeting')
commonFunction('die')
答案 5 :(得分:0)
我认为所有解决方案都应该可以正常工作。对于包含的条件,也许您可以尝试添加一个新参数。 我的意思是,正如之前的回答中所述:
let f = ['hi', 'bye', 'happy', 'greeting'].find(e => condition.includes(e))
console.log(f + ' is called');
if (!f) return;
// do something
// return the result if needed
}
commonFunction('greeting')
commonFunction('die')```
This could be converted to :
```const commonFunction = (condition, functionName) => {
// ... other code
// and then use can use the following link to call the function which you want to execute`
https://stackoverflow.com/questions/53492013/javascript-refactoring-if-else-statements-es6/53492079
const young = () => {//...}
and the calling could be:
commonFunction('greeting', slice)
commonFunction('die', young)```
Hope this makes sense!