refactoring code smell

23
Refactoring Does your code smell? by Martin Tschischauskas

Upload: martintsch

Post on 01-Sep-2014

1.559 views

Category:

Technology


1 download

DESCRIPTION

A small presentation on how one could start refactoring ruby code. - check the hotspots - check your tests - make it nice -don't forget security

TRANSCRIPT

Page 1: Refactoring code smell

Refactoring Does your code smell?

by Martin Tschischauskas

Page 2: Refactoring code smell

Refactoring – Does your code smell?

Author• http://twitter.com/MartynasC• https://www.xing.com/profile/

Martin_Tschischauskas

Martin Tschischauskas

Martin Tschischauskas

• Diplom Technische Informatik / Computer Engineering

• Ruby on Rails Developer• XING AG• Jobs Team

Page 3: Refactoring code smell

Martin Tschischauskas

gems

ConclusionTools

Motivation

Refactoring – Does your code smell?

Page 4: Refactoring code smell

Does your code smell?

Martin Tschischauskas

Motivation

Refactoring – Does your code smell?

Page 5: Refactoring code smell

Situation at work

Martin Tschischauskas

Motivation

Refactoring – Does your code smell?

Page 6: Refactoring code smell

Lots of articles / books

Martin Tschischauskas

Motivation

Refactoring – Does your code smell?

Page 7: Refactoring code smell

Refactoring – Does your code smell?

But how to start?

Martin Tschischauskas

Motivation

Page 8: Refactoring code smell

Refactoring – Does your code smell?

Code Sadist

Martin Tschischauskas

gems

Page 9: Refactoring code smell

Martin Tschischauskas

flog

gem install flog

find yourapp -name \*.rb | xargs flog

Refactoring – Does your code smell?

Page 10: Refactoring code smell

Martin Tschischauskas

flog

• ABC metrics• computed by counting the number of assignments, branches and conditions

for a section of code• Assignment - an explicit transfer of data into a variable• Branch - an explicit forward program branch out of scope - a function

call, class method call, or new operator• Condition - a logical/Boolean test

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

• http://ruby.sadi.st/Flog.html

Refactoring – Does your code smell?

Page 11: Refactoring code smell

Martin Tschischauskas

flay

gem install flay

find yourapp -name \*.rb | xargs flay

Refactoring – Does your code smell?

Page 12: Refactoring code smell

Martin Tschischauskas

flay

• DRY – Don’t Repeat yourself• Analyzes ruby code for structural similarities

• Differences in literal values,• Names,• Whitespace,• Programming style• Are ignored

• http://ruby.sadi.st/Flay.html

Refactoring – Does your code smell?

Page 13: Refactoring code smell

Martin Tschischauskas

heckle

gem install heckle

heckle Class method –t path_to_your_test –T timeout_in_seconds

Refactoring – Does your code smell?

Page 14: Refactoring code smell

Martin Tschischauskas

heckle

• Mutation tester• Changes code and checks if tests fail• If tests still succeed

• Code is not covered• Or code does nothing

• http://ruby.sadi.st/Heckle.html

Refactoring – Does your code smell?

Page 15: Refactoring code smell

Martin Tschischauskas

reek

gem install reek

find yourapp -name \*.rb | xargs reek

Refactoring – Does your code smell?

Page 16: Refactoring code smell

Martin Tschischauskas

reek

• Detects code smell• Long Method• Large Class• Feature Envy (using methods of different class excessively)• Uncommunicative Name• Long Parameter List• Nested Iterators• https://github.com/troessner/reek/wiki/Code-Smells

Refactoring – Does your code smell?

Page 17: Refactoring code smell

Martin Tschischauskas

brakeman

gem install brakeman

brakeman yourapp

Refactoring – Does your code smell?

Page 18: Refactoring code smell

Martin Tschischauskas

brakeman

• Detects possible security issues• No configuration is needed• Finds possible

• XSS• SQL Injection• …

• http://brakemanscanner.org/

Refactoring – Does your code smell?

Page 19: Refactoring code smell

Martin Tschischauskas

rails_best_practises

gem install rails_best_practises

rails_best_practices -f html

Refactoring – Does your code smell?

Page 20: Refactoring code smell

Martin Tschischauskas

rails_best_practises

• Tries to check code for rails best practises• Move code from Controller to Model• RESTful Conventions• Model• Mailer• Migration • …• http://rails-bestpractices.com/

Refactoring – Does your code smell?

Page 21: Refactoring code smell

Refactoring – Does your code smell

https://codeclimate.com/github/rails/rails

Martin Tschischauskas

Code climate

Page 22: Refactoring code smell

Martin Tschischauskas

???

Refactoring – Does your code smell?

Thank You!

Page 23: Refactoring code smell

Martin Tschischauskas

Credits

http://www.flickr.com/photos/dana_moos/5495380861/sizes/o/in/photostream/

http://www.flickr.com/photos/seatbelt67/502255276/sizes/o/in/photostream/

http://www.flickr.com/photos/digitalsextant/4842845805/sizes/o/in/photostream/

Refactoring – Does your code smell?