Share
Embed
Código Limpo
ou como escrever código que te faça ter orgulho e não faça seu colega querer te bater
Tarsis Azevedo
August 29, 2015
Transcript
57 slides
-
Código Limpo Ou como escrever um código que te dê
orgulho e não faça seus amigos terem ódio de você. -
Código feio if variavel is True: print 'sou foda' elif
variavel is False: print "dig din" else: print "duvido chegar aqui" -
Código feio $('a').click(function () { var id = ''; var
id2 = ''; id = $(this).attr('id'); id2 = $(this).attr('id'); if (id == "search11") { // do something } if (id2 == "search1") { // do something } }); -
Código feio groupLayout_2.setVerticalGroup( groupLayout_2.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout_2.createSequentialGroup() .addContainerGap() .addGroup(groupLayout_2.createParallelGroup(Alignment.BASELINE) .addComponent(txtUserIn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE) .addComponent(txtUserFilename, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(groupLayout_2.createParallelGroup(Alignment.BASELINE) .addComponent(txtPassIn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(txtPassFilename, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(groupLayout_2.createParallelGroup(Alignment.BASELINE) .addComponent(txtSuccsessOut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(txtSuccsessFile, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(groupLayout_2.createParallelGroup(Alignment.BASELINE) .addComponent(CrackedAccounts, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(txtNumberOfCracked, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(9) .addComponent(btnCrack, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE) .addContainerGap(47, Short.MAX_VALUE)) ); -
Código feio <?php function register() { if (!empty($_POST)) { $msg
= ''; if ($_POST['user_name']) { if ($_POST['user_password_new']) { if ($_POST['user_password_new'] === $_POST['user_password_repeat']) { if (strlen($_POST['user_password_new']) > 5) { if (strlen($_POST['user_name']) < 65 && strlen($_POST['user_name']) > 1) { if (preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name'])) { $user = read_user($_POST['user_name']); if (!isset($user['user_name'])) { if ($_POST['user_email']) { if (strlen($_POST['user_email']) < 65) { if (filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) { create_user(); $_SESSION['msg'] = 'You are now registered so please login'; header('Location: ' . $_SERVER['PHP_SELF']); exit(); } else $msg = 'You must provide a valid email address'; } else $msg = 'Email must be less than 64 characters'; } else $msg = 'Email cannot be empty'; } else $msg = 'Username already exists'; } else $msg = 'Username must be only a-z, A-Z, 0-9'; } else $msg = 'Username must be between 2 and 64 characters'; } else $msg = 'Password must be at least 6 characters'; } else $msg = 'Passwords do not match'; } else $msg = 'Empty Password'; } else $msg = 'Empty Username'; $_SESSION['msg'] = $msg; } return register_form(); } -
Código feio <?php function register() { if (!empty($_POST)) { $msg
= ''; if ($_POST['user_name']) { if ($_POST['user_password_new']) { if ($_POST['user_password_new'] === $_POST['user_password_repeat']) { if (strlen($_POST['user_password_new']) > 5) { if (strlen($_POST['user_name']) < 65 && strlen($_POST['user_name']) > 1) { if (preg_match('/^[a-z\d]{2,64}$/i', $_POST['user_name'])) { $user = read_user($_POST['user_name']); if (!isset($user['user_name'])) { if ($_POST['user_email']) { if (strlen($_POST['user_email']) < 65) { if (filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) { create_user(); $_SESSION['msg'] = 'You are now registered so please login'; header('Location: ' . $_SERVER['PHP_SELF']); exit(); } else $msg = 'You must provide a valid email address'; } else $msg = 'Email must be less than 64 characters'; } else $msg = 'Email cannot be empty'; } else $msg = 'Username already exists'; } else $msg = 'Username must be only a-z, A-Z, 0-9'; } else $msg = 'Username must be between 2 and 64 characters'; } else $msg = 'Password must be at least 6 characters'; } else $msg = 'Passwords do not match'; } else $msg = 'Empty Password'; } else $msg = 'Empty Username'; $_SESSION['msg'] = $msg; } return register_form(); } -
TDD Sim, às vezes. Mas te ajuda a ter confiança
no seu código -
Código limpo def caLCulA(pAramEtro, x, W): if pAramEtro != x
and pAramEtro != W and x != W: if pAramEtro + x + W <= 180: return True return False -
Código limpo def caLCulA(pAramEtro, x, W): if pAramEtro != x
and pAramEtro != W and x != W: if pAramEtro + x + W <= 180: return True return False -
Código limpo def eh_escaleno(angulo_1, angulo_2, angulo_3): if angulo_1 != angulo_2
and angulo_1 != angulo_3 and angulo_2 != angulo_3: if angulo_1 + angulo_2 + angulo_3 <= 180: return True return False -
Código limpo def eh_escaleno(angulo_1, angulo_2, angulo_3): if angulo_1 != angulo_2
and angulo_1 != angulo_3 and angulo_2 != angulo_3: if angulo_1 + angulo_2 + angulo_3 <= 180: return True return False -
Código limpo def eh_escaleno(angulos): if len(set(angulos)) == 3: if sum(angulos)
<= 180: return True return False -
Código limpo class Cafe(object): preco = 1.0 def calcula_preco(self): if
self.tipo == 'com leite': return self.preco + 0.2 if self.tipo == 'com creme': return self.preco + 0.4 if self.tipo == 'com leite e creme': return self.preco + 0.6 return self.preco -
Código limpo class Cafe(object): preco = 1.0 def calcula_preco(self): if
self.tipo == 'com leite': return self.preco + 0.2 if self.tipo == 'com creme': return self.preco + 0.4 if self.tipo == 'com leite e creme': return self.preco + 0.6 if self.tipo == 'com doce de leite': return self.preco + 0.6 if self.tipo == 'com doce de leite e leite e creme': return self.preco + 1.2 return self.preco -
Código limpo class Adicional(object): preco = None def __init__(self, cafe):
self.cafe = cafe def calcula_preco(self): raise NotImplementedError class Leite(Adicional): preco = 0.2 def calcula_preco(self): self.cafe.preco += self.preco return self.cafe.preco -
Código limpo class Creme(Adicional): preco = 0.4 def calcula_preco(self): self.cafe.preco
+= self.preco return self.cafe.preco class DoceDeLeite(Adicional): preco = 0.6 def calcula_preco(self): self.cafe.preco += self.preco return self.cafe.preco class Cafe(object): preco = 1.0 -
Código limpo cafe = Cafe() cafe_com_leite = Leite(cafe) cafe_com_leite.calcula_preco() cafe_com_leite_e_creme
= Creme(cafe_com_leite.cafe) cafe_com_leite_e_creme.calcula_preco() cafe_com_leite_e_creme_e_doce_de_leite = DoceDeLeite(cafe_com_leite_e_creme.cafe) cafe_com_leite_e_creme_e_doce_de_leite.calcula_preco() -
Design Pattern alguém já teve esse problema mas é só
um guia, adapte a sua realidade -
Problemas Projeto gigante - 300k LOC 30% de duplicação de
codigo Testes lentos e instáveis Ignorando PEP8 -
Resolvendo “Cachorro com muitos donos morre de fome” Apagando testes
inúteis Estabilizando e otimizando os testes CI para cada time