javascript - how to refactor this multi-nested if else block -


i have kind of multi-nested if-else block. understanding there 'data-driven' approach can eliminate need , trim down code, however, i'm not experienced in large way yet, can me refactor code work in 'data-driven' approach?

function (arg1) {   if(this.thing[arg1]){     // there valid property arg1     if(this.thing.a){       // there exists 'a' propertie       if(this.thing.a.arg1 == arg1){         // property has property same arg1             // if 'a' has number higher 0, avoid doing         if(this.thing.a.number > 0){         }else{           // 'a' number 0 or lower,           this.thing.a = this.thing[arg1];           this.thing.a.arg1 = arg1;         }       }else{         // the' a' not arg1         // want use current arg1!         // if 'number' lower 1         if(this.thing.a.number > 0){         }else{           // 'a' number 0 or lower,           this.thing.a = this.thing[arg1];           this.thing.a.arg1 = arg1;         }       }     }else{       // there no thing.a set arg1        this.thing.a = this.thing[arg1];       this.thing.a.arg1 = arg1;     }   } } 

pretty sure logic boils down this:

if (this.thing[arg1]) {     //confirm or set     this.thing.a = this.thing.a ? this.thing.a : this.thing[arg1];      //if a.arg1 not thing[arg1] , a.number less 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;        } } 

things should watch out for:

this:

if(somenumber > 0){    //do nothing } else {    //do } 

is never going right. don't create empty blocks, change expression, so:

if (somenumber < 1) {     //do } 

you repeat block of code 3 times. stay dry (don't repeat yourself)

this.thing.a = this.thing[arg1]; this.thing.a.arg1 = arg1; 

if notice repeating code this, step , @ how can change logical flow have write code once.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -