code smells and refactoring

Post on 22-Jan-2018

434 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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;

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;

}

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!!

top related