Basic Patterns for Everyday Programming
laktek.com
laktek.com
The other "patterns" are fishy too. How has this gotten so many points?
Now if the language you're using behaves opposite of your wish, you're either using the wrong language or doing things the wrong way.
(Furthermore, it's harder to test, as now current_date and current_day need to be stubbed out. It's also less obvious which day is being checked with regards to being discount day or not.)
I'd recommend having discount_day? taking a date as the argument, and having it invoked as discount_day?(current_date). (The day can then be calculated from the provided date.)
Memoize only works if your function is one to one (one set of inputs gives the same outputs), and you cache the result of the function for each value of the input.
if(current_day == "Monday" || current_day == "Wednesday" || current_day == "Friday")
with if(["Monday", "Wednesday", "Friday"].include?(current_day))
I've done this before, and I don't think it's any less clear -- I wouldn't have thought of making a public announcement about it, though. I like it because it more closely aligns with the way you're probably thinking in this example: you're wondering if current_day is part of a certain set of days (the discount weekdays, apparently), not if it's equal to "Monday" or equal to "Tuesday", etc. I like the Javascript example with it's indexOf(..) >= 0 a lot less, but I guess people more used to JS idioms auto-translate this sequence to a set-contains operation.It's more sensible if the set of values is larger than just three; and in this instance, you have to wonder why the day isn't available as a numeric variable, but I think you can let it slide for a contrived example.
It's even more useful if you reference the same set more than once, because you can just define it once, and as a constant if your language of choice does that kind of thing. This would also let you make your code less wordy (but arguably more literal) without hiding logic in functions, e.g. his later example would be
if(discount_weekdays.include?(current_day) && current_date > 20)Both are correct, I am strictly referring to his rationale being it's too verbose.
if 5 in [1, 2, 5, 6, 8]:
pass case current_day when "Monday", "Wednesday", "Friday" then <do stuff> endHis choice to use [].include? is idiomatic ruby so I don't think it's being clever for clever's sake... although I've always kind of stumbled over that construct, I sort of wish there was an 'in' statement... if current_day in [x,y,z]. It would be easier to read (for me at least).
Your pseudocode is idiomatic Python code :)
Moreover, the memoise part is better handled with a decorator: it is a special behavior of the function and don't relate to its logic (its body).
[1] http://www.amazon.com/Smalltalk-Best-Practice-Patterns-Kent/...