際際滷

際際滷Share a Scribd company logo
Nicola Pietroluongo @niklongstone
REFACTORING
THE THIRD
COMMANDMENT
Nicola Pietroluongo @niklongstone
TDD cycle
Nicola Pietroluongo @niklongstone
Nicola Pietroluongo
@niklongstone
 Lead developer/Solution architect
 Tech article writer
 Open Source contributor
 Author of Learning PHP7 by Packt Publishing
 +10y of PHP programming
 Amazon Alexa skill challenge award winner
Nicola Pietroluongo @niklongstone
Refactoring the 3rd commandment
Sections
 Introduction
 Code smells
 Summary and tools
Nicola Pietroluongo @niklongstone
Its a process of restructuring an existing
code, altering its internal structure
without changing its external behavior.
Nicola Pietroluongo @niklongstone
Why should you refactor?
Benefits
Your code:
 will look clean
 easy to understand
 easy to edit/maintain
You:
 will code faster
 will spot bug easier
Nicola Pietroluongo @niklongstone
Happy days
Nicola Pietroluongo @niklongstone
When should you refactor?
Rule of three two
 Everytime you add a feature
 Everytime you fix a bug
 During the review process
Nicola Pietroluongo @niklongstone
When Shouldn't You Refactor?
It depends
 Spaghetti code
 Extremely coupled
 Code too buggy
 Close to deadline
Nicola Pietroluongo @niklongstone
What do I tell to the business?
Nicola Pietroluongo @niklongstone
Happy days
Nicola Pietroluongo @niklongstone
What to do before?
Starting steps
 Announce it to your teammate
 Use a VCS
 Remember coding standard
 Remember OO and SOLID principles
 Write or improve the test
Nicola Pietroluongo @niklongstone
Code smells
Warning signs in your code
Nicola Pietroluongo @niklongstone
Code smells
If it stinks change it
 Bloaters
 Object-Orientation Abusers
 Change Preventers
 Unneeded
 Couplers
Nicola Pietroluongo @niklongstone
Bloaters
Nicola Pietroluongo @niklongstone
Method / Class
Size
 Long method
 Large class
 Long parameter list
Nicola Pietroluongo @niklongstone
Primitive Obsession
Use object instead
class Authentication
{
const ROLE_GUEST = 0;
const ROLE_MEMBER = 1;
const ROLE_ADMIN = 2;
const USER_ROLE = role;
public function getRole($user)
{
$role = $user[self::USER_ROLE];
// other operations
}
Nicola Pietroluongo @niklongstone
Data Clumps
Use object instead
public function createPermission($user, $pass, $read, $write)
{
// other operations
}
public function updatePermission($user, $pass, $read, $write)
{
// other operations
}
Nicola Pietroluongo @niklongstone
Object-Orientation Abusers
Nicola Pietroluongo @niklongstone
Switch Statement
Use object instead
switch ($city)
{
case 'Brussels':
return getWinterClothes();
case 'Miami':
return getSummerClothes();
case 'Lisbon':
return getNormalClothes();
}
Nicola Pietroluongo @niklongstone
Switch Statement
Use object instead
interface City { public function getClothes(); }
class Brussels implements City
{
function getClothes() { return 'normalClothes'; }
}
class Miami implements City{ //code...}
$cityClothes = new $city(); // i.e. $city = Brussels;
$cityClothes->getClothes();
Nicola Pietroluongo @niklongstone
Refuse Badquest
A subclass uses only some methods
interface Animal
{
public function fly();
public function swim();
public function eat();
}
class Fish implements Animal {
public function swim() { //  }
public function fly() { //trow Exception }
//
Nicola Pietroluongo @niklongstone
I exists...
Nicola Pietroluongo @niklongstone
Refuse Badquest
A subclass uses only some methods
interface GenericAnimal
{
public function eat()
}
interface SeaAnimal
{
public function swim()
}
class Fish implements GenericAnimal, SeaAnimal
Nicola Pietroluongo @niklongstone
Change Preventers
Nicola Pietroluongo @niklongstone
Change Preventers
When you make many changes in other
places
10101101010
10101101010 10101101010
Nicola Pietroluongo @niklongstone
Change Preventers
How to avoid
 Keep the class hierarchy small
 Avoid classes with same behaviour
 Enforce the Single responsibility principle
Nicola Pietroluongo @niklongstone
Unneeded
Nicola Pietroluongo @niklongstone
Unneeded
You can live without that
 Comments
 Dead code
 Future code not in use
Nicola Pietroluongo @niklongstone
Couplers
Nicola Pietroluongo @niklongstone
Couplers
You spend too much time together...
 Message Chain
 Feature Envy
 Inappropriate Intimacy
 Middle Man
 Traits freak
Nicola Pietroluongo @niklongstone
Message chain
You can live without that
CLIENT
EMPLOYEE
getOffice()
returns class Office
OFFICE
getManager()
$manager = $employee->getOffice()->getManager();
Nicola Pietroluongo @niklongstone
Message chain
You can live without that
CLIENT
EMPLOYEE
getManager()
OFFICE
$manager = $employee->getManager();
Nicola Pietroluongo @niklongstone
Traits freak
Be careful
Nicola Pietroluongo @niklongstone
I am a trait
Nicola Pietroluongo @niklongstone
Traits freak
Bad
Nicola Pietroluongo @niklongstone
Traits freak
...
Nicola Pietroluongo @niklongstone
More examples
PHP Code smells
https://github.com/niklongstone/php-refactoring
Nicola Pietroluongo @niklongstone
Summary
How to prevent code smells in PHP
Nicola Pietroluongo @niklongstone
How to improve
Part of your development process
 Short methods and classes
 Avoid duplications (DRY)
 SOLID principle
 Composition over inheritance
 Objects over simple structures
 declare(strict_types=1);
 Share your findings
Nicola Pietroluongo @niklongstone
Tools
Tools and automation
Nicola Pietroluongo @niklongstone
PHP CSF
Nicola Pietroluongo @niklongstone
Exakat
Nicola Pietroluongo @niklongstone
PhpMetrics
Nicola Pietroluongo @niklongstone
PHP MD
Nicola Pietroluongo @niklongstone
PHPMD
$ ./phpmd.phar filesOrDir reportFormat rules
$ ./phpmd.phar fileA.php,fileB.php text
cleancode,codesize
Nicola Pietroluongo @niklongstone
Git diff
$ git diff --name-only --diff-filter=d
master
src/CinemaBundle/Controller/CinemaController.php
src/CinemaBundle/Entity/Screening.php
src/CinemaBundle/Entity/ScreeningVenue.php
src/CinemaBundle/Entity/Showing.php
Nicola Pietroluongo @niklongstone
Sed
$ sed ':a; $!N; s/n/,/; ta'
:a creates a pattern
$!N appends lines to the pattern if not last
line
s/n/,/ replaces new line with comma
ta repeat the a
Nicola Pietroluongo @niklongstone
Pull it together
*All in one line
./phpmd.phar
$(git diff --name-only --diff-filter=d master
|
sed ':a; $!N; s/n/,/; ta')
text cleancode,codesize
Nicola Pietroluongo @niklongstone
To improve yourself,
Improve your code.
Nicola Pietroluongo @niklongstone
Thanks!
!
NicolaPietroluongo.com
@niklongstone
https://github.com/niklongstone
https://joind.in/talk/a7aa2
Ad

Recommended

ODP
Refactor, the third commandment
Nicola Pietroluongo
PDF
Save time by applying clean code principles
Edorian
PDF
Clean Code V2
Jean Carlo Machado
PDF
Clean code & design patterns
Pascal Larocque
PDF
Objects, Testing, and Responsibility
machuga
PDF
Dependency Injection for PHP
mtoppa
PPTX
The reviewer checklist
Nicola Pietroluongo
PPTX
Paving the Way for Agile Engineering Practices
Aman King
PDF
Reduce Reuse Refactor
Alena Holligan
PDF
Midiendo la calidad de c坦digo en WTF/Min (Revisado EUI Abril 2014)
David G坦mez Garc鱈a
PDF
Refactoring: the good, the bad and the ugly.
Jeroen Thora
PDF
Refactoring PHP
Adam Culp
PDF
Object Oriented Programming for WordPress Plugin Development
mtoppa
ODP
Documenting code yapceu2016
S淡ren Lund
PDF
Mine Your Own Code
Peter Norrhall
ODP
Documenting Code - Patterns and Anti-patterns - NLPW 2016
S淡ren Lund
PDF
Modernizing Legacy Applications in PHP, por Paul Jones
iMasters
PDF
Refactoring
Xavier Forn辿s Arrabal
PPTX
Refactoring workshop
Itzik Saban
PPTX
Art of refactoring - Code Smells and Microservices Antipatterns
El Mahdi Benzekri
PPTX
Object Oriented Apologetics
Vance Lucas
PDF
Trunk based development
go_oh
PDF
Refactoring.pdf
Mnats Karakhanyan
PDF
Writing Readable Code
eddiehaber
PDF
[DevDay2018] Lets all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
PPTX
Implementing The Open/Closed Principle
Sam Hennessy
PDF
Tool up your lamp stack
AgileOnTheBeach
PDF
Tool Up Your LAMP Stack
Lorna Mitchell
PDF
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
PPTX
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante

More Related Content

Similar to Refactoring the third commandment (20)

PDF
Reduce Reuse Refactor
Alena Holligan
PDF
Midiendo la calidad de c坦digo en WTF/Min (Revisado EUI Abril 2014)
David G坦mez Garc鱈a
PDF
Refactoring: the good, the bad and the ugly.
Jeroen Thora
PDF
Refactoring PHP
Adam Culp
PDF
Object Oriented Programming for WordPress Plugin Development
mtoppa
ODP
Documenting code yapceu2016
S淡ren Lund
PDF
Mine Your Own Code
Peter Norrhall
ODP
Documenting Code - Patterns and Anti-patterns - NLPW 2016
S淡ren Lund
PDF
Modernizing Legacy Applications in PHP, por Paul Jones
iMasters
PDF
Refactoring
Xavier Forn辿s Arrabal
PPTX
Refactoring workshop
Itzik Saban
PPTX
Art of refactoring - Code Smells and Microservices Antipatterns
El Mahdi Benzekri
PPTX
Object Oriented Apologetics
Vance Lucas
PDF
Trunk based development
go_oh
PDF
Refactoring.pdf
Mnats Karakhanyan
PDF
Writing Readable Code
eddiehaber
PDF
[DevDay2018] Lets all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
PPTX
Implementing The Open/Closed Principle
Sam Hennessy
PDF
Tool up your lamp stack
AgileOnTheBeach
PDF
Tool Up Your LAMP Stack
Lorna Mitchell
Reduce Reuse Refactor
Alena Holligan
Midiendo la calidad de c坦digo en WTF/Min (Revisado EUI Abril 2014)
David G坦mez Garc鱈a
Refactoring: the good, the bad and the ugly.
Jeroen Thora
Refactoring PHP
Adam Culp
Object Oriented Programming for WordPress Plugin Development
mtoppa
Documenting code yapceu2016
S淡ren Lund
Mine Your Own Code
Peter Norrhall
Documenting Code - Patterns and Anti-patterns - NLPW 2016
S淡ren Lund
Modernizing Legacy Applications in PHP, por Paul Jones
iMasters
Refactoring workshop
Itzik Saban
Art of refactoring - Code Smells and Microservices Antipatterns
El Mahdi Benzekri
Object Oriented Apologetics
Vance Lucas
Trunk based development
go_oh
Refactoring.pdf
Mnats Karakhanyan
Writing Readable Code
eddiehaber
[DevDay2018] Lets all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
Implementing The Open/Closed Principle
Sam Hennessy
Tool up your lamp stack
AgileOnTheBeach
Tool Up Your LAMP Stack
Lorna Mitchell

Recently uploaded (20)

PDF
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
PPTX
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
PDF
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
PDF
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
PPTX
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
PDF
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
PDF
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
PPTX
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
PDF
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
PDF
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
PPTX
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
PDF
The Growing Value and Application of FME & GenAI
Safe Software
PDF
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
PDF
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
PDF
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
PDF
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
PDF
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
The Future of Product Management in AI ERA.pdf
Alyona Owens
UserCon Belgium: Honey, VMware increased my bill
stijn40
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
The Growing Value and Application of FME & GenAI
Safe Software
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
Ad

Refactoring the third commandment