NDC 2011 - The FLUID Principles


Published on

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

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

NDC 2011 - The FLUID Principles

  1. 1. ndc 2011
  2. 2. TheFLUID Principles Kevlin Henney Anders Norås
  3. 3. FLUIDcontrasts with SOLID
  4. 4. SOLID
  5. 5. S ingle Responsibility PrincipleO pen / Closed PrincipleL iskov’s Substitution PrincipleI nterface Segregation PrincipleD ependency Inversion Principle
  6. 6. “ALL THE NEWS THAT’S FIT TO DEPLOY” LATE EDITION The Software Dev Times VOL XI...NO 12,345 OSLO, WEDNESDAY, JUNE 8, 2011 FREE AS IN BEER SHOCK-SOLID! MYSTERIOUS SOFTWARE CRAFTSMAN COINED THE SOLID ACRONYM! Bob might not be your uncle By A. NORÅS & K. HENNEYUt enim ad minim veniam, quis nostrud exerc.Irure dolor in reprehend incididunt ut labore etdolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamcolaboris nisi ut aliquip ex ea commodoconsequat. Duis aute irure dolor inreprehenderit in voluptate velit esse molestaie Etiam sit amet estcillum. Tia non ob ea soluad incommod quaeegen ium improb fugiend. Officia deseruntmollit anim id est laborum Et harumd dereud.Neque pecun modut nequeConsectetuer arcu ipsum ornare pellentesquevehicula, in vehicula diam, ornare magna eratfelis wisi a risus. Justo fermentum id. Malesuadaeleifend, tortor molestie, a fusce a vel et. Mauris NO COMMENT. The software craftsman claimed to have discovered that a set ofat suspendisse, neque aliquam faucibus principles could be abbreviated “SOLID”, declined to comment on the matter.adipiscing, vivamus in. Wisi mattis leo suscipitnec amet, nisl fermentum tempor ac a, augue in Sociosqu netus semper aenean Ut enim ad minim veniam, quis nostrudeleifend in venenatis, cras sit id in vestibulum suspendisse dictum, arcu enim conubia exerc. Irure dolor in reprehendfelis. Molestie ornare amet vel id fusce, rem leo nulla ac nibh, purus hendrerit ut incididunt ut labore et dolore magnavolutpat platea. Magnis vel, lacinia nisl, vel mattis nec maecenas, quo ac, vivamus aliqua. Ut enim ad minim veniam, quisnostra nunc eleifend arcu leo, in dignissim praesent metus eget viverra ante. nostrud exercitation ullamco laboris nisilorem vivamus laoreet. Natoque placerat sed sit hendrerit, ut aliquip ex ea commodo consequat. dapibus eleifend velit molestiae leo a, ut Duis aute irure dolor in reprehenderit inDonec arcu risus diam amet sit. Congue tortor lorem sit et lacus aliquam. Sodales nulla voluptate velit esse molestaie cillum. Tiacursus risus vestibulum commodo nisl, luctus erat et luctus faucibus aperiam sapien. non ob ea soluad incommod quae egenaugue amet quis aenean odio etiammaecenas sit, Leo inceptos augue nec pulvinar rutrum ium improb fugiend. Officia deseruntdonec velit iusto, morbi felis elit et nibh. aliquam mauris, wisi hasellus fames ac, mollit anim id est laborum Et harumdVestibulum volutpat dui lacus consectetuer ut, commodo eligendi dictumst, dapibus dereud.mauris at etiam suspendisse, eu wisi rhoncus morbi auctor.eget nibh velit, eget posuere sem in a sit.
  7. 7. e BristolDictionary of Concise English
  8. 8. prin·ci·ple /ˈprinsəpəl/ Noun 4. a natural law forming1. a fundamental truth the basis for theor prop osit ion t hat construction or workingserves as the foundation of a machine.for a system of belief orbehaviour or for a chainof reasoning. para·skevi·de·katri·a·ph o·bia /ˈpærəskevidekaˈtriəˈfōbēə/ Adjective, Noun2. morally correctbehaviour and attitudes. 1. fear of Friday the 13th.3. a general scienti c Etymology: e wordtheorem or law that hasnumerous special was devised by Dr.applications across a Donald Dossey who toldwide eld. his patients that "when you learn to pronounce it, youre cured.
  9. 9. TheFLUID Principles Kevlin Henney Anders Norås
  10. 10. TheFLUID Guidelines Kevlin Henney Anders Norås
  11. 11. What are theF L U I D Guidelines?
  12. 12. FLUID
  13. 13. FLUID
  14. 14. Functional
  15. 15. Javapublic class HeatingSystem { public void turnOn() ... public void turnOff() ... ...}public class Timer { public Timer(TimeOfDay toExpire, Runnable toDo) ... public void run() ... public void cancel() ... ...}
  16. 16. Javapublic 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(); }}
  17. 17. JavaTimer turningOn = new Timer(timeOn, new TurnOn(heatingSystem));Timer turningOff = new Timer(timeOff, new TurnOff(heatingSystem));
  18. 18. JavaTimer turningOn = new Timer( timeToTurnOn, new Runnable() { public void run() { heatingSystem.turnOn(); } });Timer turningOff = new Timer( timeToTurnOff, new Runnable() { public void run() { heatingSystem.turnOff(); } });
  19. 19. C++void turnOn(void * toTurnOn){ static_cast<HeatingSystem *>(toTurnOn)->turnOn();}void turnOff(void * toTurnOff){ static_cast<HeatingSystem *>(toTurnOff)->turnOff();}
  20. 20. C++Timer turningOn(timeOn, &heatingSystem, turnOn);Timer turningOff(timeOff, &heatingSystem, turnOff);
  21. 21. C++class Timer{ Timer(TimeOfDay toExpire, function<void()> toDo); void run(); void cancel(); ...};
  22. 22. C++Timer turningOn( timeOn, bind( &HeatingSystem::turnOn, &heatingSystem));Timer turningOff( timeOff, bind( &HeatingSystem::turnOff, &heatingSystem));
  23. 23. C#public class Timer{ public Timer( TimeOfDay toExpire, Action toDo) ... public void Run() ... public void Cancel() ... ...}
  24. 24. C#Timer turningOn = new Timer( timeOn, () => heatingSystem.TurnOn() );Timer turningOff = new Timer( timeOff, () => heatingSystem.TurnOff() );
  25. 25. C#Timer turningOn = new Timer( timeOn, heatingSystem.TurnOn );Timer turningOff = new Timer( timeOff, heatingSystem.TurnOff );
  26. 26. F unctionalLUID
  27. 27. F unctionalLUID
  28. 28. Loose
  29. 29. “ 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 Im not aware of them. •Allan Kay
  30. 30. C#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 everybodys world!n");}
  31. 31. Groovyclass HeatingSystem { def turnOn() { ... } def turnOff() { ... }}def heater = new HeatingSystem()def timer = new Timer()def action = "turnOn"timer.runAfter(1000) { heater."$action"()}
  32. 32. PHPclass DiyStore{ private $_bucket; public function getPaintBucket() { if ($this->_bucket === null) { $this->_bucket = $this->fillPaintBucket(); } return $this->_bucket; } private function fillPaintBucket() { // ... }}
  33. 33. RubyNullObject.new().say().hello().to().any().method_call().you().like()
  34. 34. TheFLUID Principles Kevlin Henney Anders Norås
  35. 35. TheFLUID Suggestions Kevlin Henney Anders Norås
  36. 36. F unctionalL ooseUID
  37. 37. F unctionalL ooseUID
  38. 38. Unit Testable
  39. 39. PowerShellfunction 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]}
  40. 40. PowerShell[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")
  41. 41. PowerShell$inputsWithExpectations | ?{ [String] $actual = GetNextFriday13th($_[0]) [String] $expected = $_[1] $actual -ne $expected}
  42. 42. F unctionalL ooseU nit TestableID
  43. 43. F unctionalL ooseU nit TestableID
  44. 44. Introspective
  45. 45. Scheme(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))))
  46. 46. JavaScriptfunction 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); };};
  47. 47. Visual BasicPublic 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
  48. 48. F unctionalL ooseU nit TestableI ntrospectiveD
  49. 49. F unctionalL ooseU nit TestableI ntrospectiveD
  50. 50. ‘Dempotent
  51. 51. “ Asking a question should not change the answer. •Betrand Meyer
  52. 52. “ Asking a question should not change the answer. •Betrand Meyer
  53. 53. Scala(1 to 10).foldLeft(0)(_ + _)
  54. 54. F unctionalL ooseU nit TestableI ntrospective‘D dempotent
  55. 55. 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 — objectsare merely a poor mans closures." Chastised, Anton took his leave from his master and returned to his cell,intent on studying closures. He carefully read the entire "Lambda: eUltimate..." series of papers and its cousins, and implemented a smallScheme 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 bysaying "Master, I have diligently studied the matter, and now understandthat objects are truly a poor mans closures."Qc Na responded by hitting Anton with his stick, saying "When will youlearn? Closures are a poor mans object." At that moment, Anton becameenlightened. http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html
  56. 56. @kevlinhenney @anoras