You didn't ask me, but if you did I'd tell you about an invaluable software engineering axiom called DRY (Don't Repeat Yourself (http://c2.com/cgi/wiki?DontRepeatYourself)).
The premise is that if something occurs twice in code, that's twice you have to remember to keep it updated when something changes. And by golly, it'll change. In this instance, observe how many times the format string appears in your function, versus in mine.
I also changed the argument order because code is poetry, it should read as you would speak it: pluralize 101 dalmations. This is known as the Principle of Least Surprise (http://c2.com/cgi/wiki?PrincipleOfLeastSurprise) because one day, maybe tomorrow, you're going to forget which way you defined those arguments ... was it dog then number? number then dog?
def pluralize(quantity, unit)
unit_suffix = ""
if 0 == quantity or 1 < quantity then
unit_suffix = "s"
end
return "#{quantity} #{unit}#{unit_suffix}"
end
It's never too early to start forming good habits.
no subject
Date: 2009-02-02 07:37 am (UTC)The premise is that if something occurs twice in code, that's twice you have to remember to keep it updated when something changes. And by golly, it'll change. In this instance, observe how many times the format string appears in your function, versus in mine.
I also changed the argument order because code is poetry, it should read as you would speak it:
pluralize 101 dalmations
. This is known as the Principle of Least Surprise (http://c2.com/cgi/wiki?PrincipleOfLeastSurprise) because one day, maybe tomorrow, you're going to forget which way you defined those arguments ... was it dog then number? number then dog?It's never too early to start forming good habits.