Code refactoring Long Nguyen © Employment Hero, September 2016 ! 1
❝I like my code to be elegant and efficient.❞ —Bjarne Stroustrup (inventor of C++) © Employment Hero, September 2016 ! 2
❝Clean code is ... full of crips abstractions ...❞ —Grady Booch (author of Object Oriented Analysis and Design with Applications) © Employment Hero, September 2016 ! 3
❝Clean code was written by someone who cares.❞ —Michael Feathers (author of Working Effectively with Legacy Code) © Employment Hero, September 2016 ! 4
What is refactoring ? Martin Fowler: Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of small behavior-preserving transformations, each of which “too small to be worth doing”. ! © Employment Hero, September 2016 ! 5
What is refactoring ? Joshua Kerievsky: By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you’ll find that it is easier to extend and maintain code. ! © Employment Hero, September 2016 ! 6
What is refactoring ? Michael Feathers: One of the clearest preconditions for refactoring is the existence of tests. Martin Fowler is pretty explicit about that in his Refactoring book, and everything I’ve experienced with teams backs it up. Want to do make things better? Sure, but if you don’t have tests to support you, you’re gambling. Can you do a little refactoring to get tests in place? Yes, I advise it, but when you start to do significant refactoring, you’d better have tests to back you up. If you don’t, it’s only a matter of time before your teammates take away your keyboard. ! © Employment Hero, September 2016 ! 7
Refactoring in couple of words ☞ A controlled technique ☞ Improving the design ☞ Existing code base ☞ Series of transformations ☞ Habit ☞ Maintain ☞ Tests ☞ Teammates © Employment Hero, September 2016 ! 8
Programming principles There're lot of them. This website did a great job of collecting most of them together http://webpro.github.io/programming-principles Please go and read them all, then you will have a good sense about good code. ! I will share some of uncommon principles which not that popular but I think they're great. Which can be a great guidance: ☞ Code symmetry ☞ Rate of change ☞ Local Consequences ☞ Minimize Repetition ☞ Logic and Data Together ☞ Declarative Expression © Employment Hero, September 2016 ! 9
Code symmetry Identifying and clearly expressing symmetry makes code easier to read. Once readers understand one half of the symmetry, they can quickly understand the other half. Example: void process() { input(); count++; output(); } © Employment Hero, September 2016 ! 10
Symmetry in code is where the same idea is expressed the same way everywhere it appears in the code. void process() { input(); incrementCount(); output(); } © Employment Hero, September 2016 ! 11
Why all that ? costtotal = costdevelop + costmaintain costmaintain = costunderstand + costchange + costtest + costdeploy © Employment Hero, September 2016 ! 12
Demo ! " # $ © Employment Hero, September 2016 ! 13
References ☞ Implementation Patterns https://www.amazon.com/ Implementation-Patterns-Kent-Beck/dp/0321413091 ☞ Working Effectively with Legacy Code https:// www.amazon.com/Working-Effectively-Legacy-Michael- Feathers/dp/0131177052 ☞ Practical Object-Oriented Design in Ruby https:// www.amazon.com/Practical-Object-Oriented-Design-Ruby- Addison-Wesley/dp/0321721330 © Employment Hero, September 2016 ! 14

Code refactoring

  • 1.
    Code refactoring Long Nguyen ©Employment Hero, September 2016 ! 1
  • 2.
    ❝I like mycode to be elegant and efficient.❞ —Bjarne Stroustrup (inventor of C++) © Employment Hero, September 2016 ! 2
  • 3.
    ❝Clean code is... full of crips abstractions ...❞ —Grady Booch (author of Object Oriented Analysis and Design with Applications) © Employment Hero, September 2016 ! 3
  • 4.
    ❝Clean code waswritten by someone who cares.❞ —Michael Feathers (author of Working Effectively with Legacy Code) © Employment Hero, September 2016 ! 4
  • 5.
    What is refactoring? Martin Fowler: Refactoring is a controlled technique for improving the design of an existing code base. Its essence is applying a series of small behavior-preserving transformations, each of which “too small to be worth doing”. ! © Employment Hero, September 2016 ! 5
  • 6.
    What is refactoring? Joshua Kerievsky: By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you’ll find that it is easier to extend and maintain code. ! © Employment Hero, September 2016 ! 6
  • 7.
    What is refactoring? Michael Feathers: One of the clearest preconditions for refactoring is the existence of tests. Martin Fowler is pretty explicit about that in his Refactoring book, and everything I’ve experienced with teams backs it up. Want to do make things better? Sure, but if you don’t have tests to support you, you’re gambling. Can you do a little refactoring to get tests in place? Yes, I advise it, but when you start to do significant refactoring, you’d better have tests to back you up. If you don’t, it’s only a matter of time before your teammates take away your keyboard. ! © Employment Hero, September 2016 ! 7
  • 8.
    Refactoring in coupleof words ☞ A controlled technique ☞ Improving the design ☞ Existing code base ☞ Series of transformations ☞ Habit ☞ Maintain ☞ Tests ☞ Teammates © Employment Hero, September 2016 ! 8
  • 9.
    Programming principles There're lotof them. This website did a great job of collecting most of them together http://webpro.github.io/programming-principles Please go and read them all, then you will have a good sense about good code. ! I will share some of uncommon principles which not that popular but I think they're great. Which can be a great guidance: ☞ Code symmetry ☞ Rate of change ☞ Local Consequences ☞ Minimize Repetition ☞ Logic and Data Together ☞ Declarative Expression © Employment Hero, September 2016 ! 9
  • 10.
    Code symmetry Identifying andclearly expressing symmetry makes code easier to read. Once readers understand one half of the symmetry, they can quickly understand the other half. Example: void process() { input(); count++; output(); } © Employment Hero, September 2016 ! 10
  • 11.
    Symmetry in codeis where the same idea is expressed the same way everywhere it appears in the code. void process() { input(); incrementCount(); output(); } © Employment Hero, September 2016 ! 11
  • 12.
    Why all that? costtotal = costdevelop + costmaintain costmaintain = costunderstand + costchange + costtest + costdeploy © Employment Hero, September 2016 ! 12
  • 13.
    Demo ! " #$ © Employment Hero, September 2016 ! 13
  • 14.
    References ☞ Implementation Patternshttps://www.amazon.com/ Implementation-Patterns-Kent-Beck/dp/0321413091 ☞ Working Effectively with Legacy Code https:// www.amazon.com/Working-Effectively-Legacy-Michael- Feathers/dp/0131177052 ☞ Practical Object-Oriented Design in Ruby https:// www.amazon.com/Practical-Object-Oriented-Design-Ruby- Addison-Wesley/dp/0321721330 © Employment Hero, September 2016 ! 14