I’m quite familiar with writing ruby code. I’ve even expressed my love for the language long time ago. But some constructs still bug me. One of them is unless…
I’ve seen variants of this code:
temperature = 36.7
unless temperature > 37.2 || temperature < 35.5
puts "Healthy temperature. Temperature is within the normal range."
end
# Output: "Healthy temperature. Temperature is within the normal range."
Just don’t.
I like unless sometimes, but for simple expressions:
print "normal" unless abnormal
But more complex conditionals should be if statements:
if temperature > 35.5 && temperature < 37.2
puts "Healthy temperature. Temperature is within the normal range."
end
or perhaps even nicer:
if (35.5...37.2).include?
puts "Healthy temperature. Temperature is within the normal range."
end
Enjoyed this? Follow me on Mastodon or add the RSS, euh ATOM feed to your feed reader.
Dit artikel van murblog van Maarten Brouwers (murb) is in licentie gegeven volgens een Creative Commons Naamsvermelding 3.0 Nederland licentie .