Use Case statements when you can
Consider the below examples:
Some things are easier to read than others. Consider the following
if x == 4
# do work
elsif x == 5
# do work
elsif x == 3
# do work
elsif x == 2
# do work
elseif x == 12
# do work
end
Now compare the above to the following:
case x
when 2
# do work
when 3
# do work
when 4
# do work
when 5
# do work
when 12
# do work
end
What do you find easier to read?
Case statements are often easier because there is slightly less mental effort involved. And the less you have to think, then the better. KISS: https://en.wikipedia.org/wiki/KISS_principle. In my opinion case statements work wonders, because you don’t have to think about equalities - it’s implicit in its function.
Written on March 17, 2017