Settings

Theme

Epic Answer - Avoiding If statements

stackoverflow.com

9 points by sidmkp96 13 years ago · 5 comments

Reader

yen223 13 years ago

It's easy - use switch/case statements!

But seriously, isn't it true that Smalltalk doesn't have if statements?

  • fzzzy 13 years ago

    Yes. Smalltalk has the ifTrue: and ifFalse: methods on the Boolean object.

      x > y ifTrue: [ Transcript show: 'Truthy' ]
    
    So it is using polymorphism as the main mechanism. The True object implements ifTrue: by evaluating the block argument and implements ifFalse: by doing nothing. The False object does the opposite.
andymoe 13 years ago

His idea in the second edit is similar to what is done with a lot of methods in Cocoa and Cocoa Touch and so on: http://cocoadev.com/wiki/MethodSwizzling

amccloud 13 years ago

Wouldn't that be considered a state machine? I've done something with an audio player. Each state was a different class.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection