Upcoming SlideShare
×

# Analisando Complexidade de Código Ruby

4,022 views

Published on

Palestra feita no evento Ruby Masters Conf edição 2011.

Published in: Technology
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
4,022
On SlideShare
0
From Embeds
0
Number of Embeds
1,811
Actions
Shares
0
25
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Analisando Complexidade de Código Ruby

1. 1. Analisando Complexidade de Código Ruby Utilizando Métricas para Encontrar Código que Fede Cássio Marques RubyMasters Conf 2011Saturday, February 26, 2011
2. 2. Cássio Marques • código + comida vegan + livros + skate + música • Taoweb • Lime • http://cassiomarques.wordpress.com • @cassiomarquesSaturday, February 26, 2011
3. 3. Porque analisar o código?Saturday, February 26, 2011
4. 4. http://www.ﬂickr.com/photos/rovingi/312078231/Saturday, February 26, 2011
5. 5. Como analisar o código?Saturday, February 26, 2011
6. 6. EstéticaSaturday, February 26, 2011
7. 7. Diﬁculdade de ManutençãoSaturday, February 26, 2011
8. 8. ChuteSaturday, February 26, 2011
9. 9. AbstraçõesSaturday, February 26, 2011
10. 10. MétricasSaturday, February 26, 2011
11. 11. Medir alguma propriedade especíﬁca do códigoSaturday, February 26, 2011
12. 12. Objetivo: Quantiﬁcar o que antes era abstratoSaturday, February 26, 2011
13. 13. Métrica ABCSaturday, February 26, 2011
14. 14. Analisa ComplexidadeSaturday, February 26, 2011
15. 15. ﬂog github.com/seattlerb/ﬂogSaturday, February 26, 2011
16. 16. Assignement Deﬁnir o valor de uma variável explicitamenteSaturday, February 26, 2011
17. 17. = += *= << /= etc... -=Saturday, February 26, 2011
18. 18. Branching Criar um novo ﬂuxo de execuçãoSaturday, February 26, 2011
19. 19. if or else rescue elsif (argh!) until case while andSaturday, February 26, 2011
20. 20. Calls Chamadas a métodosSaturday, February 26, 2011
21. 21. (Outras implementações usam Condition)Saturday, February 26, 2011
22. 22. |ABC| = sqrt((A*A)+(B*B)+(C*C))Saturday, February 26, 2011
23. 23. Mas em Ruby temos mais do que isso!Saturday, February 26, 2011
24. 24. eval & cia (pontuação alta!!!) deﬁne_method eval module_eval class_eval instance_evalSaturday, February 26, 2011
25. 25. blocos/yieldSaturday, February 26, 2011
26. 26. Introspecção instance_method method_deﬁned? private_instance_method privante_class_method public_instance_method etc...Saturday, February 26, 2011
27. 27. Hooks Module#included Module#extended Class#inherited method_deﬁned etc...Saturday, February 26, 2011
28. 28. Ou seja...Saturday, February 26, 2011
29. 29. Ou seja... METAPROGRAMAÇÃO É COMPLEXO E DIFICULTA A MANUTENÇÃO DO CÓDIGOSaturday, February 26, 2011
30. 30. Quando veriﬁcar isso no seu código?Saturday, February 26, 2011
31. 31. FrequentementeSaturday, February 26, 2011
32. 32. (já deixei para a fase ﬁnal de um projeto e o resultado foi traumático)Saturday, February 26, 2011
33. 33. Complexidade CiclomáticaSaturday, February 26, 2011
34. 34. saikuro http://saikuro.rubyforge.org/Saturday, February 26, 2011
35. 35. Mede o número de segmentos linearmente independentes na execução do códigoSaturday, February 26, 2011
36. 36. Grafo direcionado para representar os caminhos de execuçãoSaturday, February 26, 2011
37. 37. 1 def some_method(value) 2 if value % 2 == 0 3 value.times { |i| puts i } 4 else 5 while value >= 0 6 puts value 7 value -= 1 8 end 9 end 10 endSaturday, February 26, 2011
38. 38. Início value % 2 == 0 value.times { |i| ... while value >= 0 puts value puts i puts value -= 1 FimSaturday, February 26, 2011
39. 39. C = A - V + 2P C => Complexidade ciclomática A => Número de arestas V => Número de vértices P => Número de componentes conectadosSaturday, February 26, 2011
40. 40. Pode ser utilizada para deﬁnir o número de casos de teste necessários para ter total cobertura do códigoSaturday, February 26, 2011
41. 41. Métrica procedural, mas pode ser usada em código OO no escopo de métodosSaturday, February 26, 2011
42. 42. Duplicação de códigoSaturday, February 26, 2011
43. 43. ﬂay http://ruby.sadi.st/Flay.htmlSaturday, February 26, 2011
44. 44. Pesquisa por códigos SEMELHANTESSaturday, February 26, 2011
45. 45. Resultados são bons candidatos para refactoringSaturday, February 26, 2011
46. 46. Código que muda com muita frequênciaSaturday, February 26, 2011
47. 47. churn github.com/danmayer/churnSaturday, February 26, 2011
48. 48. Código que muda muito == problemasSaturday, February 26, 2011
49. 49. Indica quantidade de alterações ao longo do tempoSaturday, February 26, 2011
50. 50. Métodos Classes ArquivosSaturday, February 26, 2011
51. 51. git/mercurialSaturday, February 26, 2011
52. 52. Code SmellsSaturday, February 26, 2011
53. 53. Sintoma que indica algum problema mais sérioSaturday, February 26, 2011
54. 54. O código pode feder por vários motivosSaturday, February 26, 2011
55. 55. reek github.com/kevinrutherford/reekSaturday, February 26, 2011
56. 56. Aceita opções sobre quais smells identiﬁcar/ ignorarSaturday, February 26, 2011
57. 57. AtributosSaturday, February 26, 2011
58. 58. readers/writers (ou getters/setters) podem expor demais seus objetosSaturday, February 26, 2011
59. 59. Variáveis de ClasseSaturday, February 26, 2011
60. 60. 1 class MyClass 2 @@my_class_variable = 1 3 4 class << self 5 attr_accessor :my_other_class_variable 6 end 7 endSaturday, February 26, 2011
61. 61. Aumenta o escopo globalSaturday, February 26, 2011
62. 62. Classe LongaSaturday, February 26, 2011
63. 63. Muitas linhasSaturday, February 26, 2011
64. 64. Muitas variáveis de instânciaSaturday, February 26, 2011
65. 65. Muitos métodosSaturday, February 26, 2011
66. 66. *provavelmente* fazendo coisa demais!Saturday, February 26, 2011
67. 67. Método LongoSaturday, February 26, 2011
68. 68. Lista de Parâmetros LongaSaturday, February 26, 2011
69. 69. 1 def pay_with_credit_card(number, security_code, holder_name, expiration, value) 2 # ... 3 endSaturday, February 26, 2011
70. 70. Possíveis soluçõesSaturday, February 26, 2011
71. 71. Passar um hashSaturday, February 26, 2011
72. 72. Encapsular em um objetoSaturday, February 26, 2011
73. 73. Baixa Coesão Feature EnvySaturday, February 26, 2011
74. 74. 1 class Cart 2 def price 3 @item.price + @item.tax 4 end 5 endSaturday, February 26, 2011
75. 75. Baixa Coesão Utility FunctionSaturday, February 26, 2011
76. 76. Método que não depende do estado do objetoSaturday, February 26, 2011
77. 77. 1 class Oven 2 def formatted_temperature(sensor) 3 "#{sensor.temperature} #{sensor.unit}" 4 end 5 endSaturday, February 26, 2011
78. 78. Polimorﬁsmo SimuladoSaturday, February 26, 2011
79. 79. 1 def some_method(value) 2 if value > 10 3 if value < 20 4 if value != 15 5 puts "duh" 6 else 7 puts "meh" 8 end 9 end 10 end 11 endSaturday, February 26, 2011
80. 80. switch também :) (o reek ainda não identiﬁca)Saturday, February 26, 2011
81. 81. Uncommunicative NamesSaturday, February 26, 2011
82. 82. Nomes não reﬂetem a intenção do códigoSaturday, February 26, 2011
83. 83. 1 class SomeClass 2 def some_method 3 # ... 4 end 5 6 def some_method_2 7 # ... 8 end 9 10 def another_method 11 a = 10 12 aLocalVariable = 25 13 end 14 endSaturday, February 26, 2011
84. 84. Pode ser conﬁgurado com uma lista de regexps default [/^.[0-9]*\$/]Saturday, February 26, 2011
85. 85. Cobertura de TesteSaturday, February 26, 2011
86. 86. != “bem testado”Saturday, February 26, 2011
87. 87. rcov http://eigenclass.org/hiki.rb?rcovSaturday, February 26, 2011
88. 88. Pode indicar trechos do código que foram negligenciadosSaturday, February 26, 2011
89. 89. Too Simple To BreakSaturday, February 26, 2011
91. 91. Saturday, February 26, 2011
92. 92. Saturday, February 26, 2011
93. 93. Outros smells e erros de estiloSaturday, February 26, 2011
94. 94. roodi github.com/martinjandrews/roodiSaturday, February 26, 2011
95. 95. Atribuição de valor dentro de uma estrutura condicionalSaturday, February 26, 2011
96. 96. 1 # Deveria ser foo == 100 2 if foo = 100 3 # ... 4 endSaturday, February 26, 2011
97. 97. 1 # Falso positivo... 2 def some_method(value) 3 if @value = value 4 # ... 5 end 6 endSaturday, February 26, 2011
98. 98. case sem cláusula elseSaturday, February 26, 2011
99. 99. 1 def some_method(value) 2 case value 3 when 1..10 4 puts "Entre 1 e 10" 5 when 11..20 6 puts "Entre 11 e 20" 7 end 8 endSaturday, February 26, 2011
100. 100. Classes com muitas linhas de código (por padrão limita em 300)Saturday, February 26, 2011
101. 101. Padrão para nome de classes e módulos (veriﬁca se o nome das classes e módulos está em CamelCase)Saturday, February 26, 2011
102. 102. cláusulas rescue vaziasSaturday, February 26, 2011
103. 103. 1 def calculate_factor(number) 2 begin 3 SOME_CONSTANT / number 4 rescue 5 6 end 7 endSaturday, February 26, 2011
104. 104. 1 def calculate_factor(number) 2 begin 3 SOME_CONSTANT / number 4 rescue => e 5 # Tratar a exceção de alguma forma... 6 end 7 endSaturday, February 26, 2011
105. 105. Uso de loops com forSaturday, February 26, 2011
106. 106. 1 def print_list(list) 2 for value in list do 3 puts value 4 end 5 endSaturday, February 26, 2011
107. 107. 1 def print_list(list) 2 list.each {|value| puts value } 3 endSaturday, February 26, 2011
108. 108. Métodos com muitas linhas de código (por padrão limita em 20)Saturday, February 26, 2011
109. 109. Padrão para nomes de métodos (veriﬁca se os nomes dos métodos estão snake_case)Saturday, February 26, 2011
110. 110. rails_best_practices github.com/ﬂyerhzm/rails_best_practicesSaturday, February 26, 2011
111. 111. Ok...Saturday, February 26, 2011
112. 112. Mas é muita coisa para gerenciar!Saturday, February 26, 2011
113. 113. metric_fu http://metric-fu.rubyforge.org/Saturday, February 26, 2011
114. 114. saikuro + ﬂog + ﬂay + rcov + reek + roodi + churn + rails_best_practicesSaturday, February 26, 2011
115. 115. rake metrics:allSaturday, February 26, 2011
116. 116. Integração ContínuaSaturday, February 26, 2011
117. 117. Métricas ao longo do tempoSaturday, February 26, 2011
118. 118. Relatórios!Saturday, February 26, 2011
119. 119. Saturday, February 26, 2011
120. 120. Saturday, February 26, 2011
121. 121. Saturday, February 26, 2011
122. 122. Saturday, February 26, 2011
123. 123. Problema:Saturday, February 26, 2011
124. 124. O metric_fu acaba virando uma dependência do seu projetoSaturday, February 26, 2011
125. 125. metrical github.com/iain/metricalSaturday, February 26, 2011
126. 126. \$ gem install metrical \$ cd /path/of/your/project \$ metricalSaturday, February 26, 2011
127. 127. Dúvidas?Saturday, February 26, 2011
128. 128. Obrigado!Saturday, February 26, 2011