This question already has an answer here:
- Best Practice on IF/ELSE Statement Order 10 answers
I remember reading in a software engineering book once that there is a typical rule to follow when writing if else
conditional statements as to the order of evaluation. Should the valid case be evaluated first then followed by the invalid/erroneous case or vice versa? Is there a proper order for which cases should be evaluated first?
Example:
if (good) {
// Exec some block of code for good case.
} else {
// Bad case.
}
or
if (errorCase1) {
// errorCase1 block.
} else if (errorCase2) {
// errorCase2 block.
} else {
// good case.
}
Aucun commentaire:
Enregistrer un commentaire