I am processing data and there are a number of cases to process. Selecting the correct case involves some complexity, and so does processing once the correct case is found.
I can approach the problem in two different ways: (A) a method selects the correct process and calls it (B) a method carries out the selection, returns a code to another that controls the process.
(A) was my first option, but (B) has the advantage of decoupling the selection process, making it clearer, and separating it from the processing as such.
In pseudo code:
====== option A ======
function doEverything(data) {
if (case1(data))
result = doCase1(data)
else {
doStuff(data) // applies to all cases except case1
if (case2(data)) {
doStuff2()
result = doCase2(data)
}
else if (case3(data)) {
result = doCase3(data)
}
else result = doDefaultCase(data)
}
return result
}
Or alternatively:
====== option B ======
function doChoice(data) {
if (case1(data)) return 1
doStuff(data) // applies to all except case1
if (case2(data)) return 2
if (case3(data)) return 3
return 4 // default
}
function doProcess(data) {
which = choice(data)
switch (which) {
case 1:
return doCase1(data)
break
case 2:
doStuff(data) // all cases except case1
doStuff2()
return doCase2(data)
break
case 3:
doStuff(data) // all cases except case1
return doCase3(data)
break
case 4:
doStuff(data) // all cases except case1
return doDefaultCase(data)
default:
raise an error
}
My questions is: is there standard practice that applies to this sort of situation?
Aucun commentaire:
Enregistrer un commentaire