PEP 8
                 OU,
POR QUE UTILIZAR PADRÕES DE CÓDIGO?
AGENDA
Quem sou eu?
Motivação
PEP8
QUEM SOU EU?
Renato Oliveira
CIn-UFPE
PUG-PE
Linux User - #532503
Js e Rails developer na E.Life
MOTIVAÇÃO
“40% ~ 80% of the lifetime cost of a piece
   of software goes to maintenance”
“Hardly any software is mainteined for its
    hole life by the original author”
                   Sun
PROBLEMA
Desenvolvi um produto há 2 anos e o cliente voltou agora
     e pediu para adicionar mais alguns módulos

                     PERGUNTA
           Você tem orgulho do seu código?
           Está fácil fazer esta manutenção?
CODE SMELL
Não é um bug
Está mais para um sintoma do código que indica um
problema maior
Indica que o código é fraco de design e que pode
atrasar o desenvolvimento ou aumentar o risco de
bugs ou falhas no futuro
Um código que não segue padrões é mais suscetível a
'smells'
MANUTENÇÃO
 QUALIDADE
 REFATORAR
ENTÃO VAMOS FALAR DE COISA BOA NÉ?
      Você já conhece a nova top therm?
E POR QUE COM
PYTHON SERIA
  DIFERENTE?
PEP 8
Escrita por Guido Van Rossum e Barry Warsaw
THE ZEN OF PYTHON
      import this
THE ZEN OF PYTHON, BY TIM PETERS
             Beautiful is better than ugly.
            Explicit is better than implicit.
            Simple is better than complex.
         Complex is better than complicated.
              Flat is better than nested.
             Sparse is better than dense.
                 Readability counts.
Special cases aren't special enough to break the rules.
          Although practicality beats purity.
          Errors should never pass silently.
              Unless explicitly silenced.
THE ZEN OF PYTHON, BY TIM PETERS
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious
                      way to do it.
   Although that way may not be obvious at first unless
                     you're Dutch.
                Now is better than never.
     Although never is often better than *right* now.
 If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good
                           idea.
 Namespaces are one honking great idea -- let's do more
                        of those!
FORMATAÇÃO DE CÓDIGO
   Identação
   Tabs ou espaços
   Comprimento da linha
   Linhas em branco
   imports
ERRADO
ipr o,ss
 mot s y




                   CERTO
ipr o
 mot s
ipr ss
 mot y




                  CERTO[2]
fo o ipr pt,mdr
 rm s mot ah ki
ERRADO
pitsm(fb 5) fb 1 ))
 rn oa i(  , i( 0




                      CERTO
pitsm(i() fb1)
 rn oafb5, i(0)
OPERADORES
           ERRADO
x1
 =0
ay1
 =<0




           CERTO
x=10
a=y<10
OPERADORES
                ERRADO
sm( =1,y=2)
 oax 0    0




                CERTO
sm(=0 y2)
 oax1, =0
ATRIBUTOS
AGM_OSAT =2
 LUACNTNE  0
agm_aivl="luasrn"
 luavrae  agm tig
_tiuod_s_nen ="ur srn"
 arbt_euoitro  ota tig


casNmeelse
 ls odDCas:
     _arbt_rvd_ecas =Tu
      _tiuopiaod_lse re

    dfagm_uco)
     e luafna(:
         #cdg d ba
           óio e o

    dfOtauco)
     e urFna(:
         #cdg tnoau
           óio es qi
RECOMENDAÇÕES
i xi Nn:
 f  s oe
     ..
      .

i z
 f :
       ..
        .



i tp(b)i tp()
 f yeoj s ye1:
     #erd
       rao

i iisac(b,it:
 f sntneoj n)
     #cro
       et
RECOMENDAÇÕES
i fo:]= 'a'
 f o[3 = br:
     #erd
       rao

i frsatwt(br)
 f o.trsih'a':
     #cro
       et
MAIS EM
http://www.python.org/dev/peps/pep-0008/
DÚVIDAS?
OBRIGADO
    @_renatoOliveira
 renato@labcodes.com.br
github.com/renatooliveira
Pep 8

Pep 8

  • 1.
    PEP 8 OU, POR QUE UTILIZAR PADRÕES DE CÓDIGO?
  • 2.
  • 3.
    QUEM SOU EU? RenatoOliveira CIn-UFPE PUG-PE Linux User - #532503 Js e Rails developer na E.Life
  • 4.
    MOTIVAÇÃO “40% ~ 80%of the lifetime cost of a piece of software goes to maintenance” “Hardly any software is mainteined for its hole life by the original author” Sun
  • 5.
    PROBLEMA Desenvolvi um produtohá 2 anos e o cliente voltou agora e pediu para adicionar mais alguns módulos PERGUNTA Você tem orgulho do seu código? Está fácil fazer esta manutenção?
  • 6.
    CODE SMELL Não éum bug Está mais para um sintoma do código que indica um problema maior Indica que o código é fraco de design e que pode atrasar o desenvolvimento ou aumentar o risco de bugs ou falhas no futuro Um código que não segue padrões é mais suscetível a 'smells'
  • 7.
  • 8.
    ENTÃO VAMOS FALARDE COISA BOA NÉ? Você já conhece a nova top therm?
  • 9.
    E POR QUECOM PYTHON SERIA DIFERENTE?
  • 10.
    PEP 8 Escrita porGuido Van Rossum e Barry Warsaw
  • 11.
    THE ZEN OFPYTHON import this
  • 12.
    THE ZEN OFPYTHON, BY TIM PETERS Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced.
  • 13.
    THE ZEN OFPYTHON, BY TIM PETERS In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
  • 15.
    FORMATAÇÃO DE CÓDIGO Identação Tabs ou espaços Comprimento da linha Linhas em branco imports
  • 16.
    ERRADO ipr o,ss mots y CERTO ipr o mot s ipr ss mot y CERTO[2] fo o ipr pt,mdr rm s mot ah ki
  • 17.
    ERRADO pitsm(fb 5) fb1 )) rn oa i( , i( 0 CERTO pitsm(i() fb1) rn oafb5, i(0)
  • 18.
    OPERADORES ERRADO x1 =0 ay1 =<0 CERTO x=10 a=y<10
  • 19.
    OPERADORES ERRADO sm( =1,y=2) oax 0 0 CERTO sm(=0 y2) oax1, =0
  • 20.
    ATRIBUTOS AGM_OSAT =2 LUACNTNE 0 agm_aivl="luasrn" luavrae agm tig _tiuod_s_nen ="ur srn" arbt_euoitro ota tig casNmeelse ls odDCas: _arbt_rvd_ecas =Tu _tiuopiaod_lse re dfagm_uco) e luafna(: #cdg d ba óio e o dfOtauco) e urFna(: #cdg tnoau óio es qi
  • 21.
    RECOMENDAÇÕES i xi Nn: f s oe .. . i z f : .. . i tp(b)i tp() f yeoj s ye1: #erd rao i iisac(b,it: f sntneoj n) #cro et
  • 22.
    RECOMENDAÇÕES i fo:]= 'a' f o[3 = br: #erd rao i frsatwt(br) f o.trsih'a': #cro et
  • 23.
  • 24.
  • 25.
    OBRIGADO @_renatoOliveira renato@labcodes.com.br github.com/renatooliveira