134
js limpo e testável com segregated DOM Luciano Ratamero TDC - Porto Alegre setembro - 2015

JS limpo e testável com Segregated DOM

Embed Size (px)

Citation preview

Page 1: JS limpo e testável com Segregated DOM

js limpo e testável com segregated DOM

Luciano RatameroTDC - Porto Alegresetembro - 2015

Page 2: JS limpo e testável com Segregated DOM

[email protected]@lucianoratamero

fb.com/lucianoratamerolucianoratamero.github.io

tudo que for:

lucianoratamero(sério)

Page 3: JS limpo e testável com Segregated DOM

agradecimentos

Page 4: JS limpo e testável com Segregated DOM

históriasemi

fictícia

Page 5: JS limpo e testável com Segregated DOM
Page 6: JS limpo e testável com Segregated DOM

17 19 21

Page 7: JS limpo e testável com Segregated DOM

dojo UFF

valeu moçada ;]

Page 8: JS limpo e testável com Segregated DOM

html+

css

Page 9: JS limpo e testável com Segregated DOM

jQuery

Page 10: JS limpo e testável com Segregated DOM

jQuery!=

javascript

Page 11: JS limpo e testável com Segregated DOM

elogroup

Page 12: JS limpo e testável com Segregated DOM

elogroup|

multidisciplinar

Page 13: JS limpo e testável com Segregated DOM

vantagens•  revisão e sugestão de código

•  manutenção coletiva•  menos feudos, mais

referências•  fim do ciúme de código

•  consciência geral (general conscience) sobre os projetos

Page 14: JS limpo e testável com Segregated DOM
Page 15: JS limpo e testável com Segregated DOM

python + django

Page 16: JS limpo e testável com Segregated DOM

python + django

precisava aprender mais

técnicas

Page 17: JS limpo e testável com Segregated DOM

python + django

precisava aprender mais

técnicas

OO SOLID

TDDBDD

DDD

Page 18: JS limpo e testável com Segregated DOM

dificuldade

Page 19: JS limpo e testável com Segregated DOM

eu back

Page 20: JS limpo e testável com Segregated DOM

por que código de front é tão feio?

Page 21: JS limpo e testável com Segregated DOM

jQuery resolve

Page 22: JS limpo e testável com Segregated DOM

jQuery resolve|

front como layout

Page 23: JS limpo e testável com Segregated DOM

jQuery resolve|

front como layout|

desorganização

Page 24: JS limpo e testável com Segregated DOM

js é feio e bobo

*código de verdade, em produção, do projeto que peguei pra aprender js, 4 anos atrás

Page 25: JS limpo e testável com Segregated DOM

projetos em geral

jQuery

DOM

$(…)

$(…)

$(…) $(…)

$(…) $(…)

manipulação caos

Page 26: JS limpo e testável com Segregated DOM

DOM

Page 27: JS limpo e testável com Segregated DOM

DOMé horrível

Page 28: JS limpo e testável com Segregated DOM

é o melhor que temosDOM

Page 29: JS limpo e testável com Segregated DOM

frameworks!

Page 30: JS limpo e testável com Segregated DOM

frameworks!

Page 31: JS limpo e testável com Segregated DOM

http://www.commitstrip.com/en/2015/09/16/how-to-choose-the-right-javascript-framework/

Page 32: JS limpo e testável com Segregated DOM

frameworks são ótimos…

Page 33: JS limpo e testável com Segregated DOM

frameworks são ótimos…

…mas nos limitam

Page 34: JS limpo e testável com Segregated DOM

como que faz isso aqui mesmo?

Page 35: JS limpo e testável com Segregated DOM

se faz assi…

Page 36: JS limpo e testável com Segregated DOM

esse negócio de JS não funciona não!

Page 37: JS limpo e testável com Segregated DOM
Page 38: JS limpo e testável com Segregated DOM
Page 39: JS limpo e testável com Segregated DOM

o desânimo de trabalhar com JS no front não vem do

desconhecimento de técnicas de programação, mas da

inadequação das ferramentas que se tem em relação aos

desafios propostos

Page 40: JS limpo e testável com Segregated DOM

ok, entendi o problema

Page 41: JS limpo e testável com Segregated DOM

#comofas

Page 42: JS limpo e testável com Segregated DOM

front como tentativa e erro

Page 43: JS limpo e testável com Segregated DOM

tentativa e erro

+ erros- acertos

+ bugs

Page 44: JS limpo e testável com Segregated DOM

resolução:

testes de JS!

Page 45: JS limpo e testável com Segregated DOM

resolução:

testes de JS!são um saco!

Page 46: JS limpo e testável com Segregated DOM

DOM(você de novo?)

Page 47: JS limpo e testável com Segregated DOM
Page 48: JS limpo e testável com Segregated DOM

isolar o DOM

Page 49: JS limpo e testável com Segregated DOM

isolar o DOM

encapsular o DOM

Page 50: JS limpo e testável com Segregated DOM

isolar o DOM

encapsular o DOM

segregar o DOM

Page 51: JS limpo e testável com Segregated DOM

Segregated DOM

Page 52: JS limpo e testável com Segregated DOM

lembram disso?

jQuery

DOM

$(…)

$(…)

$(…) $(…)

$(…) $(…)

manipulação caos

Page 53: JS limpo e testável com Segregated DOM

que tal isso?

presentation layer

DOM

código da aplicação

intenção

manipulação eventos do DOM

eventos do domínio

Page 54: JS limpo e testável com Segregated DOM

o que você prefere?

$('#id_name, #id_message, #id_email’)

ou

$form.getRequiredFields()

Page 55: JS limpo e testável com Segregated DOM

isso nos permite declarar intenção

$form.isValid()

Page 56: JS limpo e testável com Segregated DOM

boa idéia, mas tá meio abstrato

Page 57: JS limpo e testável com Segregated DOM

me dá um dia. vou fazer um protótipo

Page 58: JS limpo e testável com Segregated DOM

primeiro passo:

tirar o html do js e o js do html

Page 59: JS limpo e testável com Segregated DOM
Page 60: JS limpo e testável com Segregated DOM

BRINKS!

Page 61: JS limpo e testável com Segregated DOM
Page 62: JS limpo e testável com Segregated DOM
Page 63: JS limpo e testável com Segregated DOM

segundo passo:

criar uma camada de apresentação

(presentation layer)

Page 64: JS limpo e testável com Segregated DOM

prezLayer.js

Page 65: JS limpo e testável com Segregated DOM

prezLayer.js

Page 66: JS limpo e testável com Segregated DOM

antes: (contactForm.js)

Page 67: JS limpo e testável com Segregated DOM

depois: (contactForm.js)

e essa é a única chamada no $(document).ready()

Page 68: JS limpo e testável com Segregated DOM

terceiro passo:

testes.muitos testes.

Page 69: JS limpo e testável com Segregated DOM

contactForm.js

Page 70: JS limpo e testável com Segregated DOM

contactForm.test.js

Page 71: JS limpo e testável com Segregated DOM

contactForm.test.js

Page 72: JS limpo e testável com Segregated DOM

contactForm.test.js

Page 73: JS limpo e testável com Segregated DOM

contactForm.test.js

Page 74: JS limpo e testável com Segregated DOM

contactForm.test.js

Page 75: JS limpo e testável com Segregated DOM

e com testes, podemos pensar em

refatorações…

Page 76: JS limpo e testável com Segregated DOM

antes: contactForm.js

Page 77: JS limpo e testável com Segregated DOM

antes: contactForm.js

Page 78: JS limpo e testável com Segregated DOM

contactForm.js prezLayer.js

Page 79: JS limpo e testável com Segregated DOM

quarto passo:???+

profit

Page 80: JS limpo e testável com Segregated DOM

viu? agora tá tudo testado e mais

bonitinho!

Page 81: JS limpo e testável com Segregated DOM

tá mesmo, mas tá muito grande

e confuso isso aqui

Page 82: JS limpo e testável com Segregated DOM

esse negócio de JS não

funciona não

Page 83: JS limpo e testável com Segregated DOM

Page 84: JS limpo e testável com Segregated DOM

ok.me dá mais um

dia!

Page 85: JS limpo e testável com Segregated DOM

problemas

Page 86: JS limpo e testável com Segregated DOM

1 - muita redundância

Page 87: JS limpo e testável com Segregated DOM

prezLayer.js

Page 88: JS limpo e testável com Segregated DOM

2 - prezLayergigante

Page 89: JS limpo e testável com Segregated DOM

prezLayer.js

Page 90: JS limpo e testável com Segregated DOM

contactForm.js

Page 91: JS limpo e testável com Segregated DOM

3 - confusão entre o que deve

estar na prezLayer

Page 92: JS limpo e testável com Segregated DOM

prezLayer.js

Page 93: JS limpo e testável com Segregated DOM

conclusões

Page 94: JS limpo e testável com Segregated DOM

o que importa é a app/página, não a

prezLayer…

Page 95: JS limpo e testável com Segregated DOM

…porque a app pode ser

decomposta!

Page 96: JS limpo e testável com Segregated DOM

contactForm.js services.js

Page 97: JS limpo e testável com Segregated DOM

respeito aos contratos de API

Page 98: JS limpo e testável com Segregated DOM

app.js

Page 99: JS limpo e testável com Segregated DOM

o que importa é que o elemento tenha um método

append que cumpre seu papel…

Page 100: JS limpo e testável com Segregated DOM

não importa o framework,

podemos usá-lo desde que ele

esteja abstraído

Page 101: JS limpo e testável com Segregated DOM

com essa simplificação ficou fácil…

Page 102: JS limpo e testável com Segregated DOM

definir responsabilidades

Page 103: JS limpo e testável com Segregated DOM

events.js

Page 104: JS limpo e testável com Segregated DOM

services.js

Page 105: JS limpo e testável com Segregated DOM

prezLayer.js

Page 106: JS limpo e testável com Segregated DOM

app.js

Page 107: JS limpo e testável com Segregated DOM

organizar arquivos e diretórios

Page 108: JS limpo e testável com Segregated DOM
Page 109: JS limpo e testável com Segregated DOM

manter o código a longo prazo

Page 110: JS limpo e testável com Segregated DOM

e agora? dá uma olhada aí

Page 111: JS limpo e testável com Segregated DOM

Page 112: JS limpo e testável com Segregated DOM

nossa, que tá bonito demais

isso aqui, hein

Page 113: JS limpo e testável com Segregated DOM

dá pra entender tudo, tudo testado…

Page 114: JS limpo e testável com Segregated DOM

e sem aquelas pirações de

angular, react

Page 115: JS limpo e testável com Segregated DOM

mas dá pra acoplar, se quiser. só respeitar a API e mockar os caras

Page 116: JS limpo e testável com Segregated DOM

dá até pra brincar com require… mas ia ser demais

Page 117: JS limpo e testável com Segregated DOM

recapitulando

Page 118: JS limpo e testável com Segregated DOM

adeus ao código macarrônico

Page 119: JS limpo e testável com Segregated DOM

tudo testado

TDD + BDD

Page 120: JS limpo e testável com Segregated DOM

DOM segregado

ids, classes e seletores

centralizados

Page 121: JS limpo e testável com Segregated DOM

limpolegível

fácil de manter

Page 122: JS limpo e testável com Segregated DOM

DRY

Page 123: JS limpo e testável com Segregated DOM

pitadas de DDD e MVC

Page 124: JS limpo e testável com Segregated DOM

liberdade de implementação

===use quanto quiser os frameworks, da forma

que quiser

Page 125: JS limpo e testável com Segregated DOM

e o melhor…

Page 126: JS limpo e testável com Segregated DOM
Page 127: JS limpo e testável com Segregated DOM

agora sou um front foda! sei fazer

tudo!

Page 128: JS limpo e testável com Segregated DOM

ah, é, é? então coloca essa div no lugar que

eu quero ver!

Page 129: JS limpo e testável com Segregated DOM

Page 130: JS limpo e testável com Segregated DOM
Page 131: JS limpo e testável com Segregated DOM

Page 132: JS limpo e testável com Segregated DOM

me dá mais dois dias.

Page 133: JS limpo e testável com Segregated DOM

é isso aí!

Luciano [email protected]

fb: lucianoratamero twitter: @lucianoratamero

Page 134: JS limpo e testável com Segregated DOM

quer saber mais?http://martinfowler.com/bliki/

SegregatedDOM.html

http://radar.oreilly.com/2014/01/keeping-jquery-in-check.html

http://lucianoratamero.github.io/