Smells @muc_rubyshift 06/2013

47
6/4/13 Fighting Code That Smells Anna Bulenkova JetBrains Software Developer

Transcript of Smells @muc_rubyshift 06/2013

Page 1: Smells @muc_rubyshift 06/2013

6/4/13

Fighting Code That Smells

Anna BulenkovaJetBrains Software Developer

Page 2: Smells @muc_rubyshift 06/2013

What’s that smell?

Page 3: Smells @muc_rubyshift 06/2013

WARNINGKEEP CHILDREN AWAYFROM THIS CONTENT

Page 4: Smells @muc_rubyshift 06/2013
Page 5: Smells @muc_rubyshift 06/2013
Page 6: Smells @muc_rubyshift 06/2013
Page 7: Smells @muc_rubyshift 06/2013
Page 8: Smells @muc_rubyshift 06/2013

Why?

Page 9: Smells @muc_rubyshift 06/2013

Experience

Page 10: Smells @muc_rubyshift 06/2013

Interruptions

Page 11: Smells @muc_rubyshift 06/2013

Tiredness

Page 12: Smells @muc_rubyshift 06/2013

Who cares...

Page 13: Smells @muc_rubyshift 06/2013

Other Issues

Page 14: Smells @muc_rubyshift 06/2013

What happens?

Page 15: Smells @muc_rubyshift 06/2013

• undefined local variable or method `call_me' for main:Object (NameError)

warning: Object#id will be deprecated; use Object#object_id

• warning: Object#id will be deprecated; use Object#object_id

•Dead code

•Copy/Paste

•Complexity

•code_styleAnd_FWpatternViolations

Page 16: Smells @muc_rubyshift 06/2013

• undefined local variable or method `call_me' for main:Object (NameError)

warning: Object#id will be deprecated; use Object#object_id

• warning: Object#id will be deprecated; use Object#object_id

•Dead code

•Copy/Paste

•Complexity

•code_styleAnd_FWpatternViolations

Page 17: Smells @muc_rubyshift 06/2013

• undefined local variable or method `call_me' for main:Object (NameError)

warning: Object#id will be deprecated; use Object#object_id

• warning: Object#id will be deprecated; use Object#object_id

•Dead code

•Copy/Paste

•Complexity

•code_styleAnd_FWpatternViolations

Page 18: Smells @muc_rubyshift 06/2013

• undefined local variable or method `call_me' for main:Object (NameError)

warning: Object#id will be deprecated; use Object#object_id

• warning: Object#id will be deprecated; use Object#object_id

•Dead code

•Copy/Paste

•Complexity

•code_styleAnd_FWpatternViolations

Page 19: Smells @muc_rubyshift 06/2013

• undefined local variable or method `call_me' for main:Object (NameError)

warning: Object#id will be deprecated; use Object#object_id

• warning: Object#id will be deprecated; use Object#object_id

•Dead code

•Copy/Paste

•Complexity

•code_styleAnd_FWpatternViolations

Page 20: Smells @muc_rubyshift 06/2013

• undefined local variable or method `call_me' for main:Object (NameError)

warning: Object#id will be deprecated; use Object#object_id

• warning: Object#id will be deprecated; use Object#object_id

•Dead code

•Copy/Paste

•Complexity

•code_styleAnd_FWpatternViolations

Page 21: Smells @muc_rubyshift 06/2013

How to fight?

Page 22: Smells @muc_rubyshift 06/2013

•Learn

•Re-read

•Ask for review

•Pair programming

•Use tools

Page 23: Smells @muc_rubyshift 06/2013

Code quality tools

•Static

•Runtime

Page 24: Smells @muc_rubyshift 06/2013

•No code run

•But tool runs

•No side effects

•But no on-the-fly change

Static tools

false positevebad alalyzing meta-programming

false positevebad alalyzing meta-programming

Page 25: Smells @muc_rubyshift 06/2013

Reek

•Control Couple

•Data Clump

•Feature Envy

•Large Class/Method

•Long Parameters List

•Simulated Polymorphism

Page 26: Smells @muc_rubyshift 06/2013

Control Couple

Page 27: Smells @muc_rubyshift 06/2013

Data Clump

Page 28: Smells @muc_rubyshift 06/2013

Feature Envy

Page 29: Smells @muc_rubyshift 06/2013

Large Class/Method

OneThird

Page 30: Smells @muc_rubyshift 06/2013

Long Parameter List

QuickTime™ and aH.264 decompressor

are needed to see this picture.

Page 31: Smells @muc_rubyshift 06/2013

Simulated Polymorphism

•instance_of?

•kind_of?

•is_a?

•===

•Especially with case

Page 32: Smells @muc_rubyshift 06/2013

Flog

•ABC Metric

•|ABC| = sqrt((A*A)+(B*B)+(C*C))

Page 33: Smells @muc_rubyshift 06/2013

ABC Guide Lines0-10 = Awesome

•11-20 = Good enough

•21-40 = Might need refactoring

•41-60 = Possible to justify

•61-100 = Danger

•100-200 = Whoop, whoop, whoop

•200 + = Someone please think of the children

Page 34: Smells @muc_rubyshift 06/2013

ABC = 8.5

Page 35: Smells @muc_rubyshift 06/2013

Flay

•Duplicates

•Ignores names / literals / values

•Ignores code style

Page 36: Smells @muc_rubyshift 06/2013

Roodi•Assignment in conditionals•Case missing else

•Line count

•Names check

•Cyclomatic сomplexity

•Empty rescue body

•For to foreach

•Parameters number

подровнятьподровнять

Page 37: Smells @muc_rubyshift 06/2013

metrics_fu/metrical• Flay

• Flog

• Rails Best Practices

• Reek

• Roodi

• Dynamic tools

• Fancy HTML report

Page 38: Smells @muc_rubyshift 06/2013

Where to get?

Page 39: Smells @muc_rubyshift 06/2013

Who’s gonna clean everything for me?

Page 40: Smells @muc_rubyshift 06/2013

RubyMine

•Inspections

•Duplicates

Page 41: Smells @muc_rubyshift 06/2013

RubyMine Inspections

•Static

•On-the-fly analysis

Page 42: Smells @muc_rubyshift 06/2013
Page 43: Smells @muc_rubyshift 06/2013

Static analysis

QuickTime™ and aH.264 decompressor

are needed to see this picture.

Page 44: Smells @muc_rubyshift 06/2013

QuickTime™ and aH.264 decompressor

are needed to see this picture.

On-The-Fly Analysis

Page 45: Smells @muc_rubyshift 06/2013

Duplicates

Page 46: Smells @muc_rubyshift 06/2013

There’s no cure-all

Page 47: Smells @muc_rubyshift 06/2013

6/4/13

Thanks for watching!