SlideShare a Scribd company logo
Value Objects
Make your code simpler
Who am I
Product Developer - Systems Architect - Freelancer - Development Manager
Past experience
Senior Developer for Cleverbug
Lecturer at the Digital Skills Academy
Director of Tercet, software development consultancy
Current position
Software Development Manager for OliveMedia
Barry O Sullivan
Design Patterns
When developing systems, there are common patterns of problems that
emerge.
Design patterns are common, named solutions to these problems.
The ValueObject pattern is one of these patterns.
It’s simple, easy to integrate and can bring a lot of clarity to your code with
minimal investment or technical overhead.
So lets get to it . . .
The Problem
public function change_name($firstname, $lastname)
{
if (!is_string($firstname) || $firstname == “”) {
throw new InvalidNameException(“Firstname is invalid”);
}
if (!is_string($lastname) || $lastname == “”) {
throw new InvalidNameException(“Lastname is invalid”);
}
$this->firstname = $firstname;
$this->lastname = $lastname;
}
Have you ever written code like this?
Guarding
This is called Guarding.
You are guarding against bad input
The idea is that you can’t trust the data sent by other
objects/developers, it might be invalid and break your
system, or worse, it might seem to work until it breaks
later. Guarding protects you against inconsistent state.
Guarding is as old as software development, it’s even
recommended in the Mythical Man Month, which is
nearly 40yrs old.
Too much guarding
The problem arises when you start adding this logic everywhere.
You end up with
• Duplicated code
• Messy Logic
• Poor legibility
Which leads to a system that’s very
difficult to modify and understand.
This leads to more bugs, not less.
The Solution
How do we solve this problem?
With encapsulation!
You create a class that represents the value.
On creation, it gets passed the basic types and it checks if they’re valid.
• If they are not, an exception is thrown.
• If they are, then the object is successfully created.
That’s the basic gist of it. Everywhere you need to ensure
data integrity, use one of these and you can guarantee
That the data is valid.
Example, Name as a ValueObject
class Name implements ValueObject
{
private $firstname;
private $lastname;
public function __construct($firstname, $lastname)
{
if (!is_string($firstname) || $firstname == “”) {
throw new InvalidNameException(“Firstname ‘$firstname’ is invalid”);
}
if (!is_string($lastname) || $lastname == “”) {
throw new InvalidNameException(“Lastname ‘$lastname is invalid”);
}
$this->firstname = $firstname;
$this->lastname = $lastname;
}
………
Using the Name ValueObject
//Controller method to change name
public function change_name()
{
$user = $this->user_repository->fetch($this->user_id);
$name = new Name(Input::get(‘firstname’), Input::get(‘lastname’));
$user->change_name($name);
$this->user_repository->store($user);
}
. . . . . .
//Then in our User class
public function change_name(Name $name)
{
$this->name = $name;
}
ValueObjects represent the value
Key Concept:
A value object represents the value,
not the data contained within it.
You shouldn’t care about the internal data when using VOs for business logic.
When you want to reference a value, use a value object.
The only place the internals matter is when you’re encoding/decoding them for
transmission (JSON/HTML) or storage (MySQL/REDIS), nowhere else.
ValueObjects in the wild
1. They are immutable
Once set, you cannot change their values.
In other words, you cannot change their state.
If you need to change one, simply create a new one and replace the
reference to the old one.
2. They are comparable
Two value objects of the same type can be
compared to check their values. This makes
business rules easier to build and check.
(Works a treat with the specification pattern)
Some examples of ValueObjects
• Email address
• Price (Amount + Currency)
• ID
• Country Code
• TestScore
• Discount
• Perspiration
NB: Usually you would not use a ValueObject to represent a user, since a user
can change state. User should be an entity that uses VOs internally.
Quick Recap:
ValueObjects . . .
• Encapsulate a self contained value
• Allow you to guard against bad input
• Represent the value, not the data within
• Are immutable
• Make code and system design much easier, leading to
more structured system that’s easier to maintain.
More reading
If you’re interested in learning more about this
pattern and others like it, I’d recommend
looking into “Domain Driven Design”,
specifically Eric Evans book.
It’s a how-to book on using this pattern and
others like it to build large scale, distributed
micro-services.
Thanks for listening
Any questions?
Barry O Sullivan – barry@tercet.io - http://barryosullivan.me

More Related Content

What's hot

Advanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID PrinciplesAdvanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID Principles
Jon Kruger
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
Jyoti shukla
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
Serhiy Oplakanets
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
Sergey Karpushin
 
Extending the Xbase Typesystem
Extending the Xbase TypesystemExtending the Xbase Typesystem
Extending the Xbase Typesystem
Sebastian Zarnekow
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
Hibernate using jpa
Hibernate using jpaHibernate using jpa
Hibernate using jpa
Mohammad Faizan
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
Ravi Bhadauria
 
Introduction to Core Java Programming
Introduction to Core Java ProgrammingIntroduction to Core Java Programming
Introduction to Core Java Programming
Raveendra R
 
Beyond design patterns and principles - writing good OO code
Beyond design patterns and principles - writing good OO codeBeyond design patterns and principles - writing good OO code
Beyond design patterns and principles - writing good OO code
Matthias Noback
 
GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6
jimbojsb
 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
Seo Gyansha
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?
Colin Riley
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
Michael Peacock
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
Shawn Brito
 
Java Basic
Java Basic Java Basic
Java Basic
Mahmudul Hasan
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
Collaboration Technologies
 
Introduction to Object Oriented Concepts
Introduction to Object Oriented Concepts Introduction to Object Oriented Concepts
Introduction to Object Oriented Concepts
Mamoun Nawahdah
 
Pursuing practices of Domain-Driven Design in PHP
Pursuing practices of Domain-Driven Design in PHPPursuing practices of Domain-Driven Design in PHP
Pursuing practices of Domain-Driven Design in PHP
Giorgio Sironi
 

What's hot (20)

Advanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID PrinciplesAdvanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID Principles
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
 
Extending the Xbase Typesystem
Extending the Xbase TypesystemExtending the Xbase Typesystem
Extending the Xbase Typesystem
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
Hibernate using jpa
Hibernate using jpaHibernate using jpa
Hibernate using jpa
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Introduction to Core Java Programming
Introduction to Core Java ProgrammingIntroduction to Core Java Programming
Introduction to Core Java Programming
 
Beyond design patterns and principles - writing good OO code
Beyond design patterns and principles - writing good OO codeBeyond design patterns and principles - writing good OO code
Beyond design patterns and principles - writing good OO code
 
GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6GeekAustin PHP Class - Session 6
GeekAustin PHP Class - Session 6
 
Java essentials for hadoop
Java essentials for hadoopJava essentials for hadoop
Java essentials for hadoop
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
Java Basic
Java Basic Java Basic
Java Basic
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
 
Introduction to Object Oriented Concepts
Introduction to Object Oriented Concepts Introduction to Object Oriented Concepts
Introduction to Object Oriented Concepts
 
Pursuing practices of Domain-Driven Design in PHP
Pursuing practices of Domain-Driven Design in PHPPursuing practices of Domain-Driven Design in PHP
Pursuing practices of Domain-Driven Design in PHP
 

Viewers also liked

Values
ValuesValues
Values
BenEddy
 
Serializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara HacopianSerializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara Hacopian
SmartLogic
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objects
Simone Gentili
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Brendan Eich
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
Brendan Eich
 
Persisting Value Objects
Persisting Value ObjectsPersisting Value Objects
Persisting Value Objects
The Software House
 
A System Is Not a Tree
A System Is Not a TreeA System Is Not a Tree
A System Is Not a Tree
Kevlin Henney
 
Aggregates, Entities and Value objects - Devnology 2010 community day
Aggregates, Entities and Value objects - Devnology 2010 community dayAggregates, Entities and Value objects - Devnology 2010 community day
Aggregates, Entities and Value objects - Devnology 2010 community day
Rick van der Arend
 

Viewers also liked (8)

Values
ValuesValues
Values
 
Serializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara HacopianSerializing Value Objects-Ara Hacopian
Serializing Value Objects-Ara Hacopian
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objects
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
 
Value objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progressValue objects in JS - an ES7 work in progress
Value objects in JS - an ES7 work in progress
 
Persisting Value Objects
Persisting Value ObjectsPersisting Value Objects
Persisting Value Objects
 
A System Is Not a Tree
A System Is Not a TreeA System Is Not a Tree
A System Is Not a Tree
 
Aggregates, Entities and Value objects - Devnology 2010 community day
Aggregates, Entities and Value objects - Devnology 2010 community dayAggregates, Entities and Value objects - Devnology 2010 community day
Aggregates, Entities and Value objects - Devnology 2010 community day
 

Similar to Value Objects

Value objects
Value objectsValue objects
Value objects
Barry O Sullivan
 
The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)
Matthias Noback
 
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
Mike Harris
 
The quest for global design principles - PHP Benelux 2016
The quest for global design principles - PHP Benelux 2016The quest for global design principles - PHP Benelux 2016
The quest for global design principles - PHP Benelux 2016
Matthias Noback
 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
SreeVani74
 
Clean code
Clean codeClean code
Clean code
Touhid Arastu
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
Jay Shirley
 
What You Missed in Computer Science
What You Missed in Computer ScienceWhat You Missed in Computer Science
What You Missed in Computer Science
Taylor Lovett
 
The Quest for Global Design Principles
The Quest for Global Design PrinciplesThe Quest for Global Design Principles
The Quest for Global Design Principles
Matthias Noback
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
HamletDRC
 
ER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdf
SadiaSharmin40
 
Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentals
ManjuKumara GH
 
Writing maintainable Oracle PL/SQL code
Writing maintainable Oracle PL/SQL codeWriting maintainable Oracle PL/SQL code
Writing maintainable Oracle PL/SQL code
Donald Bales
 
Writing Maintainable Code
Writing Maintainable CodeWriting Maintainable Code
Writing Maintainable Code
Donald Bales
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
Rahul Jain
 
Professional Code Reviews - Bogdan Gusiev
Professional Code Reviews - Bogdan GusievProfessional Code Reviews - Bogdan Gusiev
Professional Code Reviews - Bogdan Gusiev
Ruby Meditation
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
Miklos Csere
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove
 
Why Our Code Smells
Why Our Code SmellsWhy Our Code Smells
Why Our Code Smells
TiNguyn863920
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
Jamshid Hashimi
 

Similar to Value Objects (20)

Value objects
Value objectsValue objects
Value objects
 
The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)The quest for global design principles (SymfonyLive Berlin 2015)
The quest for global design principles (SymfonyLive Berlin 2015)
 
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
 
The quest for global design principles - PHP Benelux 2016
The quest for global design principles - PHP Benelux 2016The quest for global design principles - PHP Benelux 2016
The quest for global design principles - PHP Benelux 2016
 
AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
 
Clean code
Clean codeClean code
Clean code
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
 
What You Missed in Computer Science
What You Missed in Computer ScienceWhat You Missed in Computer Science
What You Missed in Computer Science
 
The Quest for Global Design Principles
The Quest for Global Design PrinciplesThe Quest for Global Design Principles
The Quest for Global Design Principles
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
ER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdfER diagram slides for datanase stujdy-1.pdf
ER diagram slides for datanase stujdy-1.pdf
 
Data weave 2.0 language fundamentals
Data weave 2.0 language fundamentalsData weave 2.0 language fundamentals
Data weave 2.0 language fundamentals
 
Writing maintainable Oracle PL/SQL code
Writing maintainable Oracle PL/SQL codeWriting maintainable Oracle PL/SQL code
Writing maintainable Oracle PL/SQL code
 
Writing Maintainable Code
Writing Maintainable CodeWriting Maintainable Code
Writing Maintainable Code
 
Introduction to Machine Learning
Introduction to Machine LearningIntroduction to Machine Learning
Introduction to Machine Learning
 
Professional Code Reviews - Bogdan Gusiev
Professional Code Reviews - Bogdan GusievProfessional Code Reviews - Bogdan Gusiev
Professional Code Reviews - Bogdan Gusiev
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
 
Why Our Code Smells
Why Our Code SmellsWhy Our Code Smells
Why Our Code Smells
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
 

Recently uploaded

ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
anfaltahir1010
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
aisafed42
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
Reetu63
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
Maitrey Patel
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 

Recently uploaded (20)

ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLESINTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
INTRODUCTION TO AI CLASSICAL THEORY TARGETED EXAMPLES
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabhQuarter 3 SLRP grade 9.. gshajsbhhaheabh
Quarter 3 SLRP grade 9.. gshajsbhhaheabh
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
ppt on the brain chip neuralink.pptx
ppt  on   the brain  chip neuralink.pptxppt  on   the brain  chip neuralink.pptx
ppt on the brain chip neuralink.pptx
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.ACE - Team 24 Wrapup event at ahmedabad.
ACE - Team 24 Wrapup event at ahmedabad.
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 

Value Objects

  • 1. Value Objects Make your code simpler
  • 2. Who am I Product Developer - Systems Architect - Freelancer - Development Manager Past experience Senior Developer for Cleverbug Lecturer at the Digital Skills Academy Director of Tercet, software development consultancy Current position Software Development Manager for OliveMedia Barry O Sullivan
  • 3. Design Patterns When developing systems, there are common patterns of problems that emerge. Design patterns are common, named solutions to these problems. The ValueObject pattern is one of these patterns. It’s simple, easy to integrate and can bring a lot of clarity to your code with minimal investment or technical overhead. So lets get to it . . .
  • 4. The Problem public function change_name($firstname, $lastname) { if (!is_string($firstname) || $firstname == “”) { throw new InvalidNameException(“Firstname is invalid”); } if (!is_string($lastname) || $lastname == “”) { throw new InvalidNameException(“Lastname is invalid”); } $this->firstname = $firstname; $this->lastname = $lastname; } Have you ever written code like this?
  • 5. Guarding This is called Guarding. You are guarding against bad input The idea is that you can’t trust the data sent by other objects/developers, it might be invalid and break your system, or worse, it might seem to work until it breaks later. Guarding protects you against inconsistent state. Guarding is as old as software development, it’s even recommended in the Mythical Man Month, which is nearly 40yrs old.
  • 6. Too much guarding The problem arises when you start adding this logic everywhere. You end up with • Duplicated code • Messy Logic • Poor legibility Which leads to a system that’s very difficult to modify and understand. This leads to more bugs, not less.
  • 7. The Solution How do we solve this problem? With encapsulation! You create a class that represents the value. On creation, it gets passed the basic types and it checks if they’re valid. • If they are not, an exception is thrown. • If they are, then the object is successfully created. That’s the basic gist of it. Everywhere you need to ensure data integrity, use one of these and you can guarantee That the data is valid.
  • 8. Example, Name as a ValueObject class Name implements ValueObject { private $firstname; private $lastname; public function __construct($firstname, $lastname) { if (!is_string($firstname) || $firstname == “”) { throw new InvalidNameException(“Firstname ‘$firstname’ is invalid”); } if (!is_string($lastname) || $lastname == “”) { throw new InvalidNameException(“Lastname ‘$lastname is invalid”); } $this->firstname = $firstname; $this->lastname = $lastname; } ………
  • 9. Using the Name ValueObject //Controller method to change name public function change_name() { $user = $this->user_repository->fetch($this->user_id); $name = new Name(Input::get(‘firstname’), Input::get(‘lastname’)); $user->change_name($name); $this->user_repository->store($user); } . . . . . . //Then in our User class public function change_name(Name $name) { $this->name = $name; }
  • 10. ValueObjects represent the value Key Concept: A value object represents the value, not the data contained within it. You shouldn’t care about the internal data when using VOs for business logic. When you want to reference a value, use a value object. The only place the internals matter is when you’re encoding/decoding them for transmission (JSON/HTML) or storage (MySQL/REDIS), nowhere else.
  • 11. ValueObjects in the wild 1. They are immutable Once set, you cannot change their values. In other words, you cannot change their state. If you need to change one, simply create a new one and replace the reference to the old one. 2. They are comparable Two value objects of the same type can be compared to check their values. This makes business rules easier to build and check. (Works a treat with the specification pattern)
  • 12. Some examples of ValueObjects • Email address • Price (Amount + Currency) • ID • Country Code • TestScore • Discount • Perspiration NB: Usually you would not use a ValueObject to represent a user, since a user can change state. User should be an entity that uses VOs internally.
  • 13. Quick Recap: ValueObjects . . . • Encapsulate a self contained value • Allow you to guard against bad input • Represent the value, not the data within • Are immutable • Make code and system design much easier, leading to more structured system that’s easier to maintain.
  • 14. More reading If you’re interested in learning more about this pattern and others like it, I’d recommend looking into “Domain Driven Design”, specifically Eric Evans book. It’s a how-to book on using this pattern and others like it to build large scale, distributed micro-services.
  • 15. Thanks for listening Any questions? Barry O Sullivan – barry@tercet.io - http://barryosullivan.me