ruby sadism - ryan davis

146
GoRuCo 2008 Ryan Davis, Seattle.rb Machucando Código por Diversão e Lucro www.akitaonrails.com (Fabio Akita) goruco2008.confreaks.com/04_davis.html

Upload: fabio-akita

Post on 28-Jun-2015

4.443 views

Category:

Economy & Finance


1 download

DESCRIPTION

Versão em português da apresentação "Hurting Code for Fun and Profit" que o Ryan Davis deu na GoRuCo 2008.

TRANSCRIPT

Page 1: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

www.akitaonrails.com(Fabio Akita)

goruco2008.confreaks.com/04_davis.html

Page 2: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Machucando Código

por Diversãoe Lucro

Page 3: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Criando Expectativa

• 142 slides em 30 minutos.

• Isto será rápido.

• Não é a forma como normalmente apresento.

• É mais sobre melhorar você do que o código.

Page 4: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

UmaHistória

Page 5: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 6: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Código Legado

• Qualquer código que você não escreveu é legado.

• Toda empresa tem tonelada de legados.

• Mesmo startups.

Page 7: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 8: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 9: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 10: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 11: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 12: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 13: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Ou:

Page 14: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

New Place, Inc.

Page 15: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 16: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 17: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Page 18: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

@#$%!!!

Page 19: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

CRAC

K!

Page 20: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

BLAM!

Page 21: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

SNICK!CR

ACK!

BAM!!!

Page 22: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

YAY!

Page 23: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Moral desta história?

Page 24: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Pessoas vão te incriminar se machucá-las

Page 25: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Código não.

Page 26: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Esta apresentação é um evento de “saída”

para mim

Page 27: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Meu nome é Ryan, e…

Page 28: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

…adoro machucar código…

Page 29: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

…e estou Bem!

Page 30: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Por que machucar código?

• Machucar código é divertido!

• Pode te tornar um programador e testador melhor.

• Pode tornar o código mais limpo, claro e fácil de testar, escrever e manter ... até pode torná-lo mais rápido.

Page 31: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Sadismo Ruby:nomeA tendência de derivar prazer infringindo dor ao software.Esp. mal software.

Page 32: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

É apenas outra maneira de enxergar desenvolvimento de

software

Page 33: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Eu dou risada toda vez que acho código que

posso punir.

Page 34: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Código com complexidade que conheço, eu posso

resolver.

Page 35: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

É sobre mudar sua perspectiva sobre

código ruim.

Page 36: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Torne divertido descobrir e corrigir e fará isso mais vezes.

Page 37: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Sadismo Ruby vêm em várias formas:

Page 38: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Matar um bug escrevendo um teste.

Page 39: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Lendo código esperto demais e se coçar para quebrá-lo em pedaços.

Page 40: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Explodindo mal design, tornando-o mais rápido

com menos código.

Page 41: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

e muito, muito mais.

Page 42: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Um Exemplo

• Um projeto rails de mídia social de 2 meses foi arrastado por 12 meses.

• Ligando ActiveRecord a WebDAV. Emulando o banco de dados inteiro via propriedades WebDAV.Desnecessariamente complexo e lento!

• Então, arranquei o WebDAV num fim de semana.

Page 43: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Terminado em 74.77783 segundos.622 testes, 2502 asserções

Depois:

Antes:

Terminado em 5.258559 segundos.330 testes, 961 asserções

Page 44: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Tempo de Teste em 6 Meses

Post DAV

DAV

Page 45: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Por alguma Razão as pessoas amam complexidade.

Page 46: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Elas festejam nisso.

Page 47: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

A comunidade Ruby não se parece única

nesse sentido.

Page 48: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Eu não sou uma dessas pessoas.

Page 49: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Mas, eu costumava ser uma delas.

Page 50: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Eu melhorei...

Page 51: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

...e você também pode melhorar.

Page 52: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

20071984 1986 1988 1990 1992 1994 1996 1998 2000 2002 2004

10

0

1

2

3

4

5

6

7

8

9

Year

Fu/Sty

le U

nit

s

Pascal

C

Logo

UNIX/

Smalltalk/

OO Epiphany

C++

Ruby

Ruby

Sadism/

Asceticism

Perl/

Python/

etc

BASIC

Minha História de Programação

Page 53: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Ascetismo Ruby:nounCaracterizado por ou sugerindo a prática de severa auto-disciplina & abstenção de todas as formas de indulgência.

Page 54: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Flageladores

Page 55: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Testar Antes é uma forma de auto-

disciplina.

Page 56: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

YAGNI é uma forma de abstenção.

Page 57: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Ascéticos:

Vida Real™ Ruby

Evite possessões materiais.

Evite complexidade extra.

Auto-disciplina para chegar à alta

espiritualidade.

Auto-disciplina para melhor entender a si e

a mente do código.

Page 58: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

O que é indulgência?

• Código desnecessariamente complexo e que tenta se esperto demais.

• Código ou design que você não imediatamente precise. Os “e se”.

• Débito Técnico.

Page 59: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Débito Técnico

• Pular design é como pegar dinheiro emprestado.

• Refatoramento é como o princípio de pagar de volta.

• Juros são pagos com tempo gasto com complexidade.

—Ward Cunningham–ComplexityAsDebt, c2.com

Page 60: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

80:20 ou Princípio de Pareto

• Na maioria dos sistemas existe desequilíbrio entre produtores/consumidores, dinheiro/pessoas, etc.

• 85+% da terra é ocupada por 5% das pessoas.

• 80% do tempo é gasto com 20% do código.

• Use essa tendência para focar esforços de forma eficiente e objetiva.

Page 61: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Sinal : Barulho

• Normalmente relacionado a 80:20.

• Foque no que é importante.

• Minimize interrupções e tarefas irrelevantes para ter mais tarefas reais concluídas.

• Como meu gerente favorito gosta de falar:

• Grandes Crocodilos, Pequenos Crocodilos, Pântano.

Page 62: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Em outras palavras:

“A obrigação do desenvolvedor é garantir que o código sendo escrito mostre da maneira

mais clara sobre como a solução foi entendida no momento da sua escrita.”

–Ward Cunningham, c2.com

Page 63: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Um Exemplo

• test/unit & rspec são reis de testes/specs mas são realmente grandes.

• O mspec de Brian Ford (do rubinius) é mais novo, pequeno, & compatível com rspec.

• O bacon de Christian Neukirchen & test/spec são alternativas minimalistas de spec.

• miniunit vai mais fundo: test+spec+mock < 1kloc.

Page 64: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

0

3,750

7,500

11,250

15,000

test/unit mspec rspec

12,862

6,476

2,464

7,785

2,763

3,571

lib test/spec

Page 65: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

0

3,750

7,500

11,250

15,000

bacon miniunit test/spec

1,115578310

833395298

lib test/spec

Page 66: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

0

300

600

900

1,200

bacon miniunit test/spec

1,115

578

310

833

395

298

lib test/spec

Page 67: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Acredito que sadismo e ascetismo Ruby são

muito poderosos.

Page 68: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Mas chegar lá não é fácil.

Page 69: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Requer entender a si mesmo tão bem

quanto se entende o código.

Page 70: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Desenvolvimento Orientado a

Introspecção

Page 71: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Como faço melhor?Como não vi aquele bug?Onde estou perdendo em clareza?Como posso usar melhor minhas ferramentas?Onde desperdiço meu tempo?Como me foco melhor?Onde estou me segurando? estou errado?

Page 72: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Faça essas perguntas constantemente!

Page 73: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Auto-Melhoria:Leia!

• 1 livro nerd / mês = 12× média da indústria.

• c2.com & outros wikis c/ pessoas espertas.

• Fique tão próximo dos experts quanto posssível.

• Alguns blogs espertos.

Page 74: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Ignorância NÃO é boa

“Pessoas que fazem coisas de mal jeito, [...] normalmente são altamente confiantes em

suas habilidades, de fato, mais do que pessoas que fazem as coisas direito.”

Erica Goode, NYTreportando em Dr. David Dunning, Cornell

Page 75: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Auto-Melhoria:Foco!

• Fique longe de:

• WTF, coding horror, forums idiotas.

• Um bilhão de blogs em seu leitor de feed.

• Toneladas de mailing lists super-ativas.

• Trabalhe no que é realmente importante.

Page 76: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

–7 Habits of Highly Effective People–Covey

Urgente Não Urgente

Importante

Não Importante

PressãoApagar Incêndios

Projetos orientados a datas

PrevençãoPlanejamento Pró-ativo

Implementando SistemasPrevenção de Cansaço

InterrupçõesApagar alguns IncêndiosMaioria das Reuniões

TVYoutube

Outras perdas de tempo

Page 77: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

• Aprenda uma nova linguagem por ano.

• Aprenda suas ferramentas, a fundo. Agora mais a fundo.

• Examine seus hábitos de desenvolvimento.

• Codifique com outras pessoas.

• Estude alguma coisa não orientada a código.

• Pessoas de multi-disciplinares sempre são mais interessante e codificam melhor.

Auto-Melhoria:Cresça!

Page 78: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Auto-Melhoria:Empurre!

Escreva mais código. Muito.

Quantidade > Qualidade (algumas vezes)

Page 79: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Desafio dos Potes

• Um professor de potes disse à metade da classe que suas notas dependeriam de uma única peça que eles produziriam no final do curso.

• Ele disse à outra metade que suas notas seriam baseadas no volume de todas as peças criadas.

• Qual metade você acha que produziu melhores peças ao final?

Page 80: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

“A função da grande maioria dos trabalhos artísticos é simplesmente lhe ensinar como

fazer a pequena fração de sua arte que levanta vôo.

Uma das lições básicas e difíceis que todo artista precisa aprender é que mesmo as

peças fracassadas são essenciais.”

—Art & Fear—Bayles & Orland

Page 81: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

• Seja competitivo. Competição é prática.

• Desafie o Status Quo.

• Eles estão quase sempre errados.

• (Mesmo quando estão certos)

Auto-melhoria:Pressione, mais!

Page 82: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Não seja uma ovelha!

Page 83: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Auto-melhoria:Sinta!

• Deixe as opiniões fluírem. Tenha opinião!

• Mas não as deixe mandar em você.

• zentest, flog, & heckle todos vieram do amor por boas ferramentas de desenvolvimento.

• vlad & image_science vieram do ódio de código impossivelmente complexo.• Eu gastei 10 hrs debugando UM problema de

capistrano antes de escrever vlad.

Page 84: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Auto-melhoria:Feedback

• Use feedback:

• Peça por ele.

• Ouça ele.

• Retroalimente o feedback.

Page 85: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Auto-melhoria:Feedback

• Use feedback:

• Peça por ele.

• Ouça ele.

• Retroalimente o feedback.

Page 86: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Encontre seu balanço

Page 87: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Ferramentas que podem

ajudar

Page 88: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

autotest

• Você provavelmente não tinha percebido que usar autotest é uma forma de sadismo e ascetismo ruby, não é?

• Pobre cara do Doom ...

Page 89: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

autotest

• Você provavelmente não tinha percebido que usar autotest é uma forma de sadismo e ascetismo ruby, não é?

• Pobre cara do Doom ...

Page 90: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

• Relator de complexidade baseado em métricas ABC.

• Nota Flog é correlato com complexidade de teste.

• Quanto maior a nota flog, mais difícil de ter um teste completo (& bom).

Flogclass Test def blah # 11.2 = a = eval "1+1" # 1.2 + 6.0 + if a == 2 then # 1.2 + 1.2 + 0.4 + puts "yay" # 1.2 end endend

Test#blah: (11.2) 6.0: eval 1.2: branch 1.2: == 1.2: puts 1.2: assignment 0.4: lit_fixnum

Page 91: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

main#none: (22.9) 7.0: to_proc_normal 3.3: assignment 3.1: branch 2.0: inject 1.8: constantize 1.8: association 1.6: columns 1.6: options 1.4: [] 1.4: block_pass 1.4: map 1.2: include? 1.2: keys

Complexidade Desnecessária 1if MODELS.keys.inject(true) {|b, klass| b and klass.constantize.columns.map(&:name).include? association.options[:foreign_key]} then # ...end

1linha if(!) 132caracteres 132 méd carac22.9flog

Page 92: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Complexidade Desnecessária 2

main#none: (23.5) 7.5: to_proc_normal 4.7: assignment 3.4: branch 2.2: inject 1.9: constantize 1.7: columns 1.5: block_pass 1.5: map 1.5: association 1.3: include? 1.3: keys 1.3: options 1.1: []

fk = association.options[:foreign_key]if MODELS.keys.inject(true) {|b, klass| b and klass.constantize.columns.map(&:name).include? fk} then # ...end

3linhas 144caracteres 48 méd carac.23.5flog

Page 93: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Complexidade Desnecessária 3

main#none: (22.8) 7.0: assignment 6.0: to_proc_normal 4.5: branch 2.3: map 2.2: inject 1.6: constantize 1.5: association 1.4: columns 1.3: include? 1.3: options 1.3: keys 1.2: block_pass 1.1: []

fk = association.options[:foreign_key]columns = MODELS.keys.map { |key| key.constantize.columns.map(&:name) }if columns.inject(true) {|b, column| b and column.include? fk} then # ...end

4linhas 182caracteres 45 méd caract23.5flog

Page 94: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Complexidade Desnecessária 4

main#none: (20.8) 6.0: to_proc_normal 4.6: assignment 3.3: branch 2.3: map 1.6: constantize 1.5: association 1.4: columns 1.3: keys 1.3: options 1.2: block_pass 1.2: include? 1.1: [] 1.1: all?

fk = association.options[:foreign_key]columns = MODELS.keys.map { |key| key.constantize.columns.map(&:name) }if columns.all? {|column| column.include? fk} then # ...end

4linhas 165caracteres 41 méd carac20.8flog

Page 95: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Complexidade Desnecessária 5

main#none: (15.9) 5.9: assignment 4.5: branch 2.3: map 1.6: constantize 1.5: association 1.4: columns 1.3: name 1.3: options 1.3: keys 1.2: include? 1.1: [] 1.1: all?

fk = association.options[:foreign_key]columns = MODELS.keys.map { |key| key.constantize.columns.map { |c| c.name } }if columns.all? {|column| column.include? fk} then # ...end

4linhas 172caracteres 43 méd carac15.9flog

Page 96: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Complexidade Desnecessária 5

main#none: (15.9) 5.9: assignment 4.5: branch 2.3: map 1.6: constantize 1.5: association 1.4: columns 1.3: name 1.3: options 1.3: keys 1.2: include? 1.1: [] 1.1: all?

fk = association.options[:foreign_key]columns = MODELS.keys.map { |key| key.constantize.columns.map { |c| c.name } }if columns.all? {|column| column.include? fk} then # ...end

4linhas 172caracteres 43 méd carac15.9flog

Muito mais limpo, claro e rápido

Page 97: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Ferramentas de Cobertura

São excelentes para mostrar buracos.

Page 98: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

…mas…

Page 99: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

De jeito nenhum significam qualidade

dos testes.

Page 100: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

"Temos 100% de cobertura"

Page 101: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

… E daí?!?

Page 102: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Você pode ter 100% de cobertura com 0

asserções.

Page 103: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

(ou, mais freqüente:más asserções)

Page 104: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Exemplo absurdoclass Vida def significado 42 endend

class TestVida < Test::Unit::TestCase def test_significado real = Vida.new.significado esperado = 6 * 9 assert_equal esperado, real endend

VIVA!100% de

cobertura!

Page 105: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

O que 100% de cobertura significa

então?

Page 106: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Cobertura é o Raio-X do mundo do código.

Page 107: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Raios-X são ótimas ferramentas, mas não a use quando você está

sangrando.

Page 108: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Heckle

• Ferramenta mais sádica que escrevi.

• Muda sua implementação (em tempo real) para verificar se seus testes são bons.

• Se seus testes passam depois disso, você está perdendo casos.

Page 109: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Para Repetir

Page 110: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Divirta-se machucando código ruim e você terá

menos deles com o tempo.

Page 111: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Pratique severa auto-disciplina & abstenção.

Page 112: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Evite complexidade.

Page 113: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Pratique Desenvolvimento

Orientado à Introspecção.

Page 114: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Conheça-te.Melhora-te.

Page 115: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Use todas as ferramentas que puder.

Page 116: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Mas use a ferramenta certa para o trabalho.

Page 117: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Tenha uma opinião&

Sinta seu código

Page 118: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Para Fechar

Page 119: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Frank Herbert estava errado

Page 120: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Medo não é o assassino da mente

Page 121: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Apatia é o assassino da mente

Page 122: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Você precisa se importar pelo código

Page 123: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Ame-o

Page 124: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

ou o Odeie

Page 125: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Qualquer um é melhor do que Apatia

Page 126: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Preferível ao desenvolvedor que diz

Page 127: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

“Apenas sou pago para escrever código”

Page 128: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Você vai acabar tendo que manter o código deles

Page 129: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

(porque eles vão embora)

Page 130: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Odiando o código deles.

Page 131: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

O impacto deles no projeto.

Page 132: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Na produtividade e moral da equipe.

Page 133: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Portanto, Ame o código…

Page 134: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

…ou o Odeie.

Page 135: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Se você o amar:

Page 136: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Cuide dele.

Page 137: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Faça o crescer.

Page 138: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Torne-o maravilhoso.

Page 139: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Se você o odiar:

Page 140: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Machuque-o.

Page 141: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Despedace-o.

Page 142: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Reescreva-o.

Page 143: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Seja apaixonado pelo que está trabalhando.

Page 144: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Caso contrário você está morto por dentro

Page 145: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

…assim como seu código.

Page 146: Ruby Sadism - Ryan Davis

GoRuCo 2008

Ryan Davis, Seattle.rbMachucando Código por Diversão e Lucro

Obrigado.