patternMinor
Is there a better way of implementing this control structure?
Viewed 0 times
thiscontrolwaybetterstructurethereimplementing
Problem
I have the following control structure that I would like to improve:
if(number >= 100) then doSetInc(id, 8, 20)
elseif(number >= 91) then doSetInc(id, 8, 30)
elseif(number >= 81) then doSetInc(id, 8, 50)
elseif(number >= 7) then doSetInc(id, 8, 70)
elseif(number >= 1) then doSetInc(id, 8, 100)
endSolution
Kinda hard to know what you mean with better since you don't specify in what regard.
In regards to cleaner code:
-
FunctionName doSetInc is a bit weird, what does it do?
-
You are sending in 3 variables into the function, it's common practice to use as few as possible (rarely more than 2, 0 or 1 prefered).
-
You are always sending in "id" and "8" into the function, thus this function might not need those attributes and could be set in another way.
Other than that, since I find no easy mathematical similarities between these checks and what to do, I would say no, you can't improve it given this small piece of code and no other context.
In regards to cleaner code:
-
FunctionName doSetInc is a bit weird, what does it do?
-
You are sending in 3 variables into the function, it's common practice to use as few as possible (rarely more than 2, 0 or 1 prefered).
-
You are always sending in "id" and "8" into the function, thus this function might not need those attributes and could be set in another way.
Other than that, since I find no easy mathematical similarities between these checks and what to do, I would say no, you can't improve it given this small piece of code and no other context.
Context
StackExchange Code Review Q#14249, answer score: 6
Revisions (0)
No revisions yet.