如何重构这个多嵌套的 if else 块
how to refactor this multi-nested if else block
我有这种多嵌套的 if-else 块。我的理解是,有一种 'data-driven' 方法可以帮助消除对它的需求并 trim 降低代码,但是,我还没有在很大程度上使用它,所以任何人都可以提供帮助我重构此代码以在 'data-driven' 方法中工作?
function (arg1) {
if(this.thing[arg1]){
// there is a valid property for this arg1
if(this.thing.a){
// there exists an 'a' propertie also
if(this.thing.a.arg1 == arg1){
// the a property has a property is the same as the arg1
// if this 'a' has a number higher than 0, avoid doing anything
if(this.thing.a.number > 0){
}else{
// 'a' number was 0 or lower, so we do something
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}else{
// the' a' is not the arg1
// so we want to use the current arg1!
// but only if its 'number' is lower than 1
if(this.thing.a.number > 0){
}else{
// 'a' number was 0 or lower, so we do something
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
}else{
// there is no thing.a so we set it to arg1
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
}
很确定您的逻辑归结为:
if (this.thing[arg1]) {
//confirm or set a
this.thing.a = this.thing.a ? this.thing.a : this.thing[arg1];
//if a.arg1 is not thing[arg1] and a.number is less than 1
if (this.thing.a.arg1 !== this.thing[arg1] && this.thing.a.number < 1) {
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
注意事项:
这个:
if(someNumber > 0){
//do nothing
} else {
//do something
}
永远不会是对的。不要创建空块,改变你的表达方式,像这样:
if (someNumber < 1) {
//do something
}
您重复此代码块 3 次。保持干爽(不要重复自己)
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
如果您发现自己在重复这样的代码,请退后一步,看看如何更改您的逻辑流程,这样您只需编写一次代码。
我有这种多嵌套的 if-else 块。我的理解是,有一种 'data-driven' 方法可以帮助消除对它的需求并 trim 降低代码,但是,我还没有在很大程度上使用它,所以任何人都可以提供帮助我重构此代码以在 'data-driven' 方法中工作?
function (arg1) {
if(this.thing[arg1]){
// there is a valid property for this arg1
if(this.thing.a){
// there exists an 'a' propertie also
if(this.thing.a.arg1 == arg1){
// the a property has a property is the same as the arg1
// if this 'a' has a number higher than 0, avoid doing anything
if(this.thing.a.number > 0){
}else{
// 'a' number was 0 or lower, so we do something
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}else{
// the' a' is not the arg1
// so we want to use the current arg1!
// but only if its 'number' is lower than 1
if(this.thing.a.number > 0){
}else{
// 'a' number was 0 or lower, so we do something
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
}else{
// there is no thing.a so we set it to arg1
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
}
很确定您的逻辑归结为:
if (this.thing[arg1]) {
//confirm or set a
this.thing.a = this.thing.a ? this.thing.a : this.thing[arg1];
//if a.arg1 is not thing[arg1] and a.number is less than 1
if (this.thing.a.arg1 !== this.thing[arg1] && this.thing.a.number < 1) {
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
}
}
注意事项:
这个:
if(someNumber > 0){
//do nothing
} else {
//do something
}
永远不会是对的。不要创建空块,改变你的表达方式,像这样:
if (someNumber < 1) {
//do something
}
您重复此代码块 3 次。保持干爽(不要重复自己)
this.thing.a = this.thing[arg1];
this.thing.a.arg1 = arg1;
如果您发现自己在重复这样的代码,请退后一步,看看如何更改您的逻辑流程,这样您只需编写一次代码。