$30 off During Our Annual Pro Sale. View Details »

Treating Objects Like People (Baruco, Sept 2013)

mattwynne
September 14, 2013

Treating Objects Like People (Baruco, Sept 2013)

Cucumber core developer Matt Wynne uses the example of re-developing Cucumber's internals to explain important lessons about object-oriented design.

mattwynne

September 14, 2013
Tweet

More Decks by mattwynne

Other Decks in Technology

Transcript

  1. TREATING
    OBJECTS
    LIKE PEOPLE
    @mattwynne | @baruco | Setiembre 2013

    View Slide

  2. (c) David Shrigley

    View Slide

  3. View Slide

  4. 2008

    View Slide

  5. View Slide

  6. 2012

    View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. Feature: Update card details
    Need to be able to update my card, e.g. if I decide
    to use a different one for billing.
    Scenario: Update card details successfully
    Given I have started a paid subscription on the pro plan
    When I choose to update which card is billed
    Then I should see that my card details have been saved
    Scenario: Use a bad card
    Given I have started a paid subscription on the pro plan
    When I try to change to an invalid card
    Then I should see that my card has been declined

    View Slide

  14. Feature Scenario Step
    * *
    Formatter

    View Slide

  15. Result

    View Slide

  16. Feature Scenario
    Formatter Step
    accept(self)
    before_feature
    accept(formatter)
    before_scenario
    accept(formatter)
    before_step
    after_step
    after_scenario
    after_feature
    execute(step)

    View Slide

  17. Feature: Update card details
    Need to be able to update my card, e.g. if I decide
    to use a different one for billing.
    Scenario: Update card details successfully
    Given I have started a paid subscription on the pro plan
    When I choose to update which card is billed
    Then I should see that my card details have been saved
    Scenario: Use a bad card
    Given I have started a paid subscription on the pro plan
    When I try to change to an invalid card
    Then I should see that my card has been declined

    View Slide

  18. Feature: Update card details
    Need to be able to update my card, e.g. if I decide
    to use a different one for billing.
    Scenario: Update card details successfully
    Given I have started a paid subscription on the pro plan
    When I choose to update which card is billed
    Then I should see that my card details have been saved
    Scenario: Use a bad card
    Given I have started a paid subscription on the pro plan
    When I try to change to an invalid card
    Then I should see that my card has been declined

    View Slide

  19. Feature: Update card details
    Need to be able to update my card, e.g. if I decide
    to use a different one for billing.
    Background:
    Given I have started a paid subscription on the pro plan
    Scenario: Update card details successfully
    When I choose to update which card is billed
    Then I should see that my card details have been saved
    Scenario: Use a bad card
    When I try to change to an invalid card
    Then I should see that my card has been declined

    View Slide

  20. Feature Scenario Step
    * *
    Background Step
    *
    0..1

    View Slide

  21. Feature: Update card details
    Need to be able to update my card, e.g. if I decide
    to use a different one for billing.
    Scenario Outline: Update card details
    Given I have started a paid subscription on the pro plan
    When I update my card details with a card
    Then I should see ""
    Examples:
    | card status | message |
    | valid | Your card details have been saved. |
    | expired | This card has expired. |
    | stolen | The police are on their way. |

    View Slide

  22. Feature Scenario Step
    * *
    Background Step
    *
    0..1
    Scenario
    Outline
    Step
    *
    *
    Example
    *

    View Slide

  23. module Cucumber
    module Ast
    class Background
    def accept(visitor)
    return if @already_visited
    visitor.visit_background(self) do
    comment.accept(visitor)
    visitor.visit_background_name(@keyword, name, file_colon_li
    with_visitor(feature_elements.first || self, visitor) do
    visitor.runtime.before(hook_context)
    skip_invoke! if failed?
    step_invocations.accept(visitor)
    @failed = step_invocations.any? do |step_invocation|
    step_invocation.exception ||
    step_invocation.status != :passed
    end
    if @failed || feature_elements.empty?
    visitor.runtime.after(hook_context)
    end
    end
    end
    @already_visited = true
    end

    View Slide

  24. Our First Mistake:
    Data-Centric Domain Model

    View Slide

  25. View Slide

  26. Problem
    Domain
    Solution
    Domain

    View Slide

  27. "Model the problem
    domain and the solution
    will take care of itself."
    DAVID WEST, OBJECT THINKING

    View Slide

  28. 1967

    View Slide

  29. SIMULA 67
    1967
    http://www.jot.fm/issues/issue_2002_09/eulogy/

    View Slide

  30. "It should be problem oriented and
    not computer oriented, even if this
    implies an appreciable increase in the
    amount of work that has to be done
    by the computer"
    SIMULA 67
    1967
    http://www.jot.fm/issues/issue_2002_09/eulogy/

    View Slide

  31. View Slide

  32. 1979

    View Slide

  33. C++
    1979
    "SIMULA's class-based system was a
    huge plus, but it's run-time
    performance was hopeless."

    View Slide

  34. SMALLTALK-80
    Called "Smalltalk"--as in
    "programming should be a matter
    of ..." [Smalltalk] and "children should
    program in ..." [Smalltalk].
    -- Alan Kay
    1980

    View Slide

  35. SIMULA 67
    1967
    C++
    1979
    Smalltalk-80
    1980

    View Slide

  36. Two philosophies

    View Slide

  37. Two cultures

    View Slide

  38. C++
    Smalltalk-80

    View Slide

  39. "Good" OOP "Bad" OOP
    FOCUS ON DATA
    FOCUS ON MESSAGES
    FOCUS ON BEHAVIOUR,
    RESPONSIBILITIES
    FOCUS ON STRUCTURE, LAYERS
    ABSORBED IN THE PROBLEM DISTRACTED BY THE SOLUTION
    CULTURE OF FLEXIBILITY,
    COLLABORATION, FUN, CHAOS
    CULTURE OF FORMALITY,
    HIERARCHY, CENTRALISED CONTROL

    View Slide

  40. View Slide

  41. 2.0

    View Slide

  42. View Slide

  43. Our first fix:
    A responsibility-centric
    model

    View Slide

  44. Compiler
    Runner
    Parser
    Report

    View Slide

  45. A design principle:
    Tell objects, ask values*
    * (thank GOOS)

    View Slide

  46. Compiler Runner
    Parser
    feature
    scenario
    step
    test_case
    step
    scenario
    step
    step
    test_case

    View Slide

  47. Compiler Runner
    test_case
    Report
    before_test_case(case)
    after_test_case(case, result)
    before_test_step(step)
    after_test_step(step, result)
    execute(step)

    View Slide

  48. LESS STATE,
    LESS BUGS

    View Slide

  49. YOUR PROGRAMS
    ARE PROBABLY
    TOO STUPID TO BE
    ALLOWED TO
    REMEMBER STUFF

    View Slide

  50. So remember...
    MODEL ACTIVITY, THEN PUSH
    DATA THROUGH THAT MODEL
    MAKE YOUR DATA IMMUTABLE
    TELL OBJECTS, ASK VALUES
    TREAT OBJECTS LIKE PEOPLE

    View Slide

  51. http://cucumber.pro

    View Slide