Slide 1

Slide 1 text

Pense 4 vezes antes de fazer herança Leonardo Tegon - TDC Recife 2019

Slide 2

Slide 2 text

Leonardo Tegon twitter.com/tegonl github.com/tegon

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Birigui

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Herança

Slide 7

Slide 7 text

Programação Orientada a Objetos

Slide 8

Slide 8 text

Representação do mundo real

Slide 9

Slide 9 text

class Animal def andar end def falar end end

Slide 10

Slide 10 text

class Cachorro < Animal end class Gato < Animal end

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Pense em uma fruta

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

#1. Objetos devem ser modelados do ponto de vista de negócio

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

#2. Princípio de substituição de Liskov

Slide 23

Slide 23 text

O mesmo código que funciona com um tipo, deve funcionar com um subtipo

Slide 24

Slide 24 text

passaros.each do |passaro| passaro.voar end

Slide 25

Slide 25 text

passaros.each do |passaro| passaro.voar end => NotImplementedError: pinguins não voam

Slide 26

Slide 26 text

passaros.each do |passaro| passaro.voar unless passaro.is_a?(Pinguim) end

Slide 27

Slide 27 text

Mensagens que recebe e envia

Slide 28

Slide 28 text

Forte acoplamento

Slide 29

Slide 29 text

#3. Não deve ser somente para reutilizar código

Slide 30

Slide 30 text

class TextField def initialize(name, size, value) @name, @size, @value = name, size, value end def render ... end end

Slide 31

Slide 31 text

class TextField def initialize(name, size, value) @name, @size, @value = name, size, value end def render ... end end

Slide 32

Slide 32 text

class DateField < TextField def initialize(name, size, value) super(name, size, format_date(value)) end def format_date(value) value.strftime("%Y-%m-%d") end end

Slide 33

Slide 33 text

class DateField < TextField def initialize(name, size, value) super(name, size, format_date(value)) end def format_date(value) value.strftime("%Y-%m-%d") end end

Slide 34

Slide 34 text

class DateField < TextField def initialize(name, size, value) super(name, size, format_date(value)) end def format_date(value) value.strftime("%Y-%m-%d") end end

Slide 35

Slide 35 text

class TimeField < DateField def format_date(value) value.strftime("%T.%L") end end

Slide 36

Slide 36 text

class TimeField < DateField def format_date(value) value.strftime("%T.%L") end end

Slide 37

Slide 37 text

Cuidado com especializações pequenas

Slide 38

Slide 38 text

class TimeField < DateField def format_date(value) ... end end

Slide 39

Slide 39 text

Cuidado ao remover comportamentos

Slide 40

Slide 40 text

class DateField < TextField def initialize(name, size, value) super(name, size, format_date(value)) end def format_date(value) value.strftime("%Y-%m-%d") end end

Slide 41

Slide 41 text

class DateField < TextField def initialize(name, value) super(name, nil, format_date(value)) end def format_date(value) value.strftime("%Y-%m-%d") end end

Slide 42

Slide 42 text

Composição

Slide 43

Slide 43 text

class TextField def initialize(name, size, value, formatter) @name, @size = name, size @value = formatter.format(value) end def render ... end end

Slide 44

Slide 44 text

class DateFormatter def self.format(value) value.strftime("%Y-%m-%d") end end

Slide 45

Slide 45 text

Don't repeat yourself

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Don't repeat yourself?

Slide 48

Slide 48 text

#4. The Rule of Three

Slide 49

Slide 49 text

Três lugares ao invés de dois

Slide 50

Slide 50 text

https://martinfowler.com/books/refactoring.html

Slide 51

Slide 51 text

Regra não, guia

Slide 52

Slide 52 text

Duplication is far cheaper than the wrong abstraction — Sandi Metz

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Reconsidere a abordagem

Slide 55

Slide 55 text

Junte seu time e converse sobre o domínio

Slide 56

Slide 56 text

Junte seu time e converse sobre os casos de uso

Slide 57

Slide 57 text

Deixe as coisas iguais para achar o que é diferente

Slide 58

Slide 58 text

A chegada de uma nova feature que não encaixa na abstração atual é o melhor momento para refatorar

Slide 59

Slide 59 text

Valeu! twitter.com/tegonl github.com/tegon

Slide 60

Slide 60 text

Referências • https://thoughtbot.com/blog/reusable-oo-composition-vs-inheritance • https://medium.com/@rdsubhas/10-modern-software-engineering-mistakes-bc67fbef4fc8 • https://www.bennadel.com/blog/2483-object-thinking-by-david-west.htm • https://www.bennadel.com/blog/3108-elegant-objects-by-yegor-bugayenko.htm • https://www.sandimetz.com/blog/2016/1/20/the-wrong-abstraction • https://twitter.com/sandimetz/status/496627913010470912?lang=en • https://www.youtube.com/watch?v=8bZh5LMaSmE • https://www.youtube.com/watch?v=29MAL8pJImQ • https://www.youtube.com/watch?v=_f2LYPpueAY • https://www.youtube.com/watch?v=CXyNZYDO07Q

Slide 61

Slide 61 text

Referências • https://www.deconstructconf.com/2018/sandi-metz-polly-want-a-message • https://programmingisterrible.com/post/176657481103/repeat-yourself-do-more-than- one-thing-and • https://www.thoughtworks.com/insights/blog/composition-vs-inheritance-how-choose • https://www.rubypigeon.com/posts/refactoring-inheritance-composition-data/ • https://www.tomdalling.com/blog/software-design/ solid-class-design-the-liskov-substitution-principle/ • https://super.abril.com.br/mundo-estranho/tomate-e-fruta/ • https://en.wikipedia.org/wiki/Ruleofthree(computerprogramming) • https://en.wikipedia.org/wiki/Liskovsubstitutionprinciple • https://pt.wikipedia.org/wiki/Herança

Slide 62

Slide 62 text

Créditos das Fotos • Olhar Angolano: https://unsplash.com/photos/95fbuDeudRg • Sergio Souza: https://unsplash.com/photos/gTSi041JpVc • Jason Roberts: https://unsplash.com/photos/6AQY7pO1lS0 • Ricardo Viana: https://unsplash.com/photos/-tYsPFKMm7g • Alex Ghizila: https://unsplash.com/photos/UD_j10SKj5g • Thibault Penin: https://unsplash.com/photos/3HInbCmQ8ro • Dmitry Bayer: https://unsplash.com/photos/c5Nzpjd1sn4 • Michał Parzuchowski: https://unsplash.com/photos/geNNFqfvw48 • Perry Grone: https://unsplash.com/photos/lbLgFFlADrY