for example...
if ( /* Condition */ ) {
if ( /* Condition */ ) {
if ( /* Condition */ ) {
// Superb!
} else {
// Error 3
}
} else {
// Error 2
}
} else {
// Error 1
}
Do you know how to avoid this? Thank you!
If this is a library function, throw
may be the appropriate action.
if (!condition1) {
throw "Condition 1 failed.";
}
if (!condition2) {
throw "Condition 2 failed.";
}
if (!condition3) {
throw "Condition 3 failed.";
}
// Superb!
Other acceptable actions might be:
0
, null
, or undefined
.You will have to determine which failure action is right for your use case.
It looks like you have 3 conditions to check and 4 actions (3 different errors + 1 success). Unfortunately in the general case it's going to require 3 conditional checks and 4 actions. I think the code can be cleaned up a bit by using the following structure though
if (! /* condition 1 */ ) {
// Error 1
} else if (! /* condition 2 */ ) {
// Error 2
} else if (! /* condition 3 */ ) {
// Error 3
} else {
// superb
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With