Write Rock SOLID Code

You shall not write STUPID code

Avoid Singleton

  • using global state is difficult to test
  • hides code dependencies
  • modifying singleton can have unexpected side effects

Don't create Tight coupled code

class House {
    public function __construct() {
        $this->door   = new Door;
        $this->window = new Window;
    }
}

Don't create Tightly coupled code

function setLogger(Monolog\Logger $logger) {...}

vs

function setLogger(Psr\Log\LoggerInterface $logger) {...}

Don't release Untested stuff

Premature optimization
is the root of all evil

  • Make it work.
  • Make it right (as in good OOP).
  • Then make it fast.

Dependant on context (volume, usage frequency...)

Don't give Indescriptive naming

What the heck ? strpbrk, strcspn

function createLogEntry($action, $object, LoggableAdapter $ea)

Remove Duplicated code

Remove Duplicated code

Remove Duplicated code

Duh

Knowing what to avoid is good


Knowing what to do is better

SOLID

5 principles found by Robert C. Martin

Single responsibility principle

A class should have one, and only one, reason to change.
class Book {
    function getTitle() {
        return "A Great Book";
    }
    function getAuthor() {
        return "John Doe";
    }
    function turnPage() {
        // pointer to next page
    }
    function getCurrentPage() {
        return "current page content";
    }
    function save() {
        $filename = '/documents/'. $this->getTitle(). ' - ' . $this->getAuthor();
        file_put_contents($filename, serialize($this));
    }
}

Open/closed principle

Objects or entities should be open for extension, but closed for modification.
It's funny, I opened 5 pages on that aspect and found 5 different interpretations. Maybe another one here :p

Open/closed principle

Especially useful for libraries, rigidify your code

  • Put properties private and provide getter/setter
  • Use the final keyword with caution
  • Put the proper docblock comments

Liskov substitution principle

Derived classes must be substitutable for their base classes.

Liskov substitution principle

class Square extends Rectangle {
    public function setHeight($value) {
        $this->width = $value;
        $this->height = $value;
    }

    public function setWidth($value) {
        $this->width = $value;
        $this->height = $value;
    }
}

Interface segregation principle

Make fine grained interfaces that are client specific.

Interface segregation principle

interface Vehicule {
    public function getSpeed();
    public function getMotor();
}

Interface segregation principle

interface Vehicule {
    public function getSpeed();
}

interface MotorizedVehicule {
    public function getMotor();
}

class Bicycle implements Vehicule {...}

class Car implements Vehicule, MotorizedVehicule {...}

Dependency inversion principle

Depend on abstractions, not on concretions.
function setLogger(Psr\Log\LoggerInterface $logger) {...}

Summary

Singleton
Tightly coupled code
Untested stuff
Premature optimization
Indescriptive naming
Duplicated code
Single responsibility principle
Open/closed principle
Liskov substitution
Interface segregation
Dependency inversion principle

Questions?