When writing conditional blocks of logic try to avoid nesting, for example, consider the below:
|
1 2 3 4 5 6 7 |
if(someThing) { if(someThing.property === “yes”) { if(someThing.property.property === “yes”) { console.log("stuff") } } } |
This kind of nesting can make code hard to extend, hard to read and hard to debug, so we can move everything up to the top level like:
|
1 2 3 4 5 6 7 8 9 10 11 |
// invert the check if(!someThing) return; // invert the check if(someThing.property !== “yes”) return; // final check stays the same but still only reached if // the other 2 are correct if(someThing.property.property === “yes”) { console.log("stuff") } |