Refactoring
Does your code smell?
 by Martin Tschischauskas
Author
•     http://twitter.com/MartynasC
•     https://www.xing.com/profile/Martin_Ts
      chischauskas




                                                   Martin Tschischauskas
                                                   • Diplom Technische Informatik /
                                                     Computer Engineering
                                                   • Ruby on Rails Developer
                                                   • XING AG
                                                   • Jobs Team



Martin Tschischauskas                     Refactoring – Does your code smell?
Motivation                                             gems


                  Tools                      Conclusion


Martin Tschischauskas     Refactoring – Does your code smell?
Motivation

Does your code smell?




Martin Tschischauskas   Refactoring – Does your code smell?
Motivation

Situation at work




Martin Tschischauskas   Refactoring – Does your code smell?
Motivation

Lots of articles / books




Martin Tschischauskas   Refactoring – Does your code smell?
Motivation

But how to start?




Martin Tschischauskas   Refactoring – Does your code smell?
gems
Code Sadist




Martin Tschischauskas   Refactoring – Does your code smell?
flog

gem install flog

find yourapp -name *.rb | xargs flog




Martin Tschischauskas                   Refactoring – Does your code smell?
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




Martin Tschischauskas             Refactoring – Does your code smell?
flay

gem install flay

find yourapp -name *.rb | xargs flay




Martin Tschischauskas                   Refactoring – Does your code smell?
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




Martin Tschischauskas                Refactoring – Does your code smell?
heckle

gem install heckle

heckle Class method –t path_to_your_test –T timeout_in_seconds




Martin Tschischauskas            Refactoring – Does your code smell?
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




Martin Tschischauskas               Refactoring – Does your code smell?
reek

gem install reek

find yourapp -name *.rb | xargs reek




Martin Tschischauskas               Refactoring – Does your code smell?
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




Martin Tschischauskas             Refactoring – Does your code smell?
brakeman


gem install brakeman

brakeman yourapp




Martin Tschischauskas   Refactoring – Does your code smell?
brakeman


• Detects possible security issues
• No configuration is needed
• Finds possible
    • XSS
    • SQL Injection
    • …

• http://brakemanscanner.org/




Martin Tschischauskas                Refactoring – Does your code smell?
rails_best_
                                                                          practises



gem install rails_best_practises

rails_best_practices -f html




Martin Tschischauskas              Refactoring – Does your code smell?
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/




Martin Tschischauskas                  Refactoring – Does your code smell?
Code
                                                               climate
https://codeclimate.com/github/rails/rails




Martin Tschischauskas     Refactoring – Does your code smell
???




Thank
You!
Martin Tschischauskas   Refactoring – Does your code smell?
Credits

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




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




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




Martin Tschischauskas                          Refactoring – Does your code smell?

Refactoring code smell

  • 1.
    Refactoring Does your codesmell? by Martin Tschischauskas
  • 2.
    Author • http://twitter.com/MartynasC • https://www.xing.com/profile/Martin_Ts chischauskas Martin Tschischauskas • Diplom Technische Informatik / Computer Engineering • Ruby on Rails Developer • XING AG • Jobs Team Martin Tschischauskas Refactoring – Does your code smell?
  • 3.
    Motivation gems Tools Conclusion Martin Tschischauskas Refactoring – Does your code smell?
  • 4.
    Motivation Does your codesmell? Martin Tschischauskas Refactoring – Does your code smell?
  • 5.
    Motivation Situation at work MartinTschischauskas Refactoring – Does your code smell?
  • 6.
    Motivation Lots of articles/ books Martin Tschischauskas Refactoring – Does your code smell?
  • 7.
    Motivation But how tostart? Martin Tschischauskas Refactoring – Does your code smell?
  • 8.
    gems Code Sadist Martin Tschischauskas Refactoring – Does your code smell?
  • 9.
    flog gem install flog findyourapp -name *.rb | xargs flog Martin Tschischauskas Refactoring – Does your code smell?
  • 10.
    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 Martin Tschischauskas Refactoring – Does your code smell?
  • 11.
    flay gem install flay findyourapp -name *.rb | xargs flay Martin Tschischauskas Refactoring – Does your code smell?
  • 12.
    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 Martin Tschischauskas Refactoring – Does your code smell?
  • 13.
    heckle gem install heckle heckleClass method –t path_to_your_test –T timeout_in_seconds Martin Tschischauskas Refactoring – Does your code smell?
  • 14.
    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 Martin Tschischauskas Refactoring – Does your code smell?
  • 15.
    reek gem install reek findyourapp -name *.rb | xargs reek Martin Tschischauskas Refactoring – Does your code smell?
  • 16.
    reek • Detects codesmell • 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 Martin Tschischauskas Refactoring – Does your code smell?
  • 17.
    brakeman gem install brakeman brakemanyourapp Martin Tschischauskas Refactoring – Does your code smell?
  • 18.
    brakeman • Detects possiblesecurity issues • No configuration is needed • Finds possible • XSS • SQL Injection • … • http://brakemanscanner.org/ Martin Tschischauskas Refactoring – Does your code smell?
  • 19.
    rails_best_ practises gem install rails_best_practises rails_best_practices -f html Martin Tschischauskas Refactoring – Does your code smell?
  • 20.
    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/ Martin Tschischauskas Refactoring – Does your code smell?
  • 21.
    Code climate https://codeclimate.com/github/rails/rails Martin Tschischauskas Refactoring – Does your code smell
  • 22.
    ??? Thank You! Martin Tschischauskas Refactoring – Does your code smell?
  • 23.
    Credits http://www.flickr.com/photos/dana_moos/5495380861/sizes/o/in/photostream/ http://www.flickr.com/photos/digitalsextant/4842845805/sizes/o/in/photostream/ http://www.flickr.com/photos/seatbelt67/502255276/sizes/o/in/photostream/ Martin Tschischauskas Refactoring – Does your code smell?

Editor's Notes

  • #7 "http://www.amazon.de/gp/product/0321604814/ref=as_li_ss_tl?ie=UTF8&camp=1638&creative=19454&creativeASIN=0321604814&linkCode=as2&tag=woweffekt-21"
  • #8 "http://www.amazon.de/gp/product/0321604814/ref=as_li_ss_tl?ie=UTF8&camp=1638&creative=19454&creativeASIN=0321604814&linkCode=as2&tag=woweffekt-21"