Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Surrender The Illusion of Control

Cb98700ec5151a02c191a62cb0d17005?s=47 Steve Tooke
September 07, 2015

Surrender The Illusion of Control

SwanseaCon 2015

Cb98700ec5151a02c191a62cb0d17005?s=128

Steve Tooke

September 07, 2015
Tweet

Transcript

  1. Surrender The Illusion of Control @tooky – SwanseaConf 2015

  2. Let It Go @tooky – SwanseaConf 2015

  3. Caveat

  4. What is good design?

  5. Why is good design important?

  6. None
  7. None
  8. None
  9. None
  10. None
  11. Reduce the Cost of Change

  12. None
  13. XP

  14. TDD

  15. JUnit

  16. 4 Rules of Simple Design

  17. None
  18. None
  19. None
  20. None
  21. None
  22. Your code is your understanding of the problem you're exploring.

  23. So it's only when you have your code in your

    head that you really understand the problem. 1 Paul Graham
  24. None
  25. Connascence

  26. None
  27. None
  28. Connascence1 1. The birth of two or more things at

    the same time. 2. The act of growing together 1 From wiktionary
  29. two components are connascent if a change in one would

    require the other to be modified in order to maintain the overall correctness of the system
  30. Static Connascence 1. Connascence of Name 2. Connascence of Type

    3. Connascence of Meaning 4. Connascence of Algorithm 5. Connascence of Position
  31. Dynamic Connascence 6. Connascence of Execution 7. Connascence of Timing

    8. Connascence of Value 9. Connascence of Identity
  32. Connascence of Name

  33. Connascence of Name class CardTest < Minitest::Test def test_high_card_beats_low_card high_card

    = Card.new("10", "♥") low_card = Card.new("2", "♣") assert high_card.beats?(low_card) end end class Card < Struct.new(:rank, :suit) def beats?(other_card) true end end
  34. Connascence of Type

  35. Connascence of Type public class DiamondTest { @Test public void

    first_row_of_A_diamond() { String diamond = Diamond.upTo("A"); assertThat(diamond, is("A")); } @Test public void first_row_of_B_diamond() { String diamond = Diamond.upTo("B"); assertThat(diamond, is("-A-")); } @Test public void first_row_of_C_diamond() { String diamond = Diamond.upTo("C"); assertThat(diamond, is("--A--")); } }
  36. Connascence of Type public class Diamond { public static String

    upTo(String letter) { if ("C".equals(letter)) { return "--" + "A" + "--"; } if ("B".equals(letter)) { return "-" + "A" + "-"; } return "A"; } }
  37. public class DiamondTest { @Test public void first_row_of_A_diamond() { String

    diamond = Diamond.upTo("A"); assertThat(diamond, is("A")); } // ... } public class Diamond { public static String upTo(String letter) { if ("C".equals(letter)) { return "--" + "A" + "--"; } if ("B".equals(letter)) { return "-" + "A" + "-"; } return "A"; } }
  38. Connascence of Type public class DiamondTest { @Test public void

    first_row_of_A_diamond() { String diamond = Diamond.upTo('A'); assertThat(diamond, is("A")); } @Test public void first_row_of_B_diamond() { String diamond = Diamond.upTo('B'); assertThat(diamond, is("-A-")); } @Test public void first_row_of_C_diamond() { String diamond = Diamond.upTo('C'); assertThat(diamond, is("--A--")); } }
  39. Connascence of Type public class Diamond { public static String

    upTo(char letter) { if ('C'.equals(letter)) { return "--" + "A" + "--"; } if ('B'.equals(letter)) { return "-" + "A" + "-"; } return "A"; } }
  40. Connascence of Meaning

  41. Connascence of Meaning public class Diamond { public static String

    upTo(char letter) { if ('C'.equals(letter)) { return "--" + "A" + "--"; } if ('B'.equals(letter)) { return "-" + "A" + "-"; } return "A"; } }
  42. Connascence of Name > Connascence of Meaning public class Diamond

    { public static final char START_LETTER = 'A'; public static String upTo(char letter) { if ('C'.equals(letter)) { return "--" + START_LETTER + "--"; } if ('B'.equals(letter)) { return "-" + START_LETTER + "-"; } return valueof(START_LETTER); } }
  43. Connascence of Algorithm

  44. Connascence of Algorithm public class Diamond { public static final

    char START_LETTER = 'A'; public static String upTo(char letter) { if ('C'.equals(letter)) { return "--" + START_LETTER + "--"; } if ('B'.equals(letter)) { return "-" + START_LETTER + "-"; } return valueof(START_LETTER); } }
  45. Connascence of Algorithm public class Diamond { public static final

    char START_LETTER = 'A'; public static String upTo(char letter) { if ('C'.equals(letter)) { return "--" + START_LETTER + "--"; } if ('B'.equals(letter)) { return "-" + START_LETTER + "-"; } return "" + START_LETTER + ""; } }
  46. Connascence of Name > Connascence of Algorithm public class Diamond

    { public static final char START_LETTER = 'A'; public static String upTo(char letter) { return padding(letter) + START_LETTER + padding(letter); } private static string padding(char letter) { int paddingSize = letter - START_LETTER; return StringUtils.repeat("-", paddingSize); } }
  47. Connascence of Position

  48. Connascence of Position class CardTest < Minitest::Test def test_high_card_beats_low_card high_card

    = Card.new("10", "♥") low_card = Card.new("2", "♣") assert high_card.beats?(low_card) end end class Card < Struct.new(:rank, :suit) end
  49. Connascence of Position class CardTest < Minitest::Test def test_high_card_beats_low_card high_card

    = Card.new("10", "♥") low_card = Card.new("2", "♣") assert high_card.beats?(low_card) end end class Card < Struct.new(:suit, :rank) end
  50. Connascence of Position class CardTest < Minitest::Test def test_high_card_beats_low_card high_card

    = Card.new("♥", "10") low_card = Card.new("♣", "2") assert high_card.beats?(low_card) end end class Card < Struct.new(:suit, :rank) end
  51. Connascence of Name > Position class Card def initialize(rank:, suit:)

    @rank = rank @suit = suit end # ... end
  52. Connascence of Name > Position class CardTest < Minitest::Test def

    test_high_card_beats_low_card high_card = Card.new(rank: "10", suit: "♥") low_card = Card.new(suit: "♣", rank: "2") assert high_card.beats?(low_card) end end
  53. Locality

  54. None
  55. None
  56. Locality class AvailableJobs def jobs [[customer, description]] end def assign_to(worker)

    jobs.each do |customer, description| # ... end end end
  57. Locality class AvailableJobs def jobs [[description, customer]] end def assign_to(worker)

    jobs.each do |customer, description| # ... end end end
  58. Locality class CompletedJobs def jobs [[customer, description]] end end

  59. Locality class InvoiceClerk def generate_job_invoices(jobs) jobs.each do |customer, description| #

    ... end end end
  60. Degree

  61. Degree class CompletedJobs def jobs [[customer, reference, description, start_time, finish_time,

    rate]] end end
  62. Degree class InvoiceClerk def generate_job_invoices(jobs) jobs.each do |customer, reference, description,

    start_time, finish_time, rate| # ... end end end
  63. Degree class CompletedJob attr_reader :customer, :reference, :description, :start_time, :finish_time, :rate

    end
  64. Degree class CompletedJobs def jobs [job] end end

  65. Degree class InvoiceClerk def generate_job_invoices(jobs) jobs.each do |job| # ...

    end end end
  66. Rules

  67. The further apart two components are use weaker forms of

    connascence
  68. When you have a high degree of connascence convert to

    a weaker form of connascence
  69. Good Design?

  70. Connascence

  71. Red, Green... what now?2 2 http://silkandspinach.net/2015/05/20/red-green-what-now/

  72. Principles

  73. SOLID

  74. DRY

  75. Law of Demeter

  76. Principles

  77. None
  78. Questions

  79. http://tooky.co.uk steve@boxjump.co.uk @tooky

  80. Photos good_design_1.jpg: https://www.flickr.com/photos/opalsson/ bad_design.jpg: https://www.reddit.com/r/todayilearned/comments/2dvvja/til_that_in_order_to_preserve_the_states_natural/ greenfield.jpg: https://www.flickr.com/photos/ks_marks/ stuckInTheMud.jpg: https://www.flickr.com/photos/puuikibeach/ unicorn.jpg:

    https://www.flickr.com/photos/wayfinder/ jimWeirich.jpg: https://www.flickr.com/photos/fraserspeirs/ celebrate.jpg: https://www.flickr.com/photos/clement127/ modular.jpg: https://www.flickr.com/photos/rahul3/ connected.jpg: https://www.flickr.com/photos/bernd_thaller/ reduceCost.jpg: https://www.flickr.com/photos/68751915@N05/ change.jpg: https://www.flickr.com/photos/petereed/