Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring Ruby

Refactoring Ruby

Short talk about refactoring and Ruby code complexity analysis.

Anatoli Makarevich

April 23, 2013
Tweet

More Decks by Anatoli Makarevich

Other Decks in Programming

Transcript

  1. In next 15 minutes we will • spice up our

    knowledge about refactoring • look at some common smells • get acquainted with automated code analysis • become ready to refactor
  2. Code Smell Any symptom in the source code of a

    program that possibly indicates a deeper problem Kent Beck
  3. Code smells Long method Duplications Heavy class Stupid name Too

    many params Feature envy Ubercallback Complex conditions
  4. Refactoring cycle Check test coverage Write tests if needed Change

    code Ensure that tests pass Check overall complexity
  5. Find code smells Check test coverage Write tests if needed

    Change code Ensure that tests pass Check overall complexity
  6. Check test coverage Check test coverage Write tests if needed

    Change code Ensure that tests pass Check overall complexity
  7. Write new tests if needed Check test coverage Write tests

    if needed Change code Ensure that tests pass Check overall complexity
  8. reFACTOR Check test coverage Write tests if needed Change code

    Ensure that tests pass Check overall complexity
  9. Ensure that tests pass Check test coverage Write tests if

    needed Change code Ensure that tests pass Check overall complexity
  10. Automate tests! Check test coverage Write tests if needed Change

    code Ensure that tests pass Check overall complexity Travis CI Jenkins CI Gitlab CI
  11. Automate complexity analysis? Check test coverage Write tests if needed

    Change code Ensure that tests pass Check overall complexity
  12. s(:class, :Sexp, s(:const, :Array), s(:defn, :sexp_type, s(:args), s(:call, nil, :first)),

    s(:defn, :sexp_body, s(:args), s(:call, nil, :[], s(:lit, 1..-1)))) S-expressions
  13. s(:class, :Sexp, s(:const, :Array), s(:defn, :sexp_type, s(:args), s(:call, nil, :first)),

    s(:defn, :sexp_body, s(:args), s(:call, nil, :[], s(:lit, 1..-1)))) Sexp operator
  14. s(:class, :Sexp, s(:const, :Array), s(:defn, :sexp_type, s(:args), s(:call, nil, :first)),

    s(:defn, :sexp_body, s(:args), s(:call, nil, :[], s(:lit, 1..-1)))) Sexp body
  15. Long method s(:class, :Sexp, s(:const, :Array), s(:defn, :sexp_type, s(:args), s(:call,

    nil, :first)), s(:defn, :sexp_body, s(:args), s(:call, nil, :[], s(:lit, 1..-1)))) Find method Sexp :defn
  16. Long method s(:class, :Sexp, s(:const, :Array), s(:defn, :sexp_type, s(:args), s(:call,

    nil, :first)), s(:defn, :sexp_body, s(:args), s(:call, nil, :[], s(:lit, 1..-1)))) Find method Sexp :defn Count sexp operators
  17. Complex class s(:class, :Sexp, s(:const, :Array), s(:defn, :sexp_type, s(:args), s(:call,

    nil, :first)), s(:defn, :sexp_body, s(:args), s(:call, nil, :[], s(:lit, 1..-1)))) Find method Sexp :class
  18. Complex class s(:class, :Sexp, s(:const, :Array), s(:defn, :sexp_type, s(:args), s(:call,

    nil, :first)), s(:defn, :sexp_body, s(:args), s(:call, nil, :[], s(:lit, 1..-1)))) Find method Sexp :class Cound weights of Sexp operators
  19. Stupid variable name s(:class, :HomerSimpson, s(:const, :BlahBlahBlah), s(:defn, :donuts, s(:args),

    s(:call, nil, :fist)), s(:defn, :beersnstuff, s(:args), s(:call, nil, :[], s(:lit, 1..-1))))
  20. After this talk do: • gem install flog • gem

    install flay • gem install reek • flog PROJECT_PATH • flay PROJECT_PATH • reek PROJECT_PATH