Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Day2 - Refactoring (Lecture SS 2015)

453 views

Published on

Day2 - Refactoring (Lecture SS 2015)

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Day2 - Refactoring (Lecture SS 2015)

  1. 1. TDD + JavaScript Refactoring Wolfram Kriesing, uxebu @wolframkriesing
  2. 2. Schedule Intro Collect Dive in Crowd Start Solid break we hack
  3. 3. What did you take away?
  4. 4. Reminder Software Craftsmanship Apprentice Journeyman Master
  5. 5. no IFs
  6. 6. Refactoring
  7. 7. Design is hard. The design of reusable software is especially hard. Reusable software usually is the result of many design iterations. William F. Opdyke, 1992 it’sallaboutdesign
  8. 8. Books, papers • „REFACTORING OBJECT-ORIENTED FRAMEWORKS“
 William F. Opdyke, 1992
 http://www.ai.univ-paris8.fr/~lysop/opdyke-thesis.pdf • „Refactoring: Improving the Design of Existing Code“
 Martin Fowler, 1999 • „Working Effectively with Legacy Code“
 Michael Feathers, 2004 • „31 Days of Refactoring“
 http://lostechies.com/wp-content/uploads/2011/03/31DaysRefactoring.pdf • „Subjective evaluation of software evolvability using code smells: An empirical study“
 http://www.soberit.hut.fi/~mmantyla/ESE_2006.pdf
  9. 9. http://www.testingeducation.org/pt/Refactoring-smells.pdf
  10. 10. Our software …
  11. 11. we plan first
  12. 12. over time …
  13. 13. https://www.flickr.com/photos/ronipothead/6294148752 it gets rusty …
  14. 14. we should …
  15. 15. improve design after code was written
  16. 16. Improve design after code was written what? isn’t it reverse?
  17. 17. Tennis Let’s collect smells!
  18. 18. Where does it come from?
  19. 19. smalltalk
  20. 20. - made it popular - enables agility get from the book: - code smells - refactoring techniques
  21. 21. Incremental Design Improve structure all the time was not possible with this tower, of course
  22. 22. https://www.flickr.com/photos/jessicabee/897482687/ Baby Steps to Stay Green
  23. 23. Refactoring in action
  24. 24. Refactoring in action • monolithic • >1 indention levels • nested loops
 
 
 
 
 
 • modular • explicit function names • tiny functions • max. one level indent • no nested loops • composing
  25. 25. • transparent responsibilities • separation of concerns • possible reusability • code structure • no unmaintained comments
  26. 26. • transparent responsibilities • separation of concerns • possible reusability • code structure
  27. 27. What are code smells?
  28. 28. http://james.padolsey.com/javascript/jquery-code-smells/
  29. 29. $ prefixing Artificial selectors Inefficient jQuery usage Hard-code dependencies Chain explosion No-caching
  30. 30. It’s all automatically detectable! Explore Stabilize Commoditize e.g. with esprima
  31. 31. :-)
  32. 32. Common Code Smells https://www.flickr.com/photos/awnisalan/352498081
  33. 33. Common Code Smells https://www.flickr.com/photos/awnisalan/352498081 1) Low-hanging fruit
  34. 34. Comments • may be wrong • they rot • useless • actual good (method) name
  35. 35. Long methods • hard to read • many contexts • too many LOC • too many statements • define your own
  36. 36. Duplicated code • duplicated bugs • double the fixes • more code to ship • performance break
  37. 37. Bad names
  38. 38. Long parameter list • too many responsibilities • bad API • hard to use • hard to maintain • points to design defects
  39. 39. Boolean trap • intention not clear • easy to understand wrong
  40. 40. Complexity
  41. 41. Inconsistent names Dead code many more …
  42. 42. Common Code Smells https://www.flickr.com/photos/awnisalan/352498081 1) Low-hanging fruit 2) Boomerang
  43. 43. Conditionals Combinatorial Explosion Type embedding Data Clumps sets of variables that are always together, get passed in, etc. => extract into class avoid placing types in method names; it's not only redundant, but it forces you to change the name if the type changes You have lots of code that does almost the same thing watch out for large conditional logic blocks
 particularly blocks that tend to grow larger or change significantly over time
  44. 44. Feature envy a method making more use of another class than the one it is in Speculative Generality write code to solve today's problems
 and worry about tomorrow's problems when they actually materialize
  45. 45. Common Code Smells https://www.flickr.com/photos/awnisalan/352498081 1) Low-hanging fruit 2) Boomerang 3) In tests
  46. 46. Test smells http://xunitpatterns.com/Test%20Smells.html
  47. 47. Obscure test it is difficult to understand the test at a glance Conditional test logic • tests take too long to run • make developers not run the tests • leads to untested code • demotivates continuous testing • make code rot
  48. 48. Setup too complex Unclear Act Multiple asserts No assert
  49. 49. Logic in test Duplication in tests Dependent tests Modification of unit under test
  50. 50. General fixture a setUp method contains code only used by some tests Lazy test using the same fixture for multiple tests Test run war test can only be run by one person at a time, due to use of same external ressource
  51. 51. JavaScript Code Smells https://www.flickr.com/photos/brian-fitzgerald/3334353375 ?
  52. 52. Refactorings
  53. 53. http://refactoring.com/catalog/

×