26
PROGRAMMATION STUPID VS SOLID

Programmation STUPID vs SOLID

Embed Size (px)

Citation preview

Page 1: Programmation STUPID vs SOLID

PROGRAMMATION STUPID VS SOLID

Page 2: Programmation STUPID vs SOLID

QUI SUIS-JE ?

Arnaud Langlade / @_aRn0D

Développeur Symfony chez Clever Age

Sylius core team member / @Sylius

www.clever-age.com / @CleverAge

Page 3: Programmation STUPID vs SOLID

STUPID, KÉZAKO ?SingletonTight couplingUntestabilityPremature OptimizationIndescriptive NamingDuplication

Page 4: Programmation STUPID vs SOLID

INSTANCES UNIQUES (SINGLETON)

class MyClass public function __contruct() $this­>connexion = Connexion::getInstance([ 'dsn' => 'mysql:host=localhost;dbname=my_db' ])

Un singleton ne facilite pas la configuration des objetsLes singletons introduisent des couplages forts entre les objetsPas vue globale sur l'application (les dépendences sont cachées)

Page 5: Programmation STUPID vs SOLID

COUPLAGE FORT (TIGHT COUPLING)

class MyClass public function __construct() $this­>connexion = new MyConnexion();

class MyClass public function __construct(Connexion $connexion) $this­>connexion = $connexion;

Difficulté dans la mise en place des testsImpossible de créer des mocksDépendence forte entre composants

Page 6: Programmation STUPID vs SOLID

NON TESTABILITÉ (UNTESTABILITY)

Eviter les bogues et les régressionsFaciliter la refactorisationAttention: Un code fortement couplé sera difficilementtestable

Page 7: Programmation STUPID vs SOLID

OPTIMISATIONS PRÉMATURÉES (PREMATURE OPTIMIZATION)

De simples applications peuvent se transformer en usine à gazRend difficile :

la lecture et compréhension du codela maintenance de votre application

Page 8: Programmation STUPID vs SOLID

NOMMAGE INDÉCHIFFRABLE (INDESCRIPTIVE NAMING)

class Quadrilateral public function getOutline($a, $b) return ($a + $b) * 2;

class Quadrilateral public function getPerimeter($with, $height) return ($with + $height) * 2;

Gain de temps lors de : la relecture, la maintenance et l’évolutionEviter les abréviations ou d'acronymes est fortement conseillé

Page 9: Programmation STUPID vs SOLID

DUPLICATIONS (DUPLICATION)

Une modification de code entraîne :

la répercuter le changement à plusieurs endroitsla maintenance de l'application plus difficile

Des outils automatisent la détection des duplications

PHP Copy Paste Detector (PHPC)SonarSource

Page 10: Programmation STUPID vs SOLID

SOLID, KÉZAKO ?Michael Feathers et Robert C. Martin (aka Uncle Bob)

Single Responsibility PrincipleOpen/Closed PrincipleLiskov Substitution PrincipleInterface Segregation PrincipleDependency Inversion Principle

Page 11: Programmation STUPID vs SOLID

PRINCIPE DE RESPONSABILITÉ UNIQUE (SINGLE RESPONSIBILITY PRINCIPLE)

Une classe doit remplir un rôle précis

Page 12: Programmation STUPID vs SOLID

PRINCIPE DE RESPONSABILITÉ UNIQUE (SINGLE RESPONSIBILITY PRINCIPLE)

class DataImporter public function import($file) $this­>writeData( $this­>loadFile($file) );

private function loadData($file) ... private function writeData(array $data) ...

Page 13: Programmation STUPID vs SOLID

PRINCIPE DE RESPONSABILITÉ UNIQUE (SINGLE RESPONSIBILITY PRINCIPLE)

class DataImporter public function __construct(Loader $loader, Writer $writer) $this­>loader = $loader; $this­>writer = $writer;

public function import($file) foreach($this­>loader­>load($file) as $item) $this­>writer­>write();

Page 14: Programmation STUPID vs SOLID

PRINCIPE DE RESPONSABILITÉ UNIQUE (SINGLE RESPONSIBILITY PRINCIPLE)

La classe est facilement testableFacilite l’évolution des implémentations existantesFacilite l'ajouter de nouvelles implémentations

Page 15: Programmation STUPID vs SOLID

PRINCIPE OUVERT / FERMÉ (OPEN/CLOSED PRINCIPLE)

Ce principe consiste à rendre les modules ouverts à l'extension et fermés auxmodifications

Page 16: Programmation STUPID vs SOLID

PRINCIPE OUVERT / FERMÉ (OPEN/CLOSED PRINCIPLE)

$importer = DataImporter(new CsvLoader(), new MysqlWriter());$importer = DataImporter(new XMLLoader(), new MongoWriter());

Page 17: Programmation STUPID vs SOLID

PRINCIPE OUVERT / FERMÉ (OPEN/CLOSED PRINCIPLE)

class UserControler public function createUser() // Event pre_create

// Enregistrement de l'utilisateur

// Event post_create

Page 18: Programmation STUPID vs SOLID

PRINCIPE DE SUBSTITUTION DE LISKOV (LISKOV SUBSTITUTION PRINCIPLE)

Si la classe T a une dépendance de type S alors on doit pouvoir remplacercette dépendance par tous types dérivés de S.

Formulé par Barbara Liskov et Jeannette Wing

Page 19: Programmation STUPID vs SOLID

PRINCIPE DE SUBSTITUTION DE LISKOV (LISKOV SUBSTITUTION PRINCIPLE)

class CsvLoader implements LoaderInterface public function load(File $file) // ...

class XmlLoader implements LoaderInterface public function load(File $file) // ...

class DataImporter public function load(LoaderInterface $loader, /* ... */) // ...

Page 20: Programmation STUPID vs SOLID

PRINCIPE DE SUBSTITUTION DE LISKOV (LISKOV SUBSTITUTION PRINCIPLE)

Implique de conserver de la classe parente (surtout en PHP) :

les mêmes signatures des méthodesla nature des valeurs de retour des méthodes

Diminution du couplage.

Page 21: Programmation STUPID vs SOLID

PRINCIPE DE SÉGRÉGATION D'INTERFACES (INTERFACE SEGREGATION PRINCIPLE)

Principe de responsabilité unique pour les interfaces

Page 22: Programmation STUPID vs SOLID

PRINCIPE D'INVERSION DE DÉPENDANCE (DEPENDENCY INVERSION PRINCIPLE)

Il faut dépendre des abstractions, pas des implémentations

Page 23: Programmation STUPID vs SOLID

PRINCIPE D'INJECTION/INVERSION DE DÉPENDANCE (DEPENDENCY INVERSION PRINCIPLE)

class DataImporter public function __construct() $this­>loader = new CsvLoader(); $this­>writer = new MysqlWriter();

class DataImporter public function __construct(CsvLoader $loader, MysqlGateway $writer) $this­>loader = $loader; $this­>writer = $writer;

Page 24: Programmation STUPID vs SOLID

PRINCIPE D'INVERSION DE DÉPENDANCE (DEPENDENCY INVERSION PRINCIPLE)

class DataImporter public function __construct(LoaderInterface $loader, WriterInterface $writer) $this­>loader = $loader; $this­>writer = $writer;

Page 25: Programmation STUPID vs SOLID

PRINCIPE D'INJECTION DE DÉPENDANCE (DEPENDENCY INVERSION PRINCIPLE)

Une nette diminution du couplageUne meilleure encapsulation

Page 26: Programmation STUPID vs SOLID

MERCI! QUESTIONS ?

Arnaud Langlade

Twiter @_aRn0D

Sylius : www.sylius.org