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
PDF
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
PPT
Functions in php
PDF
Java 8 Streams and Rx Java Comparison
PDF
JDK8 : parallel programming made (too ?) easy
PDF
Free your lambdas
PDF
Javaz. Functional design in Java 8.
PDF
IoC&Laravel
Beyond design patterns phpnw14
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Functions in php
Java 8 Streams and Rx Java Comparison
JDK8 : parallel programming made (too ?) easy
Free your lambdas
Javaz. Functional design in Java 8.
IoC&Laravel

What's hot (20)

PPTX
Aspects of love slideshare
PDF
Java 8 Lambda Expressions & Streams
PDF
How to get along with implicits
PDF
Introduction to functional programming (In Arabic)
PPTX
Clean Code Principles
PPTX
The Sincerest Form of Flattery
PDF
Functional Programming Essentials
PDF
Free your lambdas
PDF
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
PDF
Java 8 Stream API and RxJava Comparison
PDF
Going reactive in java
PDF
The Functional Programming Toolkit (NDC Oslo 2019)
PDF
Java 8, Streams & Collectors, patterns, performances and parallelization
PPTX
Java 8 presentation
PPTX
Functional programming with Java 8
PDF
Booting into functional programming
PDF
Building confidence in concurrent code with a model checker: TLA+ for program...
PDF
Reinventing the Transaction Script (NDC London 2020)
PPT
Introduction To Functional Programming
PDF
Functional Java 8 - Introduction
Aspects of love slideshare
Java 8 Lambda Expressions & Streams
How to get along with implicits
Introduction to functional programming (In Arabic)
Clean Code Principles
The Sincerest Form of Flattery
Functional Programming Essentials
Free your lambdas
Python For Data Analysis | Python Pandas Tutorial | Learn Python | Python Tra...
Java 8 Stream API and RxJava Comparison
Going reactive in java
The Functional Programming Toolkit (NDC Oslo 2019)
Java 8, Streams & Collectors, patterns, performances and parallelization
Java 8 presentation
Functional programming with Java 8
Booting into functional programming
Building confidence in concurrent code with a model checker: TLA+ for program...
Reinventing the Transaction Script (NDC London 2020)
Introduction To Functional Programming
Functional Java 8 - Introduction
Ad

Viewers also liked (7)

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

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

DOC
Jsphp 110312161301-phpapp02
PDF
JavaScript for PHP developers
PDF
Generating Power with Yield
PDF
Why async and functional programming in PHP7 suck and how to get overr it?
PDF
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
PDF
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PDF
Crossing the Bridge: Connecting Rails and your Front-end Framework
PPT
Php Chapter 1 Training
PDF
Čtvrtkon #53 - Štěpán Zikmund
PDF
Revisiting SOLID Principles
PDF
FP in Java - Project Lambda and beyond
PDF
PDF
Solid principles
PDF
Bootstrat REST APIs with Laravel 5
PDF
Swift internals
PDF
Giới thiệu PHP 7
ODP
Writing Maintainable Perl
PDF
Durian: a PHP 5.5 microframework with generator-style middleware
PDF
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
KEY
Jsphp 110312161301-phpapp02
JavaScript for PHP developers
Generating Power with Yield
Why async and functional programming in PHP7 suck and how to get overr it?
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
PHPCon 2016: PHP7 by Witek Adamus / XSolve
Crossing the Bridge: Connecting Rails and your Front-end Framework
Php Chapter 1 Training
Čtvrtkon #53 - Štěpán Zikmund
Revisiting SOLID Principles
FP in Java - Project Lambda and beyond
Solid principles
Bootstrat REST APIs with Laravel 5
Swift internals
Giới thiệu PHP 7
Writing Maintainable Perl
Durian: a PHP 5.5 microframework with generator-style middleware
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine

Recently uploaded (20)

PPTX
observCloud-Native Containerability and monitoring.pptx
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
The various Industrial Revolutions .pptx
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
TLE Review Electricity (Electricity).pptx
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Tartificialntelligence_presentation.pptx
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Architecture types and enterprise applications.pdf
observCloud-Native Containerability and monitoring.pptx
O2C Customer Invoices to Receipt V15A.pptx
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
The various Industrial Revolutions .pptx
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Web App vs Mobile App What Should You Build First.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
TLE Review Electricity (Electricity).pptx
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
cloud_computing_Infrastucture_as_cloud_p
DP Operators-handbook-extract for the Mautical Institute
Developing a website for English-speaking practice to English as a foreign la...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Tartificialntelligence_presentation.pptx
Zenith AI: Advanced Artificial Intelligence
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Architecture types and enterprise applications.pdf

Don't Be STUPID, Grasp SOLID - ConFoo Edition