don't be stupid, grasp solid - drupalcon prague

129
Don’t Be STUPID Grasp SOLID! Anthony Ferrara

Upload: anthony-ferrara

Post on 18-May-2015

4.938 views

Category:

Technology


0 download

DESCRIPTION

Slides from my talk at DrupalCon Prague 2013: https://prague2013.drupal.org/session/dont-be-stupid-grasp-solid

TRANSCRIPT

Page 1: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Don’t Be STUPIDGrasp SOLID!

Anthony Ferrara

Page 2: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Let’s TalkObject

OrientedProgramming

Page 3: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

WhatIs An

Object?

Page 4: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Classic View

Object == Physical “Thing”

Page 5: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Classic View

Object == Physical “Thing”

Methods == Actions on “Thing”

Page 6: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Classic View

Object == Physical “Thing”

Methods == Actions on “Thing”

Properties == Description of “Thing”

Page 7: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Animal

MammalBird Fish

CatCow Dog

Lion Feline Cheetah

Page 8: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 9: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Classic View

$lion = new Lion;$lion->roar();$lion->walkTo($point);$lion->hunt($zebra);$lion->sleep();

Page 10: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Is This Realistic?

Page 11: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Classic View

$lion = new Lion;$lion->roar();$lion->walkTo($point);$lion->hunt($zebra);$lion->sleep();

Page 12: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

(9 Months Later)

Page 13: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Classic View

$lion = new Lion;$lion->roar();$lion->walkTo($point);$lion->hunt($zebra);$lion->sleep();

Page 14: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

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?

Page 15: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

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

Page 16: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

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?

Page 17: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

The Classical Model Is Easy To

Understand

Page 18: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 19: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

The Classical Model Is

Completely Impractical

Page 20: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

“Modern” View

Object == Collection Of (Related) Behaviors

Page 21: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

“Modern” View

Object == Collection Of (Related) Behaviors

Methods == Behavior

Page 22: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

“Modern” View

Object == Collection Of (Related) Behaviors

Methods == Behavior

Properties == Details Of Behavior

Page 23: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Classic View == “(conceptually) is a”

Modern View == “behaves as a”

Page 24: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

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);

}

Page 25: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Number

IntegerFloat Decimal

longshort long long

unsigned signed

Page 26: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

But Wait!

Page 27: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

We Don’t Even Need Inheritance

Page 28: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

All We Need IsPolymorphism

And Encapsulation

Page 29: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 30: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Polymorphism

Behavior Is Determined Dynamically

“Dynamic Dispatch”

Page 31: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Procedural Code

if ($a instanceof Long) { return new Long($a->getValue() + 1);} elseif ($a instanceof Float) { return new Float($a->getValue() + 1.0);} elseif ($a instanceof Decimal) { return new Decimal($a->getValue() + 1.0);

}

Page 32: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Polymorphic Code

return $int->add(new Integer(1));

Page 33: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Polymorphic Code

class Integer implements Number { public function add(Number $a) { return new Integer( $this->getValue() + (int) $a->getValue() ); }}

Page 34: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Polymorphic Code

class Float implements Number { public function add(Number $a) { return new Float( $this->getValue() + (float) $a->getValue() ); }}

Page 35: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 36: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Encapsulation

Behavior Is Completely Contained By The Object’s API

(Information Hiding)

Page 37: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Procedural Code

if (5 == $number->value) { print “Number Is 5”;} else { print “Number Is Not 5”;}

Page 38: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Encapsulated Code

if ($number->equals(new Integer(5))) { print “Number Is 5”;} else { print “Number Is Not 5”;}

Page 39: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Encapsulated Codeclass Decimal implements Number { protected $intValue; protected $exponent; public function equals(Number $a) { if ($a instanceof Decimal) { // Compare Directly } else { // Cast } }}

Page 40: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 41: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Behavior Is Defined By The

API

Page 42: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Two Types Of Primitive APIs

Interfaces (Explicit)

Page 43: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Two Types Of Primitive APIs

Interfaces (Explicit)

Duck Typing (Implicit)

Page 44: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

If an Object Is A Collection Of Behaviors...

Page 45: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

What Is A Collection Of

Classes/Objects?

Page 46: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

APIs

Method

Page 47: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

APIs

Method MethodMethod

Class

Page 48: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

APIs

Method MethodMethod

Class ClassClass

Package

Page 49: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

APIs

Method MethodMethod

Class ClassClass

Package PackagePackage

Library

Page 50: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

APIs

Method MethodMethod

Class ClassClass

Package PackagePackage

Library

Framework

LibraryLibrary

Page 51: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 52: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

What Makes A Good API?

Page 53: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

A Good APIDoes One Thing

Page 54: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

A Good APINever Changes

Page 55: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

A Good APIBehaves Like Its

Contract

Page 56: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

A Good APIHas A Narrow Responsibility

Page 57: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

A Good APIDepends Upon Abstractions

Page 58: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

And That’sSOLIDCode

Page 59: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Single Responsibility Principle O- L - I - D-

A Good API Does One

Thing

Page 60: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 61: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 62: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Single Responsibility Principle O- Open / Closed Principle

L - I - D-

A Good API Never Changes

Page 63: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 64: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 65: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 66: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 67: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 68: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Single Responsibility Principle O- Open / Closed Principle

L - Liskov Substitution Principle

I - D-

A Good API Behaves Like Its Contract

Page 69: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 70: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 71: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 72: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Single Responsibility Principle O- Open / Closed Principle

L - Liskov Substitution Principle

I - Interface Segregation Principle

D-

A Good API Has A Narrow Responsibility

Page 73: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 74: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

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

Page 75: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 76: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 77: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Single Responsibility Principle O- Open / Closed Principle

L - Liskov Substitution Principle

I - Interface Segregation Principle

D- Dependency Inversion Principle

Page 78: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Note That SOLID Does Not Dictate

What Is Good OOP

Page 79: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

SOLID Emerges From Good OOP

Page 80: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 81: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

So, What Makes A Bad API?

Page 82: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Global Variables(Spooky Action At A Distance)

Page 83: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Depending OnSpecifics Of An Implementation

Page 84: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Hidden Dependencies

Page 85: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Unhealthy Focus On Performance

Page 86: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Poorly Named APIs

Page 87: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Duplication

Page 88: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

And That’sSTUPID

Code

Page 89: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Singletons

T -U -P -I -D-

Global Variables(Spooky ActionAt A Distance)

Page 90: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 91: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Singletons

T - Tight Coupling

U -P -I -D-

Depending On Specifics Of An

Implementation

Page 92: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 93: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Singletons

T - Tight Coupling

U - Untestable Code

P -I -D-

Hidden Dependencies

Page 94: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 95: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I -D-

Unhealthy Focus On Performance

Page 96: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 97: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I - Indescriptive Naming

D-

Poorly Named APIs

Page 98: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 99: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I - Indescriptive Naming

D- Duplication

Duplication

Duplication

DuplicationDuplication

Duplication

DuplicationDuplication

Duplication

Page 100: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 101: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I - Indescriptive Naming

D- Duplication

Page 102: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

STUPID Embodies

Lessons Learned From Bad OOP

Page 103: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

What Good OOP Gives You

Modular CodeReusable CodeExtendable CodeEasy To Read CodeMaintainable CodeEasy To Change CodeEasy To Understand CodeClean Abstractions (mostly)

Page 104: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

What Good OOP Costs You

Tends To Have More “Layers”Tends To Be Slower At RuntimeTends To Have Larger CodebasesTends To Result In Over-AbstractionTends To Require More Effort To WriteTends To Require More Tacit Knowledge

Page 105: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Let’s Look At Some Code!

Page 106: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface Car {

public function turnLeft(); public function turnRight(); public function goFaster(); public function goSlower(); public function shiftUp(); public function shiftDown(); public function start();

}

Page 107: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface Steerable { public function steer($angle);}interface Acceleratable { public function accelerate($amt);}interface Shiftable { public function shiftDown(); public function shiftUp();}

Page 108: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Let’s Look At Drupal Code!

Page 109: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

Page 110: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Page 111: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Formatting Messages

Page 112: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Formatting Messages

Encoding Messages

Page 113: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Formatting Messages

Encoding Messages

Assembling Headers

Page 114: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Formatting Messages

Encoding Messages

Assembling Headers

Calling Sendmail

Page 115: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

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

Page 116: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Open For Extension?

Page 117: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Open For Extension?

Edits Require Copy/Paste

Page 118: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Open For Extension?

Liskov Substitution...

Page 119: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Open For Extension?

Liskov Substitution...

One Interface...

Many Responsibilites

Page 120: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MailSystemInterface {

public function format(array $message);

public function mail(array $message);

}

What Responsibility?

Open For Extension?

Liskov Substitution...

One Interface...

What Dependencies?

Page 121: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

interface MessageFormatter { public function format(Message $message);

}interface MessageEncoder { public function encode(Message $message);

}interface MessageTransport { public function send(Message $message);

}

Page 122: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

class MailSystem { public function __construct( MessageFormatter $messageFormatter, MessageEncoder $messageEncoder, MessageTransport $messageTransport

) {} public function mail(Message $message);

}

Page 123: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Principle Of

GoodEnough

Page 124: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 125: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 126: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 127: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 128: Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Page 129: Don't Be STUPID, Grasp SOLID - DrupalCon Prague

Anthony Ferrarabit.ly/prague-solid-talk

@[email protected]/ircmaxell