Why setters are bad

Why setters are bad

Inspired by "Implementing DDD" some examples why setters are bad and how you can avoid these - Internal tech-talk at move:elevator

C739f65cecbb38923d95e9b7cc0d2e63?s=128

Tommy Mühle

May 02, 2017
Tweet

Transcript

  1. Tommy Mühle | tommy-muehle.io Why setters are bad 1

  2. Tommy Mühle | tommy-muehle.io They have no context 2

  3. Tommy Mühle | tommy-muehle.io Objects are not immutable 3 *Depends

    on the context
  4. Tommy Mühle | tommy-muehle.io Common used anti patterns 4

  5. Tommy Mühle | tommy-muehle.io Setter for everything 5

  6. Tommy Mühle | tommy-muehle.io 6 class Article { private $state

    = 'unknown'; // ... public function setState($state) { if ($state === 'unknown') { // ... } $this->state = $state; } }
  7. Tommy Mühle | tommy-muehle.io Use business relevant method names 7

  8. Tommy Mühle | tommy-muehle.io 8 class Article { private $state

    = 'unknown'; // ... public function publish() { $this->state = 'published'; } }
  9. Tommy Mühle | tommy-muehle.io Concrete business endpoints 9

  10. Tommy Mühle | tommy-muehle.io 10

  11. Tommy Mühle | tommy-muehle.io 10

  12. Tommy Mühle | tommy-muehle.io 11

  13. Tommy Mühle | tommy-muehle.io Use named constructors for objects in

    special state 12
  14. Tommy Mühle | tommy-muehle.io 13 class Article { private $title;

    private $state; // ... public static function draft($title) { $article = new self(); $article->state = 'draft'; $article->title = $title; return $article; } }
  15. Tommy Mühle | tommy-muehle.io Boolean flags 14

  16. Tommy Mühle | tommy-muehle.io 15 class Article { private $deleted

    = false; // ... public function setDeleted($deleted) { $this->deleted = (bool) $deleted; } // ... } Boolean flag parameters
  17. Tommy Mühle | tommy-muehle.io 16 Avoid boolean parameters class Article

    { private $deleted = false; // ... public function delete() { $this->deleted = true; } // ... }
  18. Tommy Mühle | tommy-muehle.io Setter-Injection 17

  19. Tommy Mühle | tommy-muehle.io 18 Avoid setter injection class MyService

    { private $logger; // ... public function setLogger($logger) { $this->logger = $logger; } }
  20. Tommy Mühle | tommy-muehle.io 19 Avoid unnecessary conditions class MyService

    { private $logger; // ... public function myMethod(array $parameters) { // ... if ($this->logger instanceof LoggerInterface) { $this->logger->log('...'); } } }
  21. Tommy Mühle | tommy-muehle.io Use constructor arguments instead 20

  22. Tommy Mühle | tommy-muehle.io 21 class MyService { private $logger;

    // ... public function __construct($logger) { $this->logger = $logger; } } Make immutable objects
  23. Tommy Mühle | tommy-muehle.io Special cases 22

  24. Tommy Mühle | tommy-muehle.io Symfony forms 23

  25. Tommy Mühle | tommy-muehle.io Use DataMappers 24

  26. Tommy Mühle | tommy-muehle.io Decouple form from entity 25

  27. Tommy Mühle | tommy-muehle.io Further cases … 26

  28. Tommy Mühle | tommy-muehle.io Summary 27

  29. Tommy Mühle | tommy-muehle.io Try to avoid setters anytime 28

  30. Questions?

  31. Thank you! Slides http:/ /bit.ly/2pTywBB @tommy_muehle