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

Applying the Rules of Simple Design

34147b9eecf59779b777eb68a1805113?s=47 Adam Wathan
February 05, 2016

Applying the Rules of Simple Design

You're starting a new project and this time you want to do it right. Everything's going to be test-first, your domain model is going to be perfectly portable, and your framework will be nothing but a mere detail.

But as soon as your fingers hit the keyboard you're paralyzed.

Should I use a command for this? How can I test this piece without hitting the database? Should these two things happen in the same place or should I fire an event?

This talk is about taking a step back and thinking about what really makes your code good. Using Kent Beck's four simple rules, I'll teach you how to get unstuck and stop worrying about the future by embracing emergent design and writing great code now.

34147b9eecf59779b777eb68a1805113?s=128

Adam Wathan

February 05, 2016
Tweet

More Decks by Adam Wathan

Other Decks in Programming

Transcript

  1. Applying the Rules of Simple Design @adamwathan

  2. None
  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. “I don’t really see the difference. Whether you have two

    elements in a statement, or two services in some distributed architecture, the principles are the same.” — Kent Beck
  10. The Rules of Simple Design 1. Passes the tests 2.

    Reveals intent
  11. class SalesReport { public function __construct($sales, $startDate, $endDate) { $this->sales

    = $sales; $this->startDate = $startDate; $this->endDate = $endDate; } public function totalRevenue() { $revenue = 0; foreach ($sales as $sale) { if ($sale->date >= $this->startDate && $sale->date <= $this->endDate) { $revenue += $sale->value; } } return $revenue; } }
  12. class SalesReport { // ... public function totalRevenue() { $revenue

    = 0; foreach ($sales as $sale) { if ($this->saleWithinRange($sale)) { $revenue += $sale->value; } } return $revenue; } private function saleWithinRange($sale) { return $sale->date >= $this->startDate && $sale->date <= $this->endDate; } }
  13. The Rules of Simple Design 1. Passes the tests 2.

    Reveals intent 3. No duplication
  14. <?php class Book extends Model { public function author() {

    return $this->belongsTo(Author::class); } }
  15. <div> <header> <h2>{{ $book->title }}</h2> <small>by {{ $book->author->name }}</small> </header>

    <div> {{ $book->summary }} </div> </div>
  16. <ul> @foreach ($books as $book) <li> <h2>{{ $book->title }}</h2> <small>by

    {{ $book->author->name }}</small> </li> @endforeach </ul>
  17. <?php class Book extends Model { public function authors() {

    return $this->belongsToMany(Author::class); } public function byline() { return $this->authors->pluck('name')->implode(' & '); } }
  18. <div> <header> <h2>{{ $book->title }}</h2> <small>by {{ $book->byline() }}</small> </header>

    <div> {{ $book->summary }} </div> </div>
  19. <ul> @foreach ($books as $book) <li> <h2>{{ $book->title }}</h2> <small>by

    {{ $book->byline() }}</small> </li> @endforeach </ul>
  20. The Rules of Simple Design 1. Passes the tests 2.

    Reveals intent 3. No duplication 4. Fewest elements
  21. None
  22. <?php class TagsController { public function store() { Tag::create(Request::all()); return

    Redirect::route('tags.index'); } }
  23. vs. class TagsController { public function store() { Tag::create(Request::all()); return

    Redirect::route('tags.index'); } }
  24. Stop worrying about ARCHITECTURE

  25. Focus on the SMALL STUFF

  26. None
  27. << Insert live code demo here >> https://github.com/adamwathan/simple-design-ssphp16/tree/ssphp-start

  28. @adamwathan adamwathan.me https://joind.in/talk/b0f5a