Code Smells & Refactoring
by Carlos Mourullo
Who I am?
carlosmourullo@gmail.com
@kmur48
Spanish guy who is trying to grow personally and professionally, little by little,
every day.
When the people ask me what I do I usually say “I solve problems”.
I’m also a music lover, tennis player and bike rider.
Where I’m working?
We are hiring!!! We are hiring!!!
Boy Scout rule
“Always leave the playground cleaner than
you found it” [Uncle Bob]
But!!
 What is the playground?
 What does clean mean?
For me
 Is the lines that you need to
modify to include a new
functionality
 Easy to understand and
modify
Comments
We use these when the code is not very good / easy to
understand… Whenever you feel the need to comment on
something, write a method!
public function printOwing($amount) {
$this->printBanner();
//print details
echo "name: $this->name n";
echo "amount: $amount n";
}
public function printOwing($amount) {
$this->printBanner();
$this->printDetails($amount);
}
public function printDetails($amount) {
echo "name: $this->name n";
echo "amount: $amount n";
}
How?
 Extract Method (use good naming, please)
Duplicate code
Find a way to extract this piece of code: duplicate,
unrelated class or similar
How?
 Duplicate: use Extract Method and invoke the code from both places
 Two unrelated classes: consider using Extract Class
 Similar: you can use Form Template Method
Long method
Longer = more difficult to understand [switch context]
How?
 Use Extract Method to extract conditionals and loops also
 If you end up with a lot of parameters, use Replace Temp with Query
$basePrice = $this->_quantity * $this->_item Price;
if ($basePrice > 1000)
return $basePrice * 0.95;
else
return $basePrice * 0.98;
…
if ($this->basePrice() > 1000)
return $this->basePrice() * 0.95;
else
return $this->basePrice() * 0.98;
…
private function basePrice() {
return $this->_quantity * $this->_item Price;
}
Data clumps
Group of three or four data items together in many
places
How?
 Use Extract Class to turn clumps into an object. One immediate benefit is
that you can simplify method calling
Data class
Class that only has getting and setting. Give it some
responsibility!
How?
 Look for where getting/settings are used by other classes, them try to use
Move Method or Extract Method
Speculative generality
Solve today’s problems, not the future ones!
Sometimes we create some new cases to handle
things that aren’t required
How?
Delete!!
 Remove Parameters
 Collapse Hierarchy
Divergent changes
One class that is commonly changed in different
ways for different reasons
E.g. change your DB or use a new payment system
How?
 Separate these divergent responsibilities. Sometimes two objects are better
than one, use Extract Class
Shotgun Surgery
Every time you make any kind of change you have to
make a lot of little changes to a lot of different
classes… easy to miss something
How?
 Move Method
 Move Field to put all changes into a single class
Boy Scout rule
Deciding when to start refactoring, and when to stop is just as important to the
refactoring process as knowing how to operate the mechanics of a refactoring
If it ain’t broke, don’t fix it!
Don’t go too far
RESPECT the work of others
Thanks!!
Bibliography
 http://martinfowler.com/books/refactoring.html
 https://www.amazon.co.uk/Clean-Code-Handbook-Software-
Craftsmanship/dp/0132350882
 https://medium.com/@elmendalerenda/if-it-s-not-broke-don-t-fix-it-
3a11d4170ec4#.k2eew8whi
 https://blog.codinghorror.com/code-smells/
 http://www.industriallogic.com/wp-
content/uploads/2005/09/smellstorefactorings.pdf
 https://sourcemaking.com/refactoring/smells
 p3Hotels - http://www.p3hotels.com/
 https://jobbio.com/ie/p3hotels-careers
 GTS - http://www.goodtravelsoftware.com/
 http://www.goodtravelsoftware.com/jobs.php
Hiring!!

Code smells and refactoring

  • 1.
    Code Smells &Refactoring by Carlos Mourullo
  • 2.
    Who I am? carlosmourullo@gmail.com @kmur48 Spanishguy who is trying to grow personally and professionally, little by little, every day. When the people ask me what I do I usually say “I solve problems”. I’m also a music lover, tennis player and bike rider.
  • 3.
    Where I’m working? Weare hiring!!! We are hiring!!!
  • 4.
    Boy Scout rule “Alwaysleave the playground cleaner than you found it” [Uncle Bob] But!!  What is the playground?  What does clean mean? For me  Is the lines that you need to modify to include a new functionality  Easy to understand and modify
  • 5.
    Comments We use thesewhen the code is not very good / easy to understand… Whenever you feel the need to comment on something, write a method! public function printOwing($amount) { $this->printBanner(); //print details echo "name: $this->name n"; echo "amount: $amount n"; } public function printOwing($amount) { $this->printBanner(); $this->printDetails($amount); } public function printDetails($amount) { echo "name: $this->name n"; echo "amount: $amount n"; } How?  Extract Method (use good naming, please)
  • 6.
    Duplicate code Find away to extract this piece of code: duplicate, unrelated class or similar How?  Duplicate: use Extract Method and invoke the code from both places  Two unrelated classes: consider using Extract Class  Similar: you can use Form Template Method
  • 8.
    Long method Longer =more difficult to understand [switch context] How?  Use Extract Method to extract conditionals and loops also  If you end up with a lot of parameters, use Replace Temp with Query $basePrice = $this->_quantity * $this->_item Price; if ($basePrice > 1000) return $basePrice * 0.95; else return $basePrice * 0.98; … if ($this->basePrice() > 1000) return $this->basePrice() * 0.95; else return $this->basePrice() * 0.98; … private function basePrice() { return $this->_quantity * $this->_item Price; }
  • 9.
    Data clumps Group ofthree or four data items together in many places How?  Use Extract Class to turn clumps into an object. One immediate benefit is that you can simplify method calling
  • 10.
    Data class Class thatonly has getting and setting. Give it some responsibility! How?  Look for where getting/settings are used by other classes, them try to use Move Method or Extract Method
  • 11.
    Speculative generality Solve today’sproblems, not the future ones! Sometimes we create some new cases to handle things that aren’t required How? Delete!!  Remove Parameters  Collapse Hierarchy
  • 12.
    Divergent changes One classthat is commonly changed in different ways for different reasons E.g. change your DB or use a new payment system How?  Separate these divergent responsibilities. Sometimes two objects are better than one, use Extract Class
  • 13.
    Shotgun Surgery Every timeyou make any kind of change you have to make a lot of little changes to a lot of different classes… easy to miss something How?  Move Method  Move Field to put all changes into a single class
  • 14.
    Boy Scout rule Decidingwhen to start refactoring, and when to stop is just as important to the refactoring process as knowing how to operate the mechanics of a refactoring
  • 15.
    If it ain’tbroke, don’t fix it! Don’t go too far RESPECT the work of others Thanks!!
  • 16.
    Bibliography  http://martinfowler.com/books/refactoring.html  https://www.amazon.co.uk/Clean-Code-Handbook-Software- Craftsmanship/dp/0132350882 https://medium.com/@elmendalerenda/if-it-s-not-broke-don-t-fix-it- 3a11d4170ec4#.k2eew8whi  https://blog.codinghorror.com/code-smells/  http://www.industriallogic.com/wp- content/uploads/2005/09/smellstorefactorings.pdf  https://sourcemaking.com/refactoring/smells
  • 17.
     p3Hotels -http://www.p3hotels.com/  https://jobbio.com/ie/p3hotels-careers  GTS - http://www.goodtravelsoftware.com/  http://www.goodtravelsoftware.com/jobs.php Hiring!!

Editor's Notes

  • #5 This quote is full of meaning but it’s ambiguous and need some explanation, some context. Over the years, I went to meetups or conferences and I hear about code patterns or design patterns that you should use. And when I back to coding I didn't really know when to start to apply this patterns This code smells help me to wrong and I hope can help you too. When you are writing code, can discover really small things that indicate really big problems.
  • #6 The real key to making it easy to understand is a good naming. If you have a good name, you don’t need to look at the body.
  • #9 The longer a procedure is, the more difficult it is to understand because you need to switch content to see what the procedure does.
  • #12 “Oh I think we’ll need the ability to do this kind of thing someday”.
  • #13 When we make a change we want to be able to jump to a single clear point in the system and make the change. If we can’t, we identify a smell. When you make changes to a class you need to also modify other parts of the class, otherwise it may contain too much unrelated functionality.
  • #14 Shotgun surgery is similar to divergent change but it is the opposite. You must change lots of pieces of code in different places in order to add a new feature or extended piece of behavior.
  • #15 So! Back to the Boy Scout rule … Knowing when to start, and knowing when to stop
  • #16 When, in your editor, you open code that is already in production, you are looking at code that someone spent time and money on and probably someone is using it right now. It’s important for someone, for whatever reason.