Stéphane Ducasse 1
Stéphane Ducasse
stephane.ducasse@inria.fr
http://stephane.ducasse.free.fr/
Objects to the Roots:
Learn...
S.Ducasse 2
Really?!
No primitive types
No hardcoded constructs for conditional
Only messages
Only objects
and this works?...
S.Ducasse 3
Motto
Let’s open our eyes, look, understand, and deeply
understand the underlying design aspects of object-
or...
S.Ducasse 4
Booleans
3 > 0
ifTrue: ['positive']
ifFalse: ['negative']
S.Ducasse 5
Booleans
3 > 0
ifTrue: ['positive']
ifFalse: ['negative']
'positive'
S.Ducasse 6
Yes ifTrue:ifFalse: is a message!
Weather isRaining
ifTrue: [self takeMyUmbrella]
ifFalse: [self takeMySunglas...
S.Ducasse 7
Booleans
& | not
or: and: (lazy)
xor:
ifTrue:ifFalse:
ifFalse:ifTrue:
...
S.Ducasse 8
Lazy Logical Operators
false and: [1 error: 'crazy']
PrIt-> false and not an error
S.Ducasse 9
Yes! ifTrue:ifFalse: is a message send to a
Boolean.
But optimized by the compiler :)
S.Ducasse 10
Implementing not
Now you are good and you should implement it
Propose an implementation of not in a world
whe...
S.Ducasse 11
S.Ducasse 12
Implementing ifTrue:ifFalse:
Now you are good and you should implement it
Propose an implementation of not in...
S.Ducasse 13
Boolean Objects
false and true are objects
described by classes
Boolean,True and False
S.Ducasse 14
Let’s the receiver decide!
S.Ducasse 15
Boolean>>not
“Class Boolean is an abstract class that implements
behavior common to true and false. Its subcl...
S.Ducasse 16
Not
false not -> true
true not -> false
Boolean>>not
"Negation. Answer true if the receiver is false, answer ...
S.Ducasse 17
| (Or)
• true | true -> true
• true | false -> true
• true | anything -> true
• false | true -> true
• false ...
S.Ducasse 18
Boolean>> | aBoolean
Boolean>> | aBoolean
"Evaluating disjunction (OR). Evaluate the argument.
Answer true if...
S.Ducasse 19
False>> | aBoolean
false | true -> true
false | false -> false
false | anything -> anything
False>> | aBoolea...
S.Ducasse 20
True>> | aBoolean
true | true -> true
true | false -> true
true | anything -> true
True>> | aBoolean
"Evaluat...
S.Ducasse 21
Boolean,True and False
S.Ducasse 22
Implementation Note
Note that theVirtual Machine shortcuts calls to
boolean such as condition for speed reaso...
S.Ducasse 23
S.Ducasse 24
Ok so what?
You will probably not implement another Boolean
classes
So is it really that totally useless?
S.Ducasse 25
Ternary logic
Boolean: true, false, unknown
S.Ducasse 26
More important...
Message sends act as case statements
S.Ducasse 27
OOP: the art of dispatching
Subclasses create your vocabulary
S.Ducasse 28
Avoid Conditional
Use objects and messages
VM dispatch is a conditional switch: Use it!
AntiIfCampaign
S.Ducasse 29
Summary
Messages act as a dispatcher
Avoid conditional
Upcoming SlideShare
Loading in...5
×

Dispatch

678

Published on

Published in: Education, Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
678
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dispatch

  1. 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Objects to the Roots: Learning from beauty Learning from beauty
  2. 2. S.Ducasse 2 Really?! No primitive types No hardcoded constructs for conditional Only messages Only objects and this works? I mean really? Not even slow? Can’t be real!
  3. 3. S.Ducasse 3 Motto Let’s open our eyes, look, understand, and deeply understand the underlying design aspects of object- oriented programming...
  4. 4. S.Ducasse 4 Booleans 3 > 0 ifTrue: ['positive'] ifFalse: ['negative']
  5. 5. S.Ducasse 5 Booleans 3 > 0 ifTrue: ['positive'] ifFalse: ['negative'] 'positive'
  6. 6. S.Ducasse 6 Yes ifTrue:ifFalse: is a message! Weather isRaining ifTrue: [self takeMyUmbrella] ifFalse: [self takeMySunglasses] ifTrue:ifFalse is sent to an object: a boolean!
  7. 7. S.Ducasse 7 Booleans & | not or: and: (lazy) xor: ifTrue:ifFalse: ifFalse:ifTrue: ...
  8. 8. S.Ducasse 8 Lazy Logical Operators false and: [1 error: 'crazy'] PrIt-> false and not an error
  9. 9. S.Ducasse 9 Yes! ifTrue:ifFalse: is a message send to a Boolean. But optimized by the compiler :)
  10. 10. S.Ducasse 10 Implementing not Now you are good and you should implement it Propose an implementation of not in a world where you do not have Booleans false not -> true true not -> false
  11. 11. S.Ducasse 11
  12. 12. S.Ducasse 12 Implementing ifTrue:ifFalse: Now you are good and you should implement it Propose an implementation of not in a world where you do not have Booleans false ifTrue: [ 3 ] ifFalse: [ 5 ] true ifTrue: [ 3 ] ifFalse: [ 5 ]
  13. 13. S.Ducasse 13 Boolean Objects false and true are objects described by classes Boolean,True and False
  14. 14. S.Ducasse 14 Let’s the receiver decide!
  15. 15. S.Ducasse 15 Boolean>>not “Class Boolean is an abstract class that implements behavior common to true and false. Its subclasses are True and False. Subclasses must implement methods for logical operations &, not, controlling and:, or:, ifTrue:, ifFalse:, ifTrue:ifFalse:, ifFalse:ifTrue:” Boolean>>not "Negation. Answer true if the receiver is false, answer false if the receiver is true." self subclassResponsibility
  16. 16. S.Ducasse 16 Not false not -> true true not -> false Boolean>>not "Negation. Answer true if the receiver is false, answer false if the receiver is true.” self subclassResponsibility False>>not "Negation -- answer true since the receiver is false." ^true True>>not "Negation--answer false since the receiver is true." ^false
  17. 17. S.Ducasse 17 | (Or) • true | true -> true • true | false -> true • true | anything -> true • false | true -> true • false | false -> false • false | anything -> anything
  18. 18. S.Ducasse 18 Boolean>> | aBoolean Boolean>> | aBoolean "Evaluating disjunction (OR). Evaluate the argument. Answer true if either the receiver or the argument is true." self subclassResponsibility
  19. 19. S.Ducasse 19 False>> | aBoolean false | true -> true false | false -> false false | anything -> anything False>> | aBoolean "Evaluating disjunction (OR) -- answer with the argument, aBoolean." ^ aBoolean
  20. 20. S.Ducasse 20 True>> | aBoolean true | true -> true true | false -> true true | anything -> true True>> | aBoolean "Evaluating disjunction (OR) -- answer true since the receiver is true." ^ self
  21. 21. S.Ducasse 21 Boolean,True and False
  22. 22. S.Ducasse 22 Implementation Note Note that theVirtual Machine shortcuts calls to boolean such as condition for speed reason. Virtual machines such asVisualWorks introduced a kind of macro expansion, an optimisation for essential methods and Just In Time (JIT) compilation.A method is executed once and afterwards it is compiled into native code. So the second time it is invoked, the native code will be executed.
  23. 23. S.Ducasse 23
  24. 24. S.Ducasse 24 Ok so what? You will probably not implement another Boolean classes So is it really that totally useless?
  25. 25. S.Ducasse 25 Ternary logic Boolean: true, false, unknown
  26. 26. S.Ducasse 26 More important... Message sends act as case statements
  27. 27. S.Ducasse 27 OOP: the art of dispatching Subclasses create your vocabulary
  28. 28. S.Ducasse 28 Avoid Conditional Use objects and messages VM dispatch is a conditional switch: Use it! AntiIfCampaign
  29. 29. S.Ducasse 29 Summary Messages act as a dispatcher Avoid conditional
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×