don't be stupid, grasp solid - north east php

104
Don’t Be STUPID Grasp SOLID! Anthony Ferrara NorthEastPHP 2013

Upload: anthony-ferrara

Post on 08-Sep-2014

23.678 views

Category:

Technology


0 download

DESCRIPTION

Object Oriented Design talk at North East PHP

TRANSCRIPT

Page 1: Don't be STUPID, Grasp SOLID - North East PHP

Don’t Be STUPIDGrasp SOLID!

Anthony FerraraNorthEastPHP 2013

Page 2: Don't be STUPID, Grasp SOLID - North East PHP

Let’s TalkObject

OrientedProgramming

Page 3: Don't be STUPID, Grasp SOLID - North East PHP

WhatIs An

Object?

Page 4: Don't be STUPID, Grasp SOLID - North East PHP

Classic View

Object == Physical “Thing”

Page 5: Don't be STUPID, Grasp SOLID - North East PHP

Classic View

Object == Physical “Thing”

Methods == Actions on “Thing”

Page 6: Don't be STUPID, Grasp SOLID - North East PHP

Classic View

Object == Physical “Thing”

Methods == Actions on “Thing”

Properties == Description of “Thing”

Page 7: Don't be STUPID, Grasp SOLID - North East PHP

Animal

MammalBird Fish

CatCow Dog

Lion Feline Cheetah

Page 8: Don't be STUPID, Grasp SOLID - North East PHP

Classic View

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

Page 9: Don't be STUPID, Grasp SOLID - North East PHP

Is This Realistic?

Page 10: Don't be STUPID, Grasp SOLID - North East PHP

Classic View

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

Page 11: Don't be STUPID, Grasp SOLID - North East PHP

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 12: Don't be STUPID, Grasp SOLID - North East PHP

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 13: Don't be STUPID, Grasp SOLID - North East PHP

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 14: Don't be STUPID, Grasp SOLID - North East PHP

The Classical Model Is Easy To

Understand

Page 15: Don't be STUPID, Grasp SOLID - North East PHP
Page 16: Don't be STUPID, Grasp SOLID - North East PHP

The Classical Model Is

Completely Impractical

Page 17: Don't be STUPID, Grasp SOLID - North East PHP

“Modern” View

Object == Collection Of (Related) Behaviors

Page 18: Don't be STUPID, Grasp SOLID - North East PHP

“Modern” View

Object == Collection Of (Related) Behaviors

Methods == Behavior

Page 19: Don't be STUPID, Grasp SOLID - North East PHP

“Modern” View

Object == Collection Of (Related) Behaviors

Methods == Behavior

Properties == Details Of Behavior

Page 20: Don't be STUPID, Grasp SOLID - North East PHP

Classic View == “(conceptually) is a”

Modern View == “behaves as a”

Page 21: Don't be STUPID, Grasp SOLID - North East PHP

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 22: Don't be STUPID, Grasp SOLID - North East PHP

Number

IntegerFloat Decimal

longshort long long

unsigned signed

Page 23: Don't be STUPID, Grasp SOLID - North East PHP

But Wait!

Page 24: Don't be STUPID, Grasp SOLID - North East PHP

We Don’t Even Need Inheritance

Page 25: Don't be STUPID, Grasp SOLID - North East PHP

All We Need IsPolymorphism

And Encapsulation

Page 26: Don't be STUPID, Grasp SOLID - North East PHP
Page 27: Don't be STUPID, Grasp SOLID - North East PHP

Polymorphism

Behavior Is Determined Dynamically

“Dynamic Dispatch”

Page 28: Don't be STUPID, Grasp SOLID - North East PHP

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 29: Don't be STUPID, Grasp SOLID - North East PHP

Polymorphic Code

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

Page 30: Don't be STUPID, Grasp SOLID - North East PHP

Polymorphic Code

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

Page 31: Don't be STUPID, Grasp SOLID - North East PHP

Polymorphic Code

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

Page 32: Don't be STUPID, Grasp SOLID - North East PHP

Encapsulation

Behavior Is Completely Contained By The Object’s API

(Information Hiding)

Page 33: Don't be STUPID, Grasp SOLID - North East PHP

Procedural Code

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

Page 34: Don't be STUPID, Grasp SOLID - North East PHP

Encapsulated Code

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

Page 35: Don't be STUPID, Grasp SOLID - North East PHP

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

Page 36: Don't be STUPID, Grasp SOLID - North East PHP

Behavior Is Defined By The

API

Page 37: Don't be STUPID, Grasp SOLID - North East PHP

Two Types Of Primitive APIs

Interfaces (Explicit)

Page 38: Don't be STUPID, Grasp SOLID - North East PHP

Two Types Of Primitive APIs

Interfaces (Explicit)

Duck Typing (Implicit)

Page 39: Don't be STUPID, Grasp SOLID - North East PHP

If an Object Is A Collection Of Behaviors...

Page 40: Don't be STUPID, Grasp SOLID - North East PHP

What Is A Collection Of

Classes/Objects?

Page 41: Don't be STUPID, Grasp SOLID - North East PHP

APIs

Method

Page 42: Don't be STUPID, Grasp SOLID - North East PHP

APIs

Method MethodMethod

Class

Page 43: Don't be STUPID, Grasp SOLID - North East PHP

APIs

Method MethodMethod

Class ClassClass

Package

Page 44: Don't be STUPID, Grasp SOLID - North East PHP

APIs

Method MethodMethod

Class ClassClass

Package PackagePackage

Library

Page 45: Don't be STUPID, Grasp SOLID - North East PHP

APIs

Method MethodMethod

Class ClassClass

Package PackagePackage

Library

Framework

LibraryLibrary

Page 46: Don't be STUPID, Grasp SOLID - North East PHP
Page 47: Don't be STUPID, Grasp SOLID - North East PHP

What Makes A Good API?

Page 48: Don't be STUPID, Grasp SOLID - North East PHP

A Good APIDoes One Thing

Page 49: Don't be STUPID, Grasp SOLID - North East PHP

A Good APINever Changes

Page 50: Don't be STUPID, Grasp SOLID - North East PHP

A Good APIBehaves Like Its

Contract

Page 51: Don't be STUPID, Grasp SOLID - North East PHP

A Good APIHas A Narrow Responsibility

Page 52: Don't be STUPID, Grasp SOLID - North East PHP

A Good APIDepends Upon Abstractions

Page 53: Don't be STUPID, Grasp SOLID - North East PHP

And That’sSOLIDCode

Page 54: Don't be STUPID, Grasp SOLID - North East PHP

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

A Good API Does One

Thing

Page 55: Don't be STUPID, Grasp SOLID - North East PHP
Page 56: Don't be STUPID, Grasp SOLID - North East PHP

S - Single Responsibility Principle O- Open / Closed Principle

L - I - D-

A Good API Never Changes

Page 57: Don't be STUPID, Grasp SOLID - North East PHP
Page 58: Don't be STUPID, Grasp SOLID - North East PHP

S - Single Responsibility Principle O- Open / Closed Principle

L - Liskov Substitution Principle

I - D-

A Good API Behaves Like Its Contract

Page 59: Don't be STUPID, Grasp SOLID - North East PHP
Page 60: Don't be STUPID, Grasp SOLID - North East PHP
Page 61: Don't be STUPID, Grasp SOLID - North East PHP
Page 62: Don't be STUPID, Grasp SOLID - North East PHP

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 63: Don't be STUPID, Grasp SOLID - North East PHP
Page 64: Don't be STUPID, Grasp SOLID - North East PHP

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 65: Don't be STUPID, Grasp SOLID - North East PHP
Page 66: Don't be STUPID, Grasp SOLID - North East PHP

S - Single Responsibility Principle O- Open / Closed Principle

L - Liskov Substitution Principle

I - Interface Segregation Principle

D- Dependency Inversion Principle

Page 67: Don't be STUPID, Grasp SOLID - North East PHP

Note That SOLID Does Not Dictate

What Is Good OOP

Page 68: Don't be STUPID, Grasp SOLID - North East PHP

SOLID Emerges From Good OOP

Page 69: Don't be STUPID, Grasp SOLID - North East PHP
Page 70: Don't be STUPID, Grasp SOLID - North East PHP

So, What Makes A Bad API?

Page 71: Don't be STUPID, Grasp SOLID - North East PHP

Global Variables(Spooky Action At A Distance)

Page 72: Don't be STUPID, Grasp SOLID - North East PHP

Depending OnSpecifics Of An Implementation

Page 73: Don't be STUPID, Grasp SOLID - North East PHP

Hidden Dependencies

Page 74: Don't be STUPID, Grasp SOLID - North East PHP

Unhealthy Focus On Performance

Page 75: Don't be STUPID, Grasp SOLID - North East PHP

Poorly Named APIs

Page 76: Don't be STUPID, Grasp SOLID - North East PHP

Duplication

Page 77: Don't be STUPID, Grasp SOLID - North East PHP

And That’sSTUPID

Code

Page 78: Don't be STUPID, Grasp SOLID - North East PHP

S - Singletons

T -U -P -I -D-

Global Variables(Spooky ActionAt A Distance)

Page 79: Don't be STUPID, Grasp SOLID - North East PHP
Page 80: Don't be STUPID, Grasp SOLID - North East PHP

S - Singletons

T - Tight Coupling

U -P -I -D-

Depending On Specifics Of An

Implementation

Page 81: Don't be STUPID, Grasp SOLID - North East PHP
Page 82: Don't be STUPID, Grasp SOLID - North East PHP

S - Singletons

T - Tight Coupling

U - Untestable Code

P -I -D-

Hidden Dependencies

Page 83: Don't be STUPID, Grasp SOLID - North East PHP
Page 84: Don't be STUPID, Grasp SOLID - North East PHP

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I -D-

Unhealthy Focus On Performance

Page 85: Don't be STUPID, Grasp SOLID - North East PHP
Page 86: Don't be STUPID, Grasp SOLID - North East PHP

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I - Indescriptive Naming

D-

Poorly Named APIs

Page 87: Don't be STUPID, Grasp SOLID - North East PHP
Page 88: Don't be STUPID, Grasp SOLID - North East PHP

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I - Indescriptive Naming

D- Duplication

Duplication

Duplication

DuplicationDuplication

Duplication

DuplicationDuplication

Duplication

Page 89: Don't be STUPID, Grasp SOLID - North East PHP
Page 90: Don't be STUPID, Grasp SOLID - North East PHP

S - Singletons

T - Tight Coupling

U - Untestable Code

P - Premature Optimization

I - Indescriptive Naming

D- Duplication

Page 91: Don't be STUPID, Grasp SOLID - North East PHP

STUPID Embodies

Lessons Learned From Bad OOP

Page 92: Don't be STUPID, Grasp SOLID - North East PHP

What Good OOP Gives You

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

Page 93: Don't be STUPID, Grasp SOLID - North East PHP

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 94: Don't be STUPID, Grasp SOLID - North East PHP

Let’s Look At Some Code!

Page 95: Don't be STUPID, Grasp SOLID - North East PHP

interface Car {

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

}

Page 96: Don't be STUPID, Grasp SOLID - North East PHP

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

Page 97: Don't be STUPID, Grasp SOLID - North East PHP

interface Transmission { public function getNumberOfGears(); public function getGear(); public function shiftToNeutral(); public function shiftToReverse(); public function shiftToGear($gear);}interface Manual extends Transmission { public function engageClutch(); public function releaseClutch();}interface Automatic extends Transmission { public function shiftToDrive();}

Page 98: Don't be STUPID, Grasp SOLID - North East PHP

Principle Of

GoodEnough

Page 99: Don't be STUPID, Grasp SOLID - North East PHP
Page 100: Don't be STUPID, Grasp SOLID - North East PHP
Page 101: Don't be STUPID, Grasp SOLID - North East PHP
Page 102: Don't be STUPID, Grasp SOLID - North East PHP
Page 103: Don't be STUPID, Grasp SOLID - North East PHP
Page 104: Don't be STUPID, Grasp SOLID - North East PHP

Anthony Ferrarajoind.in/8907

@[email protected]/ircmaxell