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

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. Applying the Rules of
    Simple Design
    @adamwathan

    View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  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

    View Slide

  10. The Rules of Simple Design
    1. Passes the tests
    2. Reveals intent

    View Slide

  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;
    }
    }

    View Slide

  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;
    }
    }

    View Slide

  13. The Rules of Simple Design
    1. Passes the tests
    2. Reveals intent
    3. No duplication

    View Slide

  14. class Book extends Model
    {
    public function author()
    {
    return $this->belongsTo(Author::class);
    }
    }

    View Slide



  15. {{ $book->title }}
    by {{ $book->author->name }}


    {{ $book->summary }}


    View Slide


  16. @foreach ($books as $book)

    {{ $book->title }}
    by {{ $book->author->name }}

    @endforeach

    View Slide

  17. class Book extends Model
    {
    public function authors()
    {
    return $this->belongsToMany(Author::class);
    }
    public function byline()
    {
    return $this->authors->pluck('name')->implode(' & ');
    }
    }

    View Slide



  18. {{ $book->title }}
    by {{ $book->byline() }}


    {{ $book->summary }}


    View Slide


  19. @foreach ($books as $book)

    {{ $book->title }}
    by {{ $book->byline() }}

    @endforeach

    View Slide

  20. The Rules of Simple Design
    1. Passes the tests
    2. Reveals intent
    3. No duplication
    4. Fewest elements

    View Slide

  21. View Slide

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

    View Slide

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

    View Slide

  24. Stop worrying about
    ARCHITECTURE

    View Slide

  25. Focus on the
    SMALL STUFF

    View Slide

  26. View Slide

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

    View Slide

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

    View Slide