SlideShare a Scribd company logo
Don’t Be STUPID
Grasp SOLID!
Anthony Ferrara
Let’s Talk
Object
Oriented
Programming
What
Is An
Object?
Classic View
Object == Physical “Thing”
Classic View
Object == Physical “Thing”
Methods == Actions on “Thing”
Classic View
Object == Physical “Thing”
Methods == Actions on “Thing”
Properties == Description of “Thing”
Animal
MammalBird Fish
CatCow Dog
Lion Feline Cheetah
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Is This Realistic?
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
(9 Months Later)
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Does A Lion Have
A Button To Make
It Roar?
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Does A Lion Have
A Button To Make
It Roar?What Does It
Mean For An
Object To “Hunt”?
Classic View
$lion = new Lion;
$lion->roar();
$lion->walkTo($point);
$lion->hunt($zebra);
$lion->sleep();
Does A Lion Have
A Button To Make
It Roar?What Does It
Mean For An
Object To “Hunt”?
What Is A Lion In
Relation To Our
Application?
The Classical
Model Is Easy To
Understand
Don't Be STUPID, Grasp SOLID - ConFoo Edition
The Classical
Model Is
Completely
Impractical
“Modern” View
Object == Collection Of (Related)
Behaviors
“Modern” View
Object == Collection Of (Related)
Behaviors
Methods == Behavior
“Modern” View
Object == Collection Of (Related)
Behaviors
Methods == Behavior
Properties == Details Of Behavior
Classic View == “(conceptually) is a”
Modern View == “behaves as a”
interface Number {
function getValue();
function __toString();
function add(Number $n);
function subtract(Number $n);
function equals(Number $n);
function isLessThan(Number $n);
function isGreaterThan(Number $n);
}
Number
IntegerFloat Decimal
longshort long long
unsigned signed
But Wait!
We Don’t Even
Need Inheritance
All We Need Is
Polymorphism
And Encapsulation
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Polymorphism
Behavior Is Determined Dynamically
“Dynamic Dispatch”
Procedural Code
if ($a->isLong()) {
return new Long($a->getValue() + 1);
} elseif ($a->isFloat()) {
return new Float($a->getValue() + 1.0);
} elseif ($a->isDecimal()) {
return new Decimal($a->getValue() +
1.0);
}
Polymorphic Code
return $number->add(new Integer(1));
Polymorphic Code
class Integer implements Number {
public function add(Number $a) {
return new Integer(
$this->getValue() +
(int) $a->getValue()
);
}
}
Polymorphic Code
class Float implements Number {
public function add(Number $a) {
return new Float(
$this->getValue() +
(float) $a->getValue()
);
}
}
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Encapsulation
Behavior Is Completely Contained By
The Object’s API
(Information Hiding)
Procedural Code
if (5 == $number->value) {
print “Number Is 5”;
} else {
print “Number Is Not 5”;
}
Encapsulated Code
if ($number->equals(new Integer(5))) {
print “Number Is 5”;
} else {
print “Number Is Not 5”;
}
Encapsulated Code
class Decimal implements Number {
protected $intValue;
protected $exponent;
public function equals(Number $a) {
if ($a instanceof Decimal) {
// Compare Directly
} else {
// Cast
}
}
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Behavior Is
Defined By The
API
Two Types Of Primitive APIs
Interfaces (Explicit)
Two Types Of Primitive APIs
Interfaces (Explicit)
Duck Typing (Implicit)
If an Object Is A
Collection Of
Behaviors...
What Is A
Collection Of
Classes/Objects?
APIs
Method
APIs
Method MethodMethod
Class
APIs
Method MethodMethod
Class ClassClass
Package
APIs
Method MethodMethod
Class ClassClass
Package PackagePackage
Library
APIs
Method MethodMethod
Class ClassClass
Package PackagePackage
Library
Framework
LibraryLibrary
Don't Be STUPID, Grasp SOLID - ConFoo Edition
What Makes A
Good API?
A Good API
Does One Thing
A Good API
Never Changes
A Good API
Behaves Like Its
Contract
A Good API
Has A Narrow
Responsibility
A Good API
Depends Upon
Abstractions
And That’s
SOLID
Code
S - Single Responsibility Principle
O-
L -
I -
D-
A Good API
Does One
Thing
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Single Responsibility Principle
O- Open / Closed Principle
L -
I -
D-
A Good API
Never Changes
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I -
D-
A Good API
Behaves Like
Its Contract
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D-
A Good API
Has A Narrow
Responsibility
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D- Dependency Inversion Principle
A Good API
Depends Upon
Abstractions
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Single Responsibility Principle
O- Open / Closed Principle
L - Liskov Substitution Principle
I - Interface Segregation Principle
D- Dependency Inversion Principle
Note That SOLID
Does Not Dictate
What Is Good OOP
SOLID Emerges
From Good OOP
Don't Be STUPID, Grasp SOLID - ConFoo Edition
So, What Makes
A Bad API?
Global Variables
(Spooky Action
At A Distance)
Depending On
Specifics Of An
Implementation
Hidden
Dependencies
Unhealthy Focus
On Performance
Poorly Named
APIs
Duplication
And That’s
STUPID
Code
S - Singletons
T -
U -
P -
I -
D-
Global Variables
(Spooky Action
At A Distance)
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Singletons
T - Tight Coupling
U -
P -
I -
D-
Depending On
Specifics Of An
Implementation
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Singletons
T - Tight Coupling
U - Untestable Code
P -
I -
D-
Hidden
Dependencies
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I -
D-
Unhealthy Focus
On Performance
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I - Indescriptive Naming
D-
Poorly Named
APIs
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I - Indescriptive Naming
D- Duplication
Duplication
Duplication
DuplicationDuplication
Duplication
Duplication
Duplication
Duplication
Don't Be STUPID, Grasp SOLID - ConFoo Edition
S - Singletons
T - Tight Coupling
U - Untestable Code
P - Premature Optimization
I - Indescriptive Naming
D- Duplication
Don't Be STUPID, Grasp SOLID - ConFoo Edition
STUPID
Embodies
Lessons Learned
From Bad OOP
What Good OOP Gives You
Modular Code
Reusable Code
Extendable Code
Easy To Read Code
Maintainable Code
Easy To Change Code
Easy To Understand Code
Clean Abstractions (mostly)
What Good OOP Costs You
Tends To Have More “Layers”
Tends To Be Slower At Runtime
Tends To Have Larger Codebases
Tends To Result In Over-Abstraction
Tends To Require More Effort To Write
Tends To Require More Tacit Knowledge
Let’s Look At
Some Code!
interface Car {
public function turnLeft();
public function turnRight();
public function goFaster();
public function goSlower();
public function shiftUp();
public function shiftDown();
public function start();
}
interface Steerable {
public function steer($angle);
}
interface Acceleratable {
public function accelerate($amt);
}
interface Shiftable {
public function shiftDown();
public function shiftUp();
}
Let’s Look At
Drupal Code!
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Formatting Messages
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Formatting Messages
Encoding Messages
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Formatting Messages
Encoding Messages
Assembling Headers
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Formatting Messages
Encoding Messages
Assembling Headers
Calling Sendmail
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Formatting Messages
Encoding Messages
Assembling Headers
Calling Sendmail
Setting INI settings…?
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Open For Extension?
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Open For Extension?
Edits Require
Copy/Paste
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Open For Extension?
Liskov Substitution...
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Open For Extension?
Liskov Substitution...
One Interface...
Many Responsibilites
interface MailSystemInterface {
public function format(array $message);
public function mail(array $message);
}
What Responsibility?
Open For Extension?
Liskov Substitution...
One Interface...
What Dependencies?
interface MessageFormatter {
public function format(Message $message);
}
interface MessageEncoder {
public function encode(Message $message);
}
interface MessageTransport {
public function send(Message $message);
}
class MailSystem {
public function __construct(
MessageFormatter $messageFormatter,
MessageEncoder $messageEncoder,
MessageTransport $messageTransport
) {}
public function mail(Message $message);
}
Principle Of
Good
Enough
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Know the rules well
so you can break them
effectively...
Dalai Lama XIV
Anthony Ferrara
@ircmaxell
blog.ircmaxell.com
me@ircmaxell.com
youtube.com/ircmaxell

More Related Content

PDF
Beyond design patterns phpnw14
Anthony Ferrara
 
PDF
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Anthony Ferrara
 
PPT
Functions in php
Mudasir Syed
 
PDF
Java 8 Streams and Rx Java Comparison
José Paumard
 
PDF
JDK8 : parallel programming made (too ?) easy
José Paumard
 
PDF
Free your lambdas
José Paumard
 
PDF
Javaz. Functional design in Java 8.
Vadim Dubs
 
PDF
IoC&Laravel
Hoang Long
 
Beyond design patterns phpnw14
Anthony Ferrara
 
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Anthony Ferrara
 
Functions in php
Mudasir Syed
 
Java 8 Streams and Rx Java Comparison
José Paumard
 
JDK8 : parallel programming made (too ?) easy
José Paumard
 
Free your lambdas
José Paumard
 
Javaz. Functional design in Java 8.
Vadim Dubs
 
IoC&Laravel
Hoang Long
 

What's hot (20)

PPTX
Aspects of love slideshare
Mark Baker
 
PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
PDF
How to get along with implicits
Taisuke Oe
 
PDF
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
PPTX
Clean Code Principles
YeurDreamin'
 
PPTX
The Sincerest Form of Flattery
José Paumard
 
PDF
Functional Programming Essentials
Kelley Robinson
 
PDF
Free your lambdas
José Paumard
 
PDF
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Edureka!
 
PDF
Java 8 Stream API and RxJava Comparison
José Paumard
 
PDF
Going reactive in java
José Paumard
 
PDF
The Functional Programming Toolkit (NDC Oslo 2019)
Scott Wlaschin
 
PDF
Java 8, Streams & Collectors, patterns, performances and parallelization
José Paumard
 
PPTX
Java 8 presentation
Van Huong
 
PPTX
Functional programming with Java 8
LivePerson
 
PDF
Booting into functional programming
Dhaval Dalal
 
PDF
Building confidence in concurrent code with a model checker: TLA+ for program...
Scott Wlaschin
 
PDF
Reinventing the Transaction Script (NDC London 2020)
Scott Wlaschin
 
PPT
Introduction To Functional Programming
newmedio
 
PDF
Functional Java 8 - Introduction
Łukasz Biały
 
Aspects of love slideshare
Mark Baker
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
How to get along with implicits
Taisuke Oe
 
Introduction to functional programming (In Arabic)
Omar Abdelhafith
 
Clean Code Principles
YeurDreamin'
 
The Sincerest Form of Flattery
José Paumard
 
Functional Programming Essentials
Kelley Robinson
 
Free your lambdas
José Paumard
 
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Edureka!
 
Java 8 Stream API and RxJava Comparison
José Paumard
 
Going reactive in java
José Paumard
 
The Functional Programming Toolkit (NDC Oslo 2019)
Scott Wlaschin
 
Java 8, Streams & Collectors, patterns, performances and parallelization
José Paumard
 
Java 8 presentation
Van Huong
 
Functional programming with Java 8
LivePerson
 
Booting into functional programming
Dhaval Dalal
 
Building confidence in concurrent code with a model checker: TLA+ for program...
Scott Wlaschin
 
Reinventing the Transaction Script (NDC London 2020)
Scott Wlaschin
 
Introduction To Functional Programming
newmedio
 
Functional Java 8 - Introduction
Łukasz Biały
 
Ad

Viewers also liked (7)

KEY
Programming SOLID
Ladislav Martincik
 
PDF
Development by the numbers
Anthony Ferrara
 
PDF
Development By The Numbers - ConFoo Edition
Anthony Ferrara
 
PDF
Don't be STUPID, Grasp SOLID - North East PHP
Anthony Ferrara
 
PDF
Password Storage And Attacking In PHP - PHP Argentina
Anthony Ferrara
 
PDF
PHP, Under The Hood - DPC
Anthony Ferrara
 
PPTX
SOLID -Clean Code For Mere Mortals
Wekoslav Stefanovski
 
Programming SOLID
Ladislav Martincik
 
Development by the numbers
Anthony Ferrara
 
Development By The Numbers - ConFoo Edition
Anthony Ferrara
 
Don't be STUPID, Grasp SOLID - North East PHP
Anthony Ferrara
 
Password Storage And Attacking In PHP - PHP Argentina
Anthony Ferrara
 
PHP, Under The Hood - DPC
Anthony Ferrara
 
SOLID -Clean Code For Mere Mortals
Wekoslav Stefanovski
 
Ad

Similar to Don't Be STUPID, Grasp SOLID - ConFoo Edition (20)

DOC
Jsphp 110312161301-phpapp02
Seri Moth
 
PDF
JavaScript for PHP developers
Stoyan Stefanov
 
PDF
Generating Power with Yield
Jason Myers
 
PDF
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
PDF
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
Shohei Okada
 
PDF
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
PDF
Crossing the Bridge: Connecting Rails and your Front-end Framework
Daniel Spector
 
PPT
Php Chapter 1 Training
Chris Chubb
 
PDF
Čtvrtkon #53 - Štěpán Zikmund
Péhápkaři
 
PDF
Revisiting SOLID Principles
Anis Ahmad
 
PDF
FP in Java - Project Lambda and beyond
Mario Fusco
 
PDF
Slide
Naing Lin Aung
 
PDF
Solid principles
Bastian Feder
 
PDF
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 
PDF
Swift internals
Jung Kim
 
PDF
Giới thiệu PHP 7
ZendVN
 
ODP
Writing Maintainable Perl
tinypigdotcom
 
PDF
Durian: a PHP 5.5 microframework with generator-style middleware
Kuan Yen Heng
 
PDF
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 
KEY
Fatc
Wade Arnold
 
Jsphp 110312161301-phpapp02
Seri Moth
 
JavaScript for PHP developers
Stoyan Stefanov
 
Generating Power with Yield
Jason Myers
 
Why async and functional programming in PHP7 suck and how to get overr it?
Lucas Witold Adamus
 
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
Shohei Okada
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Daniel Spector
 
Php Chapter 1 Training
Chris Chubb
 
Čtvrtkon #53 - Štěpán Zikmund
Péhápkaři
 
Revisiting SOLID Principles
Anis Ahmad
 
FP in Java - Project Lambda and beyond
Mario Fusco
 
Solid principles
Bastian Feder
 
Bootstrat REST APIs with Laravel 5
Elena Kolevska
 
Swift internals
Jung Kim
 
Giới thiệu PHP 7
ZendVN
 
Writing Maintainable Perl
tinypigdotcom
 
Durian: a PHP 5.5 microframework with generator-style middleware
Kuan Yen Heng
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 

Recently uploaded (20)

PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Doc9.....................................
SofiaCollazos
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Software Development Methodologies in 2025
KodekX
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Doc9.....................................
SofiaCollazos
 

Don't Be STUPID, Grasp SOLID - ConFoo Edition