SlideShare a Scribd company logo
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
Encapsulation – the pitfalls of Object-Oriented Programming - Andrzej Jóźwiak - TomTom DevDay 2023
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

PPTX
Oo concepts and class modeling
PPTX
Encapsulation CodeCampIasi 16 oct 2010
PPTX
Java Object-Oriented Programming Conecpts(Real-Time) Examples
PPTX
PPT
Md02 - Getting Started part-2
PDF
Object Oriented Programming in Swift Ch0 - Encapsulation
PDF
Java conventions
PPT
Oop’s
Oo concepts and class modeling
Encapsulation CodeCampIasi 16 oct 2010
Java Object-Oriented Programming Conecpts(Real-Time) Examples
Md02 - Getting Started part-2
Object Oriented Programming in Swift Ch0 - Encapsulation
Java conventions
Oop’s

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

PPT
java
PPT
Java is an Object-Oriented Language
PPTX
Module 4 Effect of Reuse on using Encapsulation.pptx
PPTX
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
PPT
Chapter 02 The Object Model_Software E.ppt
PDF
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
PPTX
Object Oriented, Design patterns and data modelling worshop
DOCX
C# concepts
PPTX
SAD05 - Encapsulation
PPTX
Object Oriented Programming (OOP) Introduction
PPTX
Value Objects
PPT
Lukito Edi Nugroho - Object Orientation Concepts
PPTX
Introduction
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
java
Java is an Object-Oriented Language
Module 4 Effect of Reuse on using Encapsulation.pptx
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
Chapter 02 The Object Model_Software E.ppt
CS8392-OOPS-Printed-Notes-All-Units.pdf for students
Object Oriented, Design patterns and data modelling worshop
C# concepts
SAD05 - Encapsulation
Object Oriented Programming (OOP) Introduction
Value Objects
Lukito Edi Nugroho - Object Orientation Concepts
Introduction
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Ad

More from Andrzej Jóźwiak (8)

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

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Construction Project Organization Group 2.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
PPT on Performance Review to get promotions
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Geodesy 1.pptx...............................................
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
composite construction of structures.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Construction Project Organization Group 2.pptx
CH1 Production IntroductoryConcepts.pptx
Safety Seminar civil to be ensured for safe working.
PPT on Performance Review to get promotions
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Embodied AI: Ushering in the Next Era of Intelligent Systems
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Geodesy 1.pptx...............................................
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT 4 Total Quality Management .pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
additive manufacturing of ss316l using mig welding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Current and future trends in Computer Vision.pptx
Internet of Things (IOT) - A guide to understanding

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
  • 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?