Slide 1

Slide 1 text

CODE SMELLS CODE SMELLS 2018-09-26 @ 7Masters Davi Marcondes Moreira - @devdrops Photo by on Braydon Anderson Unsplash

Slide 2

Slide 2 text

$ ~ $ ~ WHOAMI WHOAMI - Davi Marcondes Moreira - Software Developer @ Pagar.me - @devdrops - PHP, JavaScript, Kotlin e o que mais vier \,,/ - Mending > Making

Slide 3

Slide 3 text

Photo by on Steve Harvey Unsplash

Slide 4

Slide 4 text

Photo by on Daniil Silantev Unsplash

Slide 5

Slide 5 text

"I have an occasion to ask people if they've heard of code smells, and everyone say yes. (...) Then I ask them to list five, and no one can." Sandi Metz

Slide 6

Slide 6 text

A LISTA É GRANDE A LISTA É GRANDE - Alternative classes with different interfaces - Comments - Data Class - Data Clumps - Divergent change - Duplicated code - Feature envy - Inappropriate Intimacy - Incomplete library client - Large class - Lazy class - Long method - Long parameter list - Message chain - Middle man - Parallel inheritance hierachies - Primitive obsession - Refused bequest - Shotgun surgery - Speculative generality - Switch statements - Temporary fields

Slide 7

Slide 7 text

3 3 BONS BONS EXEMPLOS EXEMPLOS

Slide 8

Slide 8 text

MÉTODOS LONGOS MÉTODOS LONGOS

Slide 9

Slide 9 text

public function submit() { $this->load->model('checkout/order'); $this->load->model('account/customer'); $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); $customer = $this->model_account_customer->getCustomer($order_info['customer_id']); $discountPercentage = $this->config->get('checkout_boleto_discount_percentage'); $discountAmount = ($this->cart->getSubtotal() * $discountPercentage) / 100; $json=array(); $json['amount'] = str_replace(array(".", ","), array("", ""), number_format($order_info['total'], 2 $json['button_text'] = $this->config->get('checkout_texto_botao') ? $this->config->get('checkout_tex $json['boleto_discount_amount'] = number_format($discountAmount, 2, '', ''); $json['button_class'] = $this->config->get('checkout_button_css_class'); $payment_methods = $this->config->get('checkout_payment_methods'); if (count($payment_methods) == 1) { $json['payment_methods'] = $payment_methods[0]; } else { $json['payment_methods'] = $payment_methods[0] . ',' . $payment_methods[1]; } $card_brands = ''; $card_brands_array = $this->config->get('checkout_card_brands'); foreach ($card_brands_array as $card_brand) { if (reset($card_brands_array) == $card_brand) { $card_brands .= $card_brand; } else { $card_brands .= ',' . $card_brand; } } $json['card_brands'] = $card_brands; /* Máximo de parcelas */ $max_installment = $this->config->get('checkout_max_installments'); $order_total = $order_info['total']; $max_installment_value = $this->config->get('checkout_max_installment_value'); if($max_installment_value) {

Slide 10

Slide 10 text

MIDDLE MAN MIDDLE MAN Photo by on rawpixel Unsplash

Slide 11

Slide 11 text

class User { private $phone; public function getPhone() { return $this->phone; } public function getPhoneDdi() { return $this->phone->getDdi(); } public function getPhoneDdd() { return $this->phone->getDdd(); } public function getPhoneNumber() { return $this->phone->getNumber(); } }

Slide 12

Slide 12 text

class User { private $phone; public function getPhone() { return $this->phone; } public function getPhoneDdi() { return $this->phone->getDdi(); } public function getPhoneDdd() { return $this->phone->getDdd(); } public function getPhoneNumber() { return $this->phone->getNumber(); } }

Slide 13

Slide 13 text

AGLOMERADO DE DADOS AGLOMERADO DE DADOS

Slide 14

Slide 14 text

# Mudamos disso class PostRepository def draft(title, description, author) # code code code end def publish(title, description, author) # code code code end def update(id, title, description, author) # code code code end end

Slide 15

Slide 15 text

# Mudamos disso class PostRepository def draft(title, description, author) # code code code end def publish(title, description, author) # code code code end def update(id, title, description, author) # code code code end end

Slide 16

Slide 16 text

# Mudamos disso class PostRepository def draft(title, description, author) # code code code end def publish(title, description, author) # code code code end def update(id, title, description, author) # code code code end end # Para isso Post = Struct.new(:title, :description, :author) class PostRepository def draft(post) # code code code end def publish(post) # code code code end def update(id, post) # code code code end end

Slide 17

Slide 17 text

CONCLUSÃO CONCLUSÃO

Slide 18

Slide 18 text

Aprender code smells torna mais fácil identificar problemas no seu código.

Slide 19

Slide 19 text

REFERÊNCIAS REFERÊNCIAS - Code Refactoring: Learn Code Smells And Level Up Your Game! (https://www.youtube.com/watch?v=D4auWwMsEnY) - Smells to Refactoring Quick Reference Guide (http://www.industriallogic.com/wp- content/uploads/2005/09/smellstorefactorings.pdf)

Slide 20

Slide 20 text

MUITO OBRIGADO! :D MUITO OBRIGADO! :D @devdrops devdrops.me/about bit.ly/7masters-code-smells