I would use R as an example:
# x is an object
# condA(x), condB(x) and condC(x) evaluate x and return TRUE or FALSE
# The conditions must be evaluated in the following orders.
# For e.g., when validating for an random object being a number larger than 5,
# you always need to evaluate whether it is numeric first,
# only after which can you evaluate whether it is larger than 5.
# Trying to evaluate both at once will cause an error if it is non-numeric.
# process1() and process2() are two different procedures
if (condA(x)) {
if (condB(x)) {
if (condC(x)) {
process1()
} else {
process2()
}
} else {
process2()
}
} else {
if (cond(C)) {
process1()
} else {
process2()
}
}
This way I need to specify each of the processes more than once, and repeat the chunk of evaluating condC(x), which I feel clumsy doing so. Any suggestion for a more elegant way of coding this structure, so that I need to mention each of process1() and process2() only once, without disrupting the order of evaluations as stated in the above code?
Aucun commentaire:
Enregistrer un commentaire