Simple
Design
Principles

Tweeked by J. Reuben Wetherbee

@jrweth / wetherbe@sas.upenn.edu

Acknowledgements

Completely Based On Conference Talk
by Adam Wathan / @adamwathan
at Sunshine PHP Conference
Sunshine PHP Logo

Presentation in reveal.js

My Developer History

1984 LOGO: Turtle Program logo
1989 BASIC: Tripeaks Solitaire
10 PRINT "Hi World"
20 GOTO 10
                                        
1999 VBA/Access: Faculty Database (Medicine) logo
2001 PHP: Faculty Database Online logo

Why I Liked Adam Wathan's Talk

  • I Learned Nothing New
  • Beautiful, Simple, Code
  • Test Driven Development
  • Live Coding

Architecture!

Domain Driven Design Hexagonal Architecture Microservice Architecture

Kent Beck

Extreme Programmer!

"I don't really see the difference. Whether you have two elements in a statment, or two services in some distributed architecture, the principles are the same"

Rules of Simple Design

  1. Reveal Intent
class SalesReport {
    public function __construct($sales, $startDate, $endDate)
    {
        $this->sales = $sales;
        $this->startDate = $startDate;
        $this->endDate = $endDate;
    }
    public function totalRevenue()
    {
        $revenue = 0;
        foreach($this->sales as $sale) {
            if($sale->date >= $this->startDate && $sale->date <= $this->endDate) {
                $revenue += $sale->value;
            }
        }
        return $revenue;
    }
}
class SalesReport {
    public function __construct($sales, $startDate, $endDate)
    {
        $this->sales = $sales;
        $this->startDate = $startDate;
        $this->endDate = $endDate;
    }
    public function totalRevenue()
    {
        $revenue = 0;
        foreach($this->sales as $sale) {
            if($this->saleWithinRange($sale)){
                $revenue += $sale->value;
            }
        }
        return $revenue;
    }
    private function saleWithinRange($sale) {
        if($sale->date >= $this->startDate && $sale->date <= $this->endDate) {
            return true;
        }
        else return false;
    }
} 

Rules of Simple Design

  1. Reveal Intent
  2. Don't Repeat Yourself
class Book {
    public function author() {
        return $this->author;
    }
} 

{{ foreach $book in $books}}
<tr>
    <td>{{ $book->title }}</td> <td>{{$book->author->name}}</td>
</tr>
{{ endforeach }}

Title
{{ $book->title }}
Author
{{ $book->author->name }}
Summary
{{ $book->summary }}

Enter Unforeseen Complexity

class Book {
    public function authors() {
        return $this->authors;
    }

    public function byLine(){
        $authorNames = array();
        foreach($this->authors as $author) $authorNames[] = $author->name;
        return join(', ', $authorNames);
    }
} 

{{ foreach $book in $books}}
<tr>
    <td>{{ $book->title }}</td> <td>{{$book->byLine()}}</td>
</tr>
{{ endforeach }}

Title
{{ $book->title }}
Author
{{ $book->byLine() }}
Summary
{{ $book->summary }}

Rules of Simple Design

  1. Reveal Intent
  2. Don't Repeat Yourself
  3. Fewest Elements
    (Do I really need this?)

Rules of Simple Design

  1. Reveal Intent
  2. Don't Repeat Yourself
  3. Fewest Elements
    (Do I really need this?)
  4. Passes Tests

Does it Work?

Does it Still Work?

Demo