SlideShare a Scribd company logo
1 of 28
Download to read offline
Encapsulation – the pitfalls of Object-Oriented Programming
Andrzej Jóźwiak
Or am I just exaggerating?
The root of all
evil?
Or am I just completely mad?
But why?
We mostly write CRUD applications in all
honesty!
Performance
Is there anyone who still defends it? Still, it
almost touches the problems with OOP
Inheritance
What is the similarity between Age of Empires 2
and Minecraft's armor stand?
Age of Minecraft?
Or you can just call it hierarchy if you like
Taxonomy
By combining state and functions, but why?
Abstraction
Just some state and behaviours?
What is an object?
Object
State
Behaviour
State State
Behaviour
Behaviour
Private information hidden behind a public
interface
What is an object?
Object
Message
Message
Message Message
Message passing, or is it? How do we pass the
messages? Most importantly what do we pass?
What is an object?
Object Object
Object Object
Object
Object
Object
Or is it a bad practice?
Should messages pass references?
class Foo(val value: Int) {
fun doSomething(bar: Bar) { //... }
}
class Bar(val value: String) {
fun doElse(baz: Baz) { //... }
}
class Baz(val value: String) {
fun doDifferent(foo: Foo) { //... }
}
val foo = Foo(value = 2023)
val bar = Bar(value = "DevDay")
val baz = Baz(value = "TomTom")
foo.doSomething(bar) //if we pass the references
bar.doElse(baz) //and they become the internal
//state
baz.doDifferent(foo) //then who owns them?
Object should be solely responsible for its internal state?
Should objects share references?
class Foo(
val bar: Bar,
val baz: Baz,
)
class Bar(val value: String)
class Baz(val value: String)
val bar = Bar(value = "DevDay")
val baz = Baz(value = "TomTom")
val foo = Foo(bar, baz)
val qux = Qux(bar, baz)
//both Bar and Baz instances are part of the internal
//state of Foo and Qux, so which one of these two
//has full responsibility of its encapsulated state?
class Qux(
val bar: Bar,
val baz: Baz,
)
But with some extra steps!
Isn't this just shared state?
Bar
Baz
Foo Qux
If Baz and Bar are part of Foo's internal state...
If Baz and Bar are part of Qux's internal state...
Does it mean that Qux can affect Foo's state?
Does it mean that Foo can affect Qux's state?
Then objects should not be shared and have only a single parent?
If encapsulation is about hiding...
A B
D F
G
C
E
A
B C
D
E F
G
Let's change a free
form graph to a tree!
Then what should we do if we need to communication between objects?
If everything only has a single parent...
A
B C
D
E F
G
A
B C
D
E F
G
Are we doomed?
Imagine that static does not exist...
A
B C
D
E F
G
A
B C
D
E F
G
If you squint your eyes isn't this the same as with any taxonomy?
Maybe we need to change something...
A
B C
D
E F
G
A
B
D
E F H
G C
New Object
Maybe soon will not be
needed?
New root object
How many times we will have to do it?
Better safe than sorry...
Spaghetti code leads to injuries!
Yes, yes it can!
Can encapsulation
be premature?
Imposing structure is worse than no structure at all!
Think about data and processing separately!
Class
hierarchy
Single
responsibility
Composition
graph
Objects
data flow
Object
transformation
Temporal
dependencies
Design
patterns
Behaviors
Module
relationship
Nothing special just a simple association
Let's play a game
State
State
State
State
State
State
State
Behavior Behavior
Behavior
Behavior Behavior
Behavior
Behavior Behavior
What to do with leftovers?
IThinkIWillNeedItController
JustForThatOneThingHelper
IDoNotKnowWhereToPutItManager
Let's put them in a Manager, Controller, Helper or a Do-er!
What to do with leftovers?
State
State
Behavior
Behavior
Behavior
Should Message send itself?
Should Message receive itself?
The OO dilemma
About the preaching
I was not totally
honest...
1. Write methods when the association with the data type is certain. It is easy to get it
wrong!
2. Minimize state instead of only segregating it.
3. Prefer immutability for the shared state.
4. When all hope is lost parametrize the behavior.
5. Polymorphism is not limited to only classes!
6. Prefer pure functions, if not constrained by the efficiency.
7. Encapsulate on module/namespace level.
8. It is ok to sometimes repeat or have redundant pieces. Long functions are not wrong.
9. Limit surface layer. It is much easier to comprehend only a few functions comparing to
thousands of small ones.
So how should we design our code?
Thank you for sticking till the end!
Any questions?

More Related Content

Similar to Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak - TomTom DevDay 2023

Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)muhammadmubinmacadad2
 
Using binary classifiers
Using binary classifiersUsing binary classifiers
Using binary classifiersbutest
 
BBQ BASH Technology Rant - June 2018
BBQ BASH Technology Rant - June 2018BBQ BASH Technology Rant - June 2018
BBQ BASH Technology Rant - June 2018Garth Gilmour
 
State of NLP and Amazon Comprehend
State of NLP and Amazon ComprehendState of NLP and Amazon Comprehend
State of NLP and Amazon ComprehendEgor Pushkin
 
Just the basics_strata_2013
Just the basics_strata_2013Just the basics_strata_2013
Just the basics_strata_2013Ken Mwai
 
Introduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad balochIntroduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad balochSarmad Baloch
 
Delta final paper
Delta final paperDelta final paper
Delta final paperprabhatjon
 
Hickey jvm summit2009
Hickey jvm summit2009Hickey jvm summit2009
Hickey jvm summit2009Kwanzoo Dev
 
The Role Of Ontology In Modern Expert Systems Dallas 2008
The Role Of Ontology In Modern Expert Systems   Dallas   2008The Role Of Ontology In Modern Expert Systems   Dallas   2008
The Role Of Ontology In Modern Expert Systems Dallas 2008Jason Morris
 
M01_OO_Intro.ppt
M01_OO_Intro.pptM01_OO_Intro.ppt
M01_OO_Intro.pptRAJESH S
 
Debugging Your Ext JS Code
Debugging Your Ext JS CodeDebugging Your Ext JS Code
Debugging Your Ext JS CodeSencha
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingJun Shimizu
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesomeAndrew Hull
 
Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2Kuntal Bhowmick
 
From Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial IntelligenceFrom Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial IntelligenceJonathan Mugan
 
Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2Kuntal Bhowmick
 

Similar to Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak - TomTom DevDay 2023 (19)

I want a pony
I want a ponyI want a pony
I want a pony
 
Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)
 
Using binary classifiers
Using binary classifiersUsing binary classifiers
Using binary classifiers
 
BBQ BASH Technology Rant - June 2018
BBQ BASH Technology Rant - June 2018BBQ BASH Technology Rant - June 2018
BBQ BASH Technology Rant - June 2018
 
State of NLP and Amazon Comprehend
State of NLP and Amazon ComprehendState of NLP and Amazon Comprehend
State of NLP and Amazon Comprehend
 
Just the basics_strata_2013
Just the basics_strata_2013Just the basics_strata_2013
Just the basics_strata_2013
 
Introduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad balochIntroduction of object oriented analysis & design by sarmad baloch
Introduction of object oriented analysis & design by sarmad baloch
 
Fred sw jpaper2017
Fred sw jpaper2017Fred sw jpaper2017
Fred sw jpaper2017
 
Delta final paper
Delta final paperDelta final paper
Delta final paper
 
Hickey jvm summit2009
Hickey jvm summit2009Hickey jvm summit2009
Hickey jvm summit2009
 
The Role Of Ontology In Modern Expert Systems Dallas 2008
The Role Of Ontology In Modern Expert Systems   Dallas   2008The Role Of Ontology In Modern Expert Systems   Dallas   2008
The Role Of Ontology In Modern Expert Systems Dallas 2008
 
M01_OO_Intro.ppt
M01_OO_Intro.pptM01_OO_Intro.ppt
M01_OO_Intro.ppt
 
Debugging Your Ext JS Code
Debugging Your Ext JS CodeDebugging Your Ext JS Code
Debugging Your Ext JS Code
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesome
 
CogSigma
CogSigmaCogSigma
CogSigma
 
Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2
 
From Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial IntelligenceFrom Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial Intelligence
 
Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2Class notes(week 2) on basic concepts of oop-2
Class notes(week 2) on basic concepts of oop-2
 

More from Andrzej Jóźwiak

Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022Andrzej Jóźwiak
 
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...Andrzej Jóźwiak
 
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...Andrzej Jóźwiak
 
Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021
Capability Driven Design - Andrzej Jóźwiak  - TomTom Dev Day 2021Capability Driven Design - Andrzej Jóźwiak  - TomTom Dev Day 2021
Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021Andrzej Jóźwiak
 
Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020Andrzej Jóźwiak
 
Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020Andrzej Jóźwiak
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Andrzej Jóźwiak
 
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...Andrzej Jóźwiak
 

More from Andrzej Jóźwiak (8)

Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
Does testability imply good design - Andrzej Jóźwiak - TomTom Dev Day 2022
 
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
Introduction to the Kotlin programming language - Andrzej Jóźwiak - JUG Łódź ...
 
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
Property based tests and where to find them - Andrzej Jóźwiak - TomTom Webina...
 
Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021
Capability Driven Design - Andrzej Jóźwiak  - TomTom Dev Day 2021Capability Driven Design - Andrzej Jóźwiak  - TomTom Dev Day 2021
Capability Driven Design - Andrzej Jóźwiak - TomTom Dev Day 2021
 
Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020Types of Randomness in Game Design - Rapid Talks - December 2020
Types of Randomness in Game Design - Rapid Talks - December 2020
 
Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020Capability Driven Design - Rapid Talks - November 2020
Capability Driven Design - Rapid Talks - November 2020
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
 
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
JUnit 4 Can it still teach us something? - Andrzej Jóźwiak - Kariera IT Łodź ...
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 

Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak - TomTom DevDay 2023

  • 1. Encapsulation – the pitfalls of Object-Oriented Programming Andrzej Jóźwiak
  • 2. Or am I just exaggerating? The root of all evil?
  • 3. Or am I just completely mad? But why?
  • 4. We mostly write CRUD applications in all honesty! Performance
  • 5. Is there anyone who still defends it? Still, it almost touches the problems with OOP Inheritance
  • 6. What is the similarity between Age of Empires 2 and Minecraft's armor stand? Age of Minecraft?
  • 7. Or you can just call it hierarchy if you like Taxonomy
  • 8. By combining state and functions, but why? Abstraction
  • 9.
  • 10. Just some state and behaviours? What is an object? Object State Behaviour State State Behaviour Behaviour
  • 11. Private information hidden behind a public interface What is an object? Object Message Message Message Message
  • 12. Message passing, or is it? How do we pass the messages? Most importantly what do we pass? What is an object? Object Object Object Object Object Object Object
  • 13. Or is it a bad practice? Should messages pass references? class Foo(val value: Int) { fun doSomething(bar: Bar) { //... } } class Bar(val value: String) { fun doElse(baz: Baz) { //... } } class Baz(val value: String) { fun doDifferent(foo: Foo) { //... } } val foo = Foo(value = 2023) val bar = Bar(value = "DevDay") val baz = Baz(value = "TomTom") foo.doSomething(bar) //if we pass the references bar.doElse(baz) //and they become the internal //state baz.doDifferent(foo) //then who owns them?
  • 14. Object should be solely responsible for its internal state? Should objects share references? class Foo( val bar: Bar, val baz: Baz, ) class Bar(val value: String) class Baz(val value: String) val bar = Bar(value = "DevDay") val baz = Baz(value = "TomTom") val foo = Foo(bar, baz) val qux = Qux(bar, baz) //both Bar and Baz instances are part of the internal //state of Foo and Qux, so which one of these two //has full responsibility of its encapsulated state? class Qux( val bar: Bar, val baz: Baz, )
  • 15. But with some extra steps! Isn't this just shared state? Bar Baz Foo Qux If Baz and Bar are part of Foo's internal state... If Baz and Bar are part of Qux's internal state... Does it mean that Qux can affect Foo's state? Does it mean that Foo can affect Qux's state?
  • 16. Then objects should not be shared and have only a single parent? If encapsulation is about hiding... A B D F G C E A B C D E F G Let's change a free form graph to a tree!
  • 17. Then what should we do if we need to communication between objects? If everything only has a single parent... A B C D E F G A B C D E F G
  • 18. Are we doomed? Imagine that static does not exist... A B C D E F G A B C D E F G
  • 19. If you squint your eyes isn't this the same as with any taxonomy? Maybe we need to change something... A B C D E F G A B D E F H G C New Object Maybe soon will not be needed? New root object How many times we will have to do it?
  • 20. Better safe than sorry... Spaghetti code leads to injuries!
  • 21. Yes, yes it can! Can encapsulation be premature?
  • 22. Imposing structure is worse than no structure at all! Think about data and processing separately! Class hierarchy Single responsibility Composition graph Objects data flow Object transformation Temporal dependencies Design patterns Behaviors Module relationship
  • 23. Nothing special just a simple association Let's play a game State State State State State State State Behavior Behavior Behavior Behavior Behavior Behavior Behavior Behavior What to do with leftovers?
  • 24. IThinkIWillNeedItController JustForThatOneThingHelper IDoNotKnowWhereToPutItManager Let's put them in a Manager, Controller, Helper or a Do-er! What to do with leftovers? State State Behavior Behavior Behavior
  • 25. Should Message send itself? Should Message receive itself? The OO dilemma
  • 26. About the preaching I was not totally honest...
  • 27. 1. Write methods when the association with the data type is certain. It is easy to get it wrong! 2. Minimize state instead of only segregating it. 3. Prefer immutability for the shared state. 4. When all hope is lost parametrize the behavior. 5. Polymorphism is not limited to only classes! 6. Prefer pure functions, if not constrained by the efficiency. 7. Encapsulate on module/namespace level. 8. It is ok to sometimes repeat or have redundant pieces. Long functions are not wrong. 9. Limit surface layer. It is much easier to comprehend only a few functions comparing to thousands of small ones. So how should we design our code?
  • 28. Thank you for sticking till the end! Any questions?