the only problem I can see with this code is that if an invalid new value exists, it will have a priority of 0 and therefore the CurrentValue will be overwritten.
this can be fixed by having an ELSE which sets priority to 9 (or another high value).
eg. something like:
GetPriority PROCEDURE(BYTE Value) !,BYTE
Priority BYTE, AUTO
Code
CASE Value
OF 0 ; Priority = 8
OF 1 ; Priority = 6
OF 2 ; Priority = 5
OF 3 ; Priority = 7
OF 4 ; Priority = 1 ! Highest / Best
OF 5 ; Priority = 2
OF 6 ; Priority = 3
OF 7 ; Priority = 4
OF 8 ; Priority = 3
OF 9 ; Priority = 3
ELSE ! invalid
Priority = 9
END
RETURN Priority
another thought is to use EXECUTE
GetPriority PROCEDURE(BYTE Value) !,BYTE
Priority BYTE, AUTO
Code
EXECUTE Value+1
Priority = 8
Priority = 6
Priority = 5
Priority = 7
Priority = 1 ! Highest / Best
Priority = 2
Priority = 3
Priority = 4
Priority = 3
Priority = 3
ELSE ! invalid
Priority = 9
END
RETURN Priority
you could probably do something similar using choose:
priority = choose(value+1,8,6,5,7,1,2,3,4,3,3,9)
note the extra value ‘9’ on the end to catch invalid input.
yet another way might be to use an array
priorities string('<8,6,5,7,1,2,3,4,3,3>')
priority byte,dim(10),over(priorities)
Clarion arrays are 1-based (unlike C and derivatives) so you add one
if NewValue < 10 and priority[CurrentValue+1] > priority[NewValue+1]
CurrentValue = NewValue
end
if you are not sure the current value is valid then you might want to validate that too:
if CurrentValue > 9 ! invalid current value
if NewValue < 10
CurrentValue = NewValue
end
elsif NewValue < 10 and priority[CurrentValue+1] > priority[NewValue+1]
CurrentValue = NewValue
end
Doing it this way saves the overhead of a function call as you don’t need the GetPriority procedure. Admittedly that overhead is small…
anyway there’s some more ideas.