ndc 2011 - the fluid principles

89
ndc 2011

Upload: anoras

Post on 05-Dec-2014

4.643 views

Category:

Technology


8 download

DESCRIPTION

The slides from Kevlin Henney and Anders Norås introduction of the FLUID principles at The Norwegian Developer's Conference June 8th 2011.

TRANSCRIPT

Page 1: NDC 2011 - The FLUID Principles

ndc 2011

Page 2: NDC 2011 - The FLUID Principles
Page 3: NDC 2011 - The FLUID Principles

FLUIDThe

PrinciplesKevlin HenneyAnders Norås

Page 4: NDC 2011 - The FLUID Principles

FLUID

SOLIDcontrasts with

Page 5: NDC 2011 - The FLUID Principles
Page 6: NDC 2011 - The FLUID Principles

SOLID

Page 7: NDC 2011 - The FLUID Principles

SOL

ingle Responsibility Principle

pen / Closed Principle

iskov’s Substitution Principle

nterface Segregation Principle

ependency Inversion Principle

ID

Page 8: NDC 2011 - The FLUID Principles

Bob might not be your uncle

By A. NORÅS &K. HENNEY

Ut enim ad minim veniam, quis nostrud exerc. Irure dolor in reprehend incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse molestaie cillum. Tia non ob ea soluad incommod quae egen ium improb fugiend. Officia deserunt mollit anim id est laborum Et harumd dereud.

Neque pecun modut nequeConsectetuer arcu ipsum ornare pellentesque vehicula, in vehicula diam, ornare magna erat felis wisi a risus. Justo fermentum id. Malesuada eleifend, tortor molestie, a fusce a vel et. Mauris at suspendisse, neque aliquam faucibus adipiscing, vivamus in. Wisi mattis leo suscipit nec amet, nisl fermentum tempor ac a, augue in eleifend in venenatis, cras sit id in vestibulum felis. Molestie ornare amet vel id fusce, rem volutpat platea. Magnis vel, lacinia nisl, vel nostra nunc eleifend arcu leo, in dignissim lorem vivamus laoreet.

Donec arcu risus diam amet sit. Congue tortor cursus risus vestibulum commodo nisl, luctus augue amet quis aenean odio etiammaecenas sit, donec velit iusto, morbi felis elit et nibh. Vestibulum volutpat dui lacus consectetuer ut, mauris at etiam suspendisse, eu wisi rhoncus eget nibh velit, eget posuere sem in a sit.

Sociosqu netus semper aenean suspendisse dictum, arcu enim conubia leo nulla ac nibh, purus hendrerit ut mattis nec maecenas, quo ac, vivamus praesent metus eget viverra ante. Natoque placerat sed sit hendrerit, dapibus eleifend velit molestiae leo a, ut lorem sit et lacus aliquam. Sodales nulla erat et luctus faucibus aperiam sapien. Leo inceptos augue nec pulvinar rutrum aliquam mauris, wisi hasellus fames ac, commodo eligendi dictumst, dapibus morbi auctor.

Ut enim ad minim veniam, quis nostrud exerc. Irure dolor in reprehend incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse molestaie cillum. Tia non ob ea soluad incommod quae egen ium improb fugiend. Officia deserunt mollit anim id est laborum Et harumd dereud.

Etiam sit amet est

The Software Dev Times“ALL THE NEWS THAT’S FIT TO DEPLOY” LATE EDITION

VOL XI...NO 12,345 OSLO, WEDNESDAY, JUNE 8, 2011 FREE AS IN BEER

SHOCK-SOLID!MYSTERIOUS SOFTWARE CRAFTSMAN

COINED THE SOLID ACRONYM!

NO COMMENT. The software craftsman claimed to have discovered that a set of principles could be abbreviated “SOLID”, declined to comment on the matter.

Bob might not be your uncle

By A. NORÅS &K. HENNEY

Ut enim ad minim veniam, quis nostrud exerc. Irure dolor in reprehend incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse molestaie cillum. Tia non ob ea soluad incommod quae egen ium improb fugiend. Officia deserunt mollit anim id est laborum Et harumd dereud.

Neque pecun modut nequeConsectetuer arcu ipsum ornare pellentesque vehicula, in vehicula diam, ornare magna erat felis wisi a risus. Justo fermentum id. Malesuada eleifend, tortor molestie, a fusce a vel et. Mauris at suspendisse, neque aliquam faucibus adipiscing, vivamus in. Wisi mattis leo suscipit nec amet, nisl fermentum tempor ac a, augue in eleifend in venenatis, cras sit id in vestibulum felis. Molestie ornare amet vel id fusce, rem volutpat platea. Magnis vel, lacinia nisl, vel nostra nunc eleifend arcu leo, in dignissim lorem vivamus laoreet.

Donec arcu risus diam amet sit. Congue tortor cursus risus vestibulum commodo nisl, luctus augue amet quis aenean odio etiammaecenas sit, donec velit iusto, morbi felis elit et nibh. Vestibulum volutpat dui lacus consectetuer ut, mauris at etiam suspendisse, eu wisi rhoncus eget nibh velit, eget posuere sem in a sit.

Sociosqu netus semper aenean suspendisse dictum, arcu enim conubia leo nulla ac nibh, purus hendrerit ut mattis nec maecenas, quo ac, vivamus praesent metus eget viverra ante. Natoque placerat sed sit hendrerit, dapibus eleifend velit molestiae leo a, ut lorem sit et lacus aliquam. Sodales nulla erat et luctus faucibus aperiam sapien. Leo inceptos augue nec pulvinar rutrum aliquam mauris, wisi hasellus fames ac, commodo eligendi dictumst, dapibus morbi auctor.

Ut enim ad minim veniam, quis nostrud exerc. Irure dolor in reprehend incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse molestaie cillum. Tia non ob ea soluad incommod quae egen ium improb fugiend. Officia deserunt mollit anim id est laborum Et harumd dereud.

Etiam sit amet est

The Software Dev Times“ALL THE NEWS THAT’S FIT TO DEPLOY” LATE EDITION

VOL XI...NO 12,345 OSLO, WEDNESDAY, JUNE 8, 2011 FREE AS IN BEER

SHOCK-SOLID!MYSTERIOUS SOFTWARE CRAFTSMAN

COINED THE SOLID ACRONYM!

NO COMMENT. The software craftsman claimed to have discovered that a set of principles could be abbreviated “SOLID”, declined to comment on the matter.

Page 9: NDC 2011 - The FLUID Principles
Page 10: NDC 2011 - The FLUID Principles
Page 11: NDC 2011 - The FLUID Principles

eBristol

Dictionaryof

ConciseEnglish

Page 12: NDC 2011 - The FLUID Principles

prin·ci·ple /ˈprinsəpəl/ Noun

1. a fundamental truth or proposit ion that serves as the foundation for a system of belief or behaviour or for a chain of reasoning.2. morally correct behaviour and attitudes.3. a general scienti"c theorem or law that has n u m e r o u s s p e c i a l applications across a wide "eld.

4. a natural law forming t h e b a s i s f o r t h e construction or working of a machine.

para·skevi·de·katri·a·pho·bia /ˈpærəskevidekaˈtriəˈfōbēə/ Adjective, Noun

1. fear of Friday the 13th. Etymology: e word was devised by Dr. Donald Dossey who told his patients that "when you learn to pronounce it, you're cured.

Page 13: NDC 2011 - The FLUID Principles
Page 14: NDC 2011 - The FLUID Principles

FLUIDThe

PrinciplesKevlin HenneyAnders Norås

Page 15: NDC 2011 - The FLUID Principles

FLUIDThe

Kevlin HenneyAnders Norås

Guidelines

Page 16: NDC 2011 - The FLUID Principles
Page 17: NDC 2011 - The FLUID Principles

F L U I DWhat are the

Guidelines?

Page 18: NDC 2011 - The FLUID Principles

F

L

U

I

D

Page 19: NDC 2011 - The FLUID Principles

F

L

U

I

D

Page 20: NDC 2011 - The FLUID Principles
Page 21: NDC 2011 - The FLUID Principles
Page 22: NDC 2011 - The FLUID Principles

Functional

Page 23: NDC 2011 - The FLUID Principles

public class HeatingSystem { public void turnOn() ... public void turnOff() ... ...}

public class Timer { public Timer(TimeOfDay toExpire, Runnable toDo) ... public void run() ... public void cancel() ... ...}

Java

Page 24: NDC 2011 - The FLUID Principles

public class TurnOn implements Runnable { private HeatingSystem toTurnOn; public TurnOn(HeatingSystem toRun) { toTurnOn = toRun; } public void run() { toTurnOn.turnOn(); }}public class TurnOff implements Runnable { private HeatingSystem toTurnOff; public TurnOff(HeatingSystem toRun) { toTurnOff = toRun; } public void run() { toTurnOff.turnOff(); }}

Java

Page 25: NDC 2011 - The FLUID Principles

Timer turningOn = new Timer(timeOn, new TurnOn(heatingSystem));Timer turningOff = new Timer(timeOff, new TurnOff(heatingSystem));

Java

Page 26: NDC 2011 - The FLUID Principles

Timer turningOn = new Timer( timeToTurnOn, new Runnable() { public void run() { heatingSystem.turnOn(); } });Timer turningOff = new Timer( timeToTurnOff, new Runnable() { public void run() { heatingSystem.turnOff(); } });

Java

Page 27: NDC 2011 - The FLUID Principles

void turnOn(void * toTurnOn){ static_cast<HeatingSystem *>(toTurnOn)->turnOn();}void turnOff(void * toTurnOff){ static_cast<HeatingSystem *>(toTurnOff)->turnOff();}

C++

Page 28: NDC 2011 - The FLUID Principles

Timer turningOn(timeOn, &heatingSystem, turnOn);Timer turningOff(timeOff, &heatingSystem, turnOff);

C++

Page 29: NDC 2011 - The FLUID Principles

class Timer{ Timer(TimeOfDay toExpire, function<void()> toDo); void run(); void cancel(); ...};

C++

Page 30: NDC 2011 - The FLUID Principles

Timer turningOn( timeOn, bind( &HeatingSystem::turnOn, &heatingSystem));Timer turningOff( timeOff, bind( &HeatingSystem::turnOff, &heatingSystem));

C++

Page 31: NDC 2011 - The FLUID Principles

public class Timer{ public Timer( TimeOfDay toExpire, Action toDo) ... public void Run() ... public void Cancel() ... ...}

C#

Page 32: NDC 2011 - The FLUID Principles

Timer turningOn = new Timer( timeOn, () => heatingSystem.TurnOn() );Timer turningOff = new Timer( timeOff, () => heatingSystem.TurnOff() );

C#

Page 33: NDC 2011 - The FLUID Principles

Timer turningOn = new Timer( timeOn, heatingSystem.TurnOn );Timer turningOff = new Timer( timeOff, heatingSystem.TurnOff );

C#

Page 34: NDC 2011 - The FLUID Principles
Page 35: NDC 2011 - The FLUID Principles
Page 36: NDC 2011 - The FLUID Principles
Page 37: NDC 2011 - The FLUID Principles

F

L

U

I

D

unctional

Page 38: NDC 2011 - The FLUID Principles

F

L

U

I

D

unctional

Page 39: NDC 2011 - The FLUID Principles
Page 40: NDC 2011 - The FLUID Principles
Page 41: NDC 2011 - The FLUID Principles

Loose

Page 42: NDC 2011 - The FLUID Principles
Page 43: NDC 2011 - The FLUID Principles

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them.

•Allan Kay

Page 44: NDC 2011 - The FLUID Principles

#include <windows.h> #include <stdio.h> typedef int (__cdecl *MYPROC)(LPWSTR); VOID main(VOID) { HINSTANCE hinstLib; MYPROC ProcAdd; BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; hinstLib = LoadLibrary(TEXT("echo.dll")); if (hinstLib != NULL) { ProcAdd = (MYPROC) GetProcAddress(hinstLib, "echo"); if (NULL != ProcAdd) { fRunTimeLinkSuccess = TRUE; (ProcAdd) (L"Hello my world!\n"); } fFreeResult = FreeLibrary(hinstLib); } if (!fRunTimeLinkSuccess) printf("Hello everybody's world!\n"); }

C

Page 45: NDC 2011 - The FLUID Principles

class HeatingSystem { def turnOn() { ... } def turnOff() { ... }}def heater = new HeatingSystem()def timer = new Timer()def action = "turnOn"timer.runAfter(1000) { heater."$action"()}

Groovy

Page 46: NDC 2011 - The FLUID Principles

class DiyStore{ private $_bucket; public function getPaintBucket() { if ($this->_bucket === null) { $this->_bucket = $this->fillPaintBucket(); } return $this->_bucket; } private function fillPaintBucket() { // ... }}

PHP

Page 47: NDC 2011 - The FLUID Principles
Page 48: NDC 2011 - The FLUID Principles

NullObject.new().say().hello().to().any().method_call().you().like()

Ruby

Page 49: NDC 2011 - The FLUID Principles

FLUIDThe

PrinciplesKevlin HenneyAnders Norås

Page 50: NDC 2011 - The FLUID Principles

FLUIDThe

Kevlin HenneyAnders Norås

Suggestions

Page 51: NDC 2011 - The FLUID Principles
Page 52: NDC 2011 - The FLUID Principles

F

U

L

I

D

unctional

oose

Page 53: NDC 2011 - The FLUID Principles

U

F

L

I

D

unctional

oose

Page 54: NDC 2011 - The FLUID Principles
Page 55: NDC 2011 - The FLUID Principles

Unit Testable

Page 56: NDC 2011 - The FLUID Principles
Page 57: NDC 2011 - The FLUID Principles

function GetNextFriday13th($from) { [DateTime[]] $friday13ths = &{ foreach($i in 1..500) { $from = $from.AddDays(1) $from } } | ?{ $_.DayOfWeek -eq [DayOfWeek]::Friday -and $_.Day -eq 13 } return $friday13ths[0]}

PowerShell

Page 58: NDC 2011 - The FLUID Principles

[DateTime[][]] $inputsWithExpectations = ("2011-01-01", "2011-05-13"), ("2011-05-13", "2012-01-13"), ("2007-04-01", "2007-04-13"), ("2007-04-12", "2007-04-13"), ("2007-04-13", "2007-07-13"), ("2012-01-01", "2012-01-13"), ("2012-01-13", "2012-04-13"), ("2012-04-13", "2012-07-13"), ("2001-07-13", "2002-09-13")

PowerShell

Page 59: NDC 2011 - The FLUID Principles

$inputsWithExpectations | ?{ [String] $actual = GetNextFriday13th($_[0]) [String] $expected = $_[1] $actual -ne $expected}

PowerShell

Page 60: NDC 2011 - The FLUID Principles
Page 61: NDC 2011 - The FLUID Principles

F

L

U

I

D

unctional

oose

nit Testable

Page 62: NDC 2011 - The FLUID Principles

F

L

U

I

D

unctional

oose

nit Testable

Page 63: NDC 2011 - The FLUID Principles
Page 64: NDC 2011 - The FLUID Principles
Page 65: NDC 2011 - The FLUID Principles

Introspective

Page 66: NDC 2011 - The FLUID Principles
Page 67: NDC 2011 - The FLUID Principles

(define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp)) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type -EVAL" exp))))

Scheme

Page 68: NDC 2011 - The FLUID Principles

function Shoebox() { var things = ["Nike 42", "Adidas 41", "Adidas 43", "Paul Smith 41"]; def(this, "find", function(brand) { var result = []; for (var i=0; i<things.length; i++) { if (things[i].indexOf(brand) !== -1) result.push(things[i]); } return result; }); def(this, "find", function(brand,size) { var result = []; for (var i=0; i<things.length; i++) { if (things[i].indexOf(brand) !== -1 || parseInt(things[i].match(/\d+/),10) === size) result.push(things[i]); } return result; });}

function def(obj, name, fn) { var implFn = obj[name]; obj[name]=function() { if (fn.length === arguments.length) return fn.apply(this,arguments); else if (typeof implFn === "function") return implFn.apply(this,arguments); };};

JavaScript

Page 69: NDC 2011 - The FLUID Principles

Public Class Book <Key> _ Public Property ISBN() As String ' ... End Property

<StringLength(256)> _ Public Property Title() As String ' ... End Property

Public Property AuthorSSN() As String ' ... End Property

<RelatedTo(RelatedProperty := Books, Key := AuthorSSN, RelatedKey := SSN)> _ Public Property Author() As Person ' ... End PropertyEnd Class

Visual Basic

Page 70: NDC 2011 - The FLUID Principles
Page 71: NDC 2011 - The FLUID Principles

F

L

U

I

D

unctional

oose

nit Testable

ntrospective

Page 72: NDC 2011 - The FLUID Principles

F

L

U

I

D

unctional

oose

nit Testable

ntrospective

Page 73: NDC 2011 - The FLUID Principles
Page 74: NDC 2011 - The FLUID Principles
Page 75: NDC 2011 - The FLUID Principles

‘Dempotent

Page 76: NDC 2011 - The FLUID Principles
Page 77: NDC 2011 - The FLUID Principles
Page 78: NDC 2011 - The FLUID Principles

Asking a question s h o u l d n o t c h a n g e t h e answer.

“•Betrand Meyer

Page 79: NDC 2011 - The FLUID Principles

Asking a question s h o u l d n o t c h a n g e t h e answer.

“•Betrand Meyer

, and nor should asking it twice!

Retweeted by @kevlinhenney

Page 80: NDC 2011 - The FLUID Principles

(1 to 10).foldLeft(0)(_ + _)Scala

Page 81: NDC 2011 - The FLUID Principles
Page 82: NDC 2011 - The FLUID Principles

F

L

U

I

D

unctional

oose

nit Testable

ntrospective

dempotent‘

Page 83: NDC 2011 - The FLUID Principles
Page 84: NDC 2011 - The FLUID Principles
Page 85: NDC 2011 - The FLUID Principles
Page 86: NDC 2011 - The FLUID Principles
Page 87: NDC 2011 - The FLUID Principles
Page 88: NDC 2011 - The FLUID Principles

e venerable master Qc Na was walking with his student, Anton.Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing — is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil — objects are merely a poor man's closures." Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: e Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress. On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened.

http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html

Page 89: NDC 2011 - The FLUID Principles

@anoras@kevlinhenney