SOLID e Best Practices

Quem Somos?


Guilherme Guitte (Guitte)

Fatec Sorocaba

Desenvolvedor Web

Já trabalhei com Ruby on Rails

Trabalho na Leroy Merlin (Laravel4)

Quem somos?


Luiz Fernando / Zizaco

  • Programador
  • Desenvolvedor web
  • FATEC Sorocaba
  • Artigos publicados
  • WebDesigner wannabe
  • Leroy Merlin (Laravel4)
  • Sempre mais


O que vamos falar hoje?


  • O que é SOLID?

  • O que cada letra significa + exemplos

  • Best Practices

  • Progressive enhancement

  • Conclusão






NO Cargo Cult Programming

O que é SOLID?





  • Quando falamos de SOLID, na verdade é S.O.L.I.D.
  • É uma sigla
  • Criado/nomeado por Michael Feathers/Robert C. Martin (Uncle Bob)
  • São 5 princípios

O que é solid?





S -> SRP -> Single Responsibility Principle
O -> OCP -> Open-closed Principle
L -> LSP -> Liskov Substitution Principle
I -> ISP -> Interface Segregation Principle
D -> DIP -> Dependecy Inversion Principle

SRP - Single responsibility Principle






"The Single Responsibility Principle states that a class should have one, and only one, reason to change.  "

Exemplo - errado


<?php

class Product extends Eloquent
{
    static public function last() {...}

    public function getPrice() {...}

    public function getImageUrl() {...}

    public function processImage() {...}

    static public function exportAll() {...}

    public function hasValidName() {...}

    public function isValid() {...}
}




EXEMPLO - Certo


<?php

class Product extends Eloquent
{
    public function __construct(ProductValidator $validator) {...}

    public function getPrice() {...}

    public function getImageUrl() {...}

    public function isValid() {...} // Uses $this->validator

    /** static public function last() {...} **/
    /** public function processImage() {...} **/
    /** static public function exportAll() {...} **/
    /** public function hasValidName() {...} **/
}

class ImageProcessor
{
    public function processImage(Product $product) {...}
}


OCP - Open closed Principle






"The Open Closed principle of SOLID design states that code is open for extension but closed for modification."

Exemplo - Errado

<?php

class Product extends Eloquent
{
    public function getPrice() {
        switch ($this->type) {
            case 'kit':
                // ...
                break;

            default:
                // ...
                break;
        }
    }
}
class Report
{
    public function Report(Product $product) {
        ...
        $product->getPrice();
        ...
    }
}

Exemplo - certo


<?php

class Product extends Eloquent
{
    public function getPrice() {...}
}

class ProductKit extends Product
{
    public function getPrice() {...}
}

class Report
{
    public function Report(Product $product) {
        ...
        $product->getPrice();
        ...
    }
}

LSP - Liskov Substitution Principle






"This principle states that objects should be replaceable with instances of their subtypes without altering the correctness of that program. "

Exemplo



<?php

Cache::put('foo', 'bar', 5);

Cache::get('foo', null);

Mail::send('emails.hiho', $data, function($message) {
    $message->to('zizaco@gmail.com', 'Zizaco')
        ->subject('Hiho!');
});



ISP - Interface Segregation Principle







"This principle states that no implementation of an interface should be forced to depend on methods it does not use. "

Exemplo - Errado


<?php
// NO CARGO CULT!!!!

interface ProductInterface
{
    static public function last();
    public function getPrice();
    public function getImageUrl();
    public function processImage();
    static public function exportAll();
    public function hasValidName();
    public function isValid();
}

Exemplo - certo


<?php

interface ProductInterface
{
    public function getPrice();
    public function isValid();
}

interface HasImageInterface
{
    public function getImageUrl();
}

Dependency Inversion Principle






"This principle states that high-level code should not depend on low-level code, and that abstractions should not depend upon details. "

Exemplo - Errado



<?php

class Product extends Eloquent
{
    public function isValid()
    {
        $validator = new ProductValidator;

        return $validator->validate($product);
    }
}

class ProductValidator
{
    public function validate(Product $product) {...}
}

Exemplo - Certo

<?php

class Product implements ProductInterface
{
    protected $validator;

    public function __construct(ValidatorInterface $validator)
    {
        $this->validator = $validator;
    }

    public function isValid()
    {
        return $this->validator->validate($product);
    }
}

class ProductValidator implements ValidatorInterface
{
    public function validate(ProductInterface $product)
}

Agora entendi...






Tudo faz sentido...






Quer dizer que agora sei tudo sobre SOLID?





Sim/não






Agora mão na massa!!






Progressive enhancement






Again, No Cargo Cult Programming





Conclusão




Links:

https://leanpub.com/laravel


Merchan:

http://codeinaction.org


Pulse:

https://github.com/Zizaco/pulse


Blog tech (Coming soon):

http://tech.leroymerlin.com.br





Obrigado =)

SOLID BEST PRACTICES

By guilhermeguitte

SOLID BEST PRACTICES

  • 3,061