SlideShare a Scribd company logo
1 of 14
Download to read offline
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, there are common patterns of problems.
Design patterns are common solutions to those problems.
The ValueObject is a simple design pattern that you can use right away.
The Problem
public function changeName($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;
}
Have you ever written code like this?
Guarding
This is called Guarding
You are guarding against bad input
The idea is, you can’t trust that other
objects/developers are actually sending you valid
data, you’re trying to protect against inconsistent
state.
Guarding is a defensive patterns and is encoraged
in the book “The Mythical Man Month”.
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.
Leading to more bugs, not less. Developers
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 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.
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;
. . . . . .
Using the Name ValueObject
//Controller method to change name
public function changeName()
{
$user = $this->get_user($this->user_id);
$name = new Name(Input::get(‘firstname’), Input::get(‘lastname’));
$user->changeName($name);
}
. . . . . .
//Then in our User class
public function changeName(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 them 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.
Key features in the wild
They are immutable
They are comparable
Quick Recap:
ValueObjects . . .
● Encapsulate a 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
Thanks for listening
If you’re interested in learning more
about this pattern and others like it, I’d
recommend
Domain Driven Design.
Q&A
“Judge a man by his questions rather than by his answers.”
- Voltaire
“I needed content for this slide, so I added quotes.”
- Barry O Sullivan

More Related Content

What's hot

Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2
Wildan Maulana
 
Object-oriented Development with PL-SQL
Object-oriented Development with PL-SQLObject-oriented Development with PL-SQL
Object-oriented Development with PL-SQL
Donald Bales
 

What's hot (20)

OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
J query
J queryJ query
J query
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Code smells and refactoring
Code smells and refactoringCode smells and refactoring
Code smells and refactoring
 
PHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOPPHPID online Learning #6 Migration from procedural to OOP
PHPID online Learning #6 Migration from procedural to OOP
 
J query1
J query1J query1
J query1
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
 
Php Oop
Php OopPhp Oop
Php Oop
 
Let's write secure drupal code!
Let's write secure drupal code!Let's write secure drupal code!
Let's write secure drupal code!
 
Let's write secure Drupal code!
Let's write secure Drupal code!Let's write secure Drupal code!
Let's write secure Drupal code!
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Vision academy classes bcs_bca_bba_sybba_php
Vision academy  classes bcs_bca_bba_sybba_phpVision academy  classes bcs_bca_bba_sybba_php
Vision academy classes bcs_bca_bba_sybba_php
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Forms
 
jQuery - Tips And Tricks
jQuery - Tips And TricksjQuery - Tips And Tricks
jQuery - Tips And Tricks
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2Object Oriented Programming With PHP 5 #2
Object Oriented Programming With PHP 5 #2
 
Object-oriented Development with PL-SQL
Object-oriented Development with PL-SQLObject-oriented Development with PL-SQL
Object-oriented Development with PL-SQL
 
Optionals Swift - Swift Paris Junior #3
Optionals Swift - Swift Paris Junior #3 Optionals Swift - Swift Paris Junior #3
Optionals Swift - Swift Paris Junior #3
 

Similar to Value objects

Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
Jean Michel
 

Similar to Value objects (20)

Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
 
Lithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate FrameworksLithium: The Framework for People Who Hate Frameworks
Lithium: The Framework for People Who Hate Frameworks
 
Symfony2 - extending the console component
Symfony2 - extending the console componentSymfony2 - extending the console component
Symfony2 - extending the console component
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
Demystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHPDemystifying Object-Oriented Programming - Midwest PHP
Demystifying Object-Oriented Programming - Midwest PHP
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
WordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPressWordCamp Portland 2018: PHP for WordPress
WordCamp Portland 2018: PHP for WordPress
 
Coding for Scale and Sanity
Coding for Scale and SanityCoding for Scale and Sanity
Coding for Scale and Sanity
 
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
4Developers 2015: Be pragmatic, be SOLID - Krzysztof Menżyk
 
Be pragmatic, be SOLID
Be pragmatic, be SOLIDBe pragmatic, be SOLID
Be pragmatic, be SOLID
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHP
 
Taming Command Bus
Taming Command BusTaming Command Bus
Taming Command Bus
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
Value Objects
Value ObjectsValue Objects
Value Objects
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Pim Elshoff "Technically DDD"
Pim Elshoff "Technically DDD"Pim Elshoff "Technically DDD"
Pim Elshoff "Technically DDD"
 

More from Barry O Sullivan

More from Barry O Sullivan (6)

Building a blog with an Onion Architecture
Building a blog with an Onion ArchitectureBuilding a blog with an Onion Architecture
Building a blog with an Onion Architecture
 
Managing expectations
Managing expectationsManaging expectations
Managing expectations
 
DDD: lessons learned
DDD: lessons learnedDDD: lessons learned
DDD: lessons learned
 
Php test fest
Php test festPhp test fest
Php test fest
 
Cleaning up your codebase with a clean architecture
Cleaning up your codebase with a clean architectureCleaning up your codebase with a clean architecture
Cleaning up your codebase with a clean architecture
 
Design patterns - The Good, the Bad, and the Anti-Pattern
Design patterns -  The Good, the Bad, and the Anti-PatternDesign patterns -  The Good, the Bad, and the Anti-Pattern
Design patterns - The Good, the Bad, and the Anti-Pattern
 

Recently uploaded

Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Lisi Hocke
 

Recently uploaded (20)

Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
What is a Recruitment Management Software?
What is a Recruitment Management Software?What is a Recruitment Management Software?
What is a Recruitment Management Software?
 
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
Abortion Clinic In Springs ](+27832195400*)[ 🏥 Safe Abortion Pills in Springs...
 
Sinoville Clinic ](+27832195400*)[🏥Abortion Pill Prices Sinoville ● Women's A...
Sinoville Clinic ](+27832195400*)[🏥Abortion Pill Prices Sinoville ● Women's A...Sinoville Clinic ](+27832195400*)[🏥Abortion Pill Prices Sinoville ● Women's A...
Sinoville Clinic ](+27832195400*)[🏥Abortion Pill Prices Sinoville ● Women's A...
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
architecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdfarchitecting-ai-in-the-enterprise-apis-and-applications.pdf
architecting-ai-in-the-enterprise-apis-and-applications.pdf
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
^Clinic ^%[+27788225528*Abortion Pills For Sale In harare
 
Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14Spring into AI presented by Dan Vega 5/14
Spring into AI presented by Dan Vega 5/14
 
^Clinic ^%[+27788225528*Abortion Pills For Sale In birch acres
^Clinic ^%[+27788225528*Abortion Pills For Sale In birch acres^Clinic ^%[+27788225528*Abortion Pills For Sale In birch acres
^Clinic ^%[+27788225528*Abortion Pills For Sale In birch acres
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdf
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
 

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, there are common patterns of problems. Design patterns are common solutions to those problems. The ValueObject is a simple design pattern that you can use right away.
  • 4. The Problem public function changeName($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; } Have you ever written code like this?
  • 5. Guarding This is called Guarding You are guarding against bad input The idea is, you can’t trust that other objects/developers are actually sending you valid data, you’re trying to protect against inconsistent state. Guarding is a defensive patterns and is encoraged in the book “The Mythical Man Month”.
  • 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. Leading to more bugs, not less. Developers
  • 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 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.
  • 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; . . . . . .
  • 9. Using the Name ValueObject //Controller method to change name public function changeName() { $user = $this->get_user($this->user_id); $name = new Name(Input::get(‘firstname’), Input::get(‘lastname’)); $user->changeName($name); } . . . . . . //Then in our User class public function changeName(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 them 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. Key features in the wild They are immutable They are comparable
  • 12. Quick Recap: ValueObjects . . . ● Encapsulate a 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.
  • 13. More reading Thanks for listening If you’re interested in learning more about this pattern and others like it, I’d recommend Domain Driven Design.
  • 14. Q&A “Judge a man by his questions rather than by his answers.” - Voltaire “I needed content for this slide, so I added quotes.” - Barry O Sullivan