code smells and refactoring

17
Code Smells & Refactoring by Carlos Mourullo

Upload: carlos-mourullo

Post on 22-Jan-2018

433 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Code smells and refactoring

Code Smells & Refactoring

by Carlos Mourullo

Page 2: Code smells and refactoring

Who I [email protected]

@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.

Page 3: Code smells and refactoring

Where I’m working?

We are hiring!!! We are hiring!!!

Page 4: Code smells and refactoring

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

Page 5: Code smells and refactoring

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 detailsecho "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)

Page 6: Code smells and refactoring

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

Page 7: Code smells and refactoring
Page 8: Code smells and refactoring

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;

elsereturn $basePrice * 0.98;…

if ($this->basePrice() > 1000)return $this->basePrice() * 0.95;

elsereturn $this->basePrice() * 0.98;

private function basePrice() {return $this->_quantity * $this->_item Price;

}

Page 9: Code smells and refactoring

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

Page 10: Code smells and refactoring

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

Page 11: Code smells and refactoring

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

Page 12: Code smells and refactoring

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

Page 13: Code smells and refactoring

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

Page 14: Code smells and refactoring

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

Page 15: Code smells and refactoring

If it ain’t broke, don’t fix it!

Don’t go too far

RESPECT the work of others

Thanks!!

Page 16: Code smells and refactoring

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

Page 17: Code smells and refactoring

p3Hotels - http://www.p3hotels.com/

https://jobbio.com/ie/p3hotels-careers

GTS - http://www.goodtravelsoftware.com/

http://www.goodtravelsoftware.com/jobs.php

Hiring!!