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

Applying the Rules of Simple Design

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.

Adam Wathan

February 05, 2016
Tweet

More Decks by Adam Wathan

Other Decks in Programming

Transcript

  1. “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
  2. 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; } }
  3. 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; } }
  4. The Rules of Simple Design 1. Passes the tests 2.

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

    return $this->belongsTo(Author::class); } }
  6. <ul> @foreach ($books as $book) <li> <h2>{{ $book->title }}</h2> <small>by

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

    return $this->belongsToMany(Author::class); } public function byline() { return $this->authors->pluck('name')->implode(' & '); } }
  8. <ul> @foreach ($books as $book) <li> <h2>{{ $book->title }}</h2> <small>by

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

    Reveals intent 3. No duplication 4. Fewest elements