SlideShare a Scribd company logo
1 of 22
Stéphane Ducasse 1
Stéphane Ducasse
stephane.ducasse@inria.fr
http://stephane.ducasse.free.fr/
Design Points - Law of
Demeter
Stéphane Ducasse --- 2005
S.Ducasse 2
About Coupling
• Why coupled classes is fragile design?
• Law of Demeter
• Thoughts about accessor use
S.Ducasse 3
Coupling hampers...
Reuse
I cannot reuse this component in another application
Substitution
I cannot substitute easily this component for another one
Encapsulation
When a far away change happens, I get impacted
S.Ducasse 4
The Core of the Problem
S.Ducasse 5
The Law of Demeter
You should only send messages to:
an argument passed to you
instance variables
an object you create
self, super
your class
Avoid global variables
Avoid objects returned from message sends other
than self
S.Ducasse 6
Correct Messages
someMethod: aParameter
self foo.
super someMethod: aParameter.
self class foo.
self instVarOne foo.
instVarOne foo.
aParameter foo.
thing := Thing new.
thing foo
S.Ducasse 7
In other words
• Only talk to your immediate friends.
• In other words:
• You can play with yourself. (this.method())
• You can play with your own toys (but you can't take them
apart). (field.method(), field.getX())
• You can play with toys that were given to you. (arg.method())
• And you can play with toys you've made yourself. (A a =
new A(); a.method())
S.Ducasse 8
Halt!
S.Ducasse 9
To not skip your intermediate
S.Ducasse 10
Solution
S.Ducasse 11
Transformation
S.Ducasse 12
Heuristic of Demeter
Not a law
Know when you can bend it or not apply it
Encapsulating collections may produce large
interfaces so not applying the LoD may help.
S.Ducasse 13
Collections
Object subclass: #A
instVar: myCollection
A>>do: aBlock
myCollection do: aBlock
A>>collect: aBlock
^ myCollection collect: aBlock
A>>select: aBlock
^ myCollection select: aBlock
A>>detect: aBlock
^ myCollection detect: aBlock
A>>isEmpty
^ myCollection isEmpty
…………………
S.Ducasse 14
About the Use of Accessors
Some schools say:“Access instance variables using
methods”
In such a case
Be consistent inside a class, do not mix direct access and
accessor use
Think accessors as protected methods (not invoked by
clients)
in ST: put them in accessing only when public
S.Ducasse 15
Accessors
Accessors are good for lazy initialization
Scheduler>>tasks
tasks isNil ifTrue: [task := ...].
^ tasks
BUT accessors methods should be Protected by
default at least at the beginning
S.Ducasse 16
Example
Scheduler>>initialize
self tasks: OrderedCollection new.
Scheduler>>tasks
^ tasks
But now everybody can tweak the tasks!
S.Ducasse 17
Accessors open Encapsulation
The fact that accessors are methods doesn’t
support a good data encapsulation.
You could be tempted to write in a client:
ScheduledView>>addTaskButton
...
model tasks add: newTask
What’s happen if we change the representation of
tasks?
S.Ducasse 18
Tasks
If tasks is now an array, it will break
Take care about the coupling between your objects
and provide a good interface!
Schedule>>addTask: aTask
tasks add: aTask
ScheduledView>>addTaskButton
...
model addTask: newTask
S.Ducasse 19
About Copy Accessor
Should I copy the structure?
Scheduler>>tasks
^ tasks copy
But then the clients can get confused...
Scheduler uniqueInstance tasks removeFirst
and nothing happens!
S.Ducasse 20
You will read code
Code that others wrote
Code that you wrote and forgot
S.Ducasse 21
Use intention revealing names
Probably Better
Scheduler>>taskCopy or copiedTasks
“returns a copy of the pending tasks”
^ task copy
S.Ducasse 22
Provide a Complete Interface
Workstation>>accept: aPacket
aPacket addressee = self name
…
It is the responsibility of an object to offer a
complete interface that protects itself from client
intrusion.
Shift the responsibility to the Packet object
Packet>>isAddressedTo: aNode
^ addressee = aNode name
Workstation>>accept: aPacket
(aPacket isAddressedTo: self)
ifTrue:[Transcript show: 'A packet is accepted by the
Workstation ', self name asString]

More Related Content

Viewers also liked (20)

Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
Stoop 400 o-metaclassonly
Stoop 400 o-metaclassonlyStoop 400 o-metaclassonly
Stoop 400 o-metaclassonly
 
5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)5 - OOP - Smalltalk in a Nutshell (c)
5 - OOP - Smalltalk in a Nutshell (c)
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
Stoop 437-proxy
Stoop 437-proxyStoop 437-proxy
Stoop 437-proxy
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
10 reflection
10 reflection10 reflection
10 reflection
 
Stoop 430-design patternsintro
Stoop 430-design patternsintroStoop 430-design patternsintro
Stoop 430-design patternsintro
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop 433-chain
Stoop 433-chainStoop 433-chain
Stoop 433-chain
 
12 - Conditions and Loops
12 - Conditions and Loops12 - Conditions and Loops
12 - Conditions and Loops
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 

Similar to Stoop ed-lod

4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)The World of Smalltalk
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.ManojSatishKumar
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural PatternsSameh Deabes
 
Divide and Conquer Approach.pptx
Divide and Conquer Approach.pptxDivide and Conquer Approach.pptx
Divide and Conquer Approach.pptxMuktarulHoque1
 
Bca winter 2013 2nd sem
Bca winter 2013 2nd semBca winter 2013 2nd sem
Bca winter 2013 2nd semsmumbahelp
 

Similar to Stoop ed-lod (20)

Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
Stoop 432-singleton
Stoop 432-singletonStoop 432-singleton
Stoop 432-singleton
 
Calc ii complete
Calc ii completeCalc ii complete
Calc ii complete
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
Divide and Conquer Approach.pptx
Divide and Conquer Approach.pptxDivide and Conquer Approach.pptx
Divide and Conquer Approach.pptx
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Debugging VisualWorks
Debugging VisualWorksDebugging VisualWorks
Debugging VisualWorks
 
Bca winter 2013 2nd sem
Bca winter 2013 2nd semBca winter 2013 2nd sem
Bca winter 2013 2nd sem
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 

More from The World of Smalltalk (17)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 

Recently uploaded

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 

Recently uploaded (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 

Stoop ed-lod

  • 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Design Points - Law of Demeter Stéphane Ducasse --- 2005
  • 2. S.Ducasse 2 About Coupling • Why coupled classes is fragile design? • Law of Demeter • Thoughts about accessor use
  • 3. S.Ducasse 3 Coupling hampers... Reuse I cannot reuse this component in another application Substitution I cannot substitute easily this component for another one Encapsulation When a far away change happens, I get impacted
  • 4. S.Ducasse 4 The Core of the Problem
  • 5. S.Ducasse 5 The Law of Demeter You should only send messages to: an argument passed to you instance variables an object you create self, super your class Avoid global variables Avoid objects returned from message sends other than self
  • 6. S.Ducasse 6 Correct Messages someMethod: aParameter self foo. super someMethod: aParameter. self class foo. self instVarOne foo. instVarOne foo. aParameter foo. thing := Thing new. thing foo
  • 7. S.Ducasse 7 In other words • Only talk to your immediate friends. • In other words: • You can play with yourself. (this.method()) • You can play with your own toys (but you can't take them apart). (field.method(), field.getX()) • You can play with toys that were given to you. (arg.method()) • And you can play with toys you've made yourself. (A a = new A(); a.method())
  • 9. S.Ducasse 9 To not skip your intermediate
  • 12. S.Ducasse 12 Heuristic of Demeter Not a law Know when you can bend it or not apply it Encapsulating collections may produce large interfaces so not applying the LoD may help.
  • 13. S.Ducasse 13 Collections Object subclass: #A instVar: myCollection A>>do: aBlock myCollection do: aBlock A>>collect: aBlock ^ myCollection collect: aBlock A>>select: aBlock ^ myCollection select: aBlock A>>detect: aBlock ^ myCollection detect: aBlock A>>isEmpty ^ myCollection isEmpty …………………
  • 14. S.Ducasse 14 About the Use of Accessors Some schools say:“Access instance variables using methods” In such a case Be consistent inside a class, do not mix direct access and accessor use Think accessors as protected methods (not invoked by clients) in ST: put them in accessing only when public
  • 15. S.Ducasse 15 Accessors Accessors are good for lazy initialization Scheduler>>tasks tasks isNil ifTrue: [task := ...]. ^ tasks BUT accessors methods should be Protected by default at least at the beginning
  • 16. S.Ducasse 16 Example Scheduler>>initialize self tasks: OrderedCollection new. Scheduler>>tasks ^ tasks But now everybody can tweak the tasks!
  • 17. S.Ducasse 17 Accessors open Encapsulation The fact that accessors are methods doesn’t support a good data encapsulation. You could be tempted to write in a client: ScheduledView>>addTaskButton ... model tasks add: newTask What’s happen if we change the representation of tasks?
  • 18. S.Ducasse 18 Tasks If tasks is now an array, it will break Take care about the coupling between your objects and provide a good interface! Schedule>>addTask: aTask tasks add: aTask ScheduledView>>addTaskButton ... model addTask: newTask
  • 19. S.Ducasse 19 About Copy Accessor Should I copy the structure? Scheduler>>tasks ^ tasks copy But then the clients can get confused... Scheduler uniqueInstance tasks removeFirst and nothing happens!
  • 20. S.Ducasse 20 You will read code Code that others wrote Code that you wrote and forgot
  • 21. S.Ducasse 21 Use intention revealing names Probably Better Scheduler>>taskCopy or copiedTasks “returns a copy of the pending tasks” ^ task copy
  • 22. S.Ducasse 22 Provide a Complete Interface Workstation>>accept: aPacket aPacket addressee = self name … It is the responsibility of an object to offer a complete interface that protects itself from client intrusion. Shift the responsibility to the Packet object Packet>>isAddressedTo: aNode ^ addressee = aNode name Workstation>>accept: aPacket (aPacket isAddressedTo: self) ifTrue:[Transcript show: 'A packet is accepted by the Workstation ', self name asString]