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

Refactoring with examples

Avatar for Adam Jonas Adam Jonas
November 14, 2012

Refactoring with examples

The Why, Whens, and Hows refactoring. Inspired by Refactoring: Ruby Edition.

Avatar for Adam Jonas

Adam Jonas

November 14, 2012

Other Decks in Programming

Transcript

  1. Refactoring Patterns - If you don't know the patterns, you

    are less likely to capture their wisdom. - We learn through heuristics.
  2. Learning Great Habits “I'm not a great programmer; I'm just

    a good programmer with great habits.” - Kent Beck
  3. More Kent Beck Wisdom... Make it work Make it right

    Make it fast Refactoring is about making it right
  4. Refactoring is... A change made to the internal structure of

    software to make it easier to understand and cheaper to modify without changing its observable behavior. [p. 53]
  5. Refactoring and Baby Pandas - Who doesn't like rainbows? baby

    pandas? parfait? - But why is refactoring important?
  6. Why should I do it? - Code I wrote yesterday

    isn't as good as today - Know more about the system and I can apply that knowledge. - Make it easier to add new code - Help the next person understand -
  7. "Programs must be written for people to read, and only

    incidentally for machines to execute." - Abelson & Sussman, Structure and Interpretation of Computer Programs "Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Refactoring [p.11] Why change code that works?
  8. Got to have tests to let you know when it's

    broken. TDD: Red, Green, Refactor Know when you broke your code
  9. Code Smells - Duplicated Code - Long method - Large

    class - Long parameter list - Local variable assigned more than once, but not a loop or collection
  10. - Replace Loop With a Collection - Extraction method -

    Dynamic Method Definition Refactoring Patterns
  11. Extraction Pattern Turn a fragment into a method whose name

    explains the purpose of the method. In the wild:
  12. Photos: - http://www.flickr.com/photos/cobalt/5637229371 - http://www.flickr.com/photos/sheeshoo/258734514 - http://www.flickr.com/photos/ruoxinyi/4596954281 - http://www.flickr.com/photos/rockerbotrhi/3858953947/ -

    http://www.flickr.com/photos/kenberg/6311136154/ - http://www.flickr.com/photos/sanchom/2963072255 - http://www.flickr.com/photos/broken_simulacra/118535492 - http://cdn.memegenerator.net/instances/400x/26336623.jpg Books: Refactoring: Ruby Edition By Fields, Harvie, Fowler with Beck, 2010 Structure and Interpretation of Computer Programs, By Abelson, Sussman, 1985 Catalog: - http://www.refactoring.com/catalog/index.html Sources