Sometimes I force myself to not use a feature. What if I couldn’t use if? Were that true, I’d have to do something else. Here’s an example from Smalltalk, where there’s no special syntax for if. Instead, there’s a Boolean receiver with ifTrue: and ifFalse: methods:
That’s easy enough to do in Perl too, and this is almost the same implementation as Smalltalk (at least Pharo’s, but I haven’t checked any others). The object knows what to do because it knows what it is rather than what it contains. Different classes have the same methods but act differently only based on their identity:
The Ruby version looks a bit closer to Smalltalk because Ruby syntax knows about bare blocks and I don’t need parens to call another method right after the block. I cheat a bit in Ruby because I define my own #new:
This works for more than just Booleans. Here’s a four state version that does FizzBin. Once I see people solve the problem in a job interview, I ask them to do it again with no if statements. I don’t expect them to come up with this, and I do use and here so I’m cheating a bit: