2. 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.
4. 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
5. 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)
6. 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
7.
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 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
10. 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
11. 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
12. 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
13. 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
14. 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
15. If it ain’t broke, don’t fix it!
Don’t go too far
RESPECT the work of others
Thanks!!
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.
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.
The longer a procedure is, the more difficult it is to understand because you need to switch content to see what the procedure does.
“Oh I think we’ll need the ability to do this kind of thing someday”.
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.
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.
So! Back to the Boy Scout rule …
Knowing when to start, and knowing when to stop
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.