by jim_h · 16y ago · 0 comments
'foo = 42 and foo / 2'
I was expecting an undefined variable since foo was never set because I was thinking of it as foo = (42 and foo / 2)
Basically I would write the example as this in 2 lines to avoid confusion and not use 'and':
foo = 42
foo / 2
Maybe another example in the article would have been better to show the power of 'and'.
Your post is explaining and demonstrating the use of "and" and "or", whose purpose is to allow idioms which rely on short-circuiting logic to control side effects, but that particular example didn't involve side effects after the operator (which is the whole point).
Perhaps it would be a good idea to replace that example with one that shows what would happen if you used "and" in an "if". It would serve the same purpose as the one you have currently -- show the unintended effects of using the wrong flavor of operator -- without confusing people or unintentionally teaching bad practices.