Refactor, the third commandment

Nicola Pietroluongo
Nicola PietroluongoLead PHP developer (full stack, Symfony expert). Technical writer, open source contributor at Royal Opera House
REFACTOR THE THIRD COMANDEMENT
by Nicola Pietroluongo
@niklongstone
Nicola Pietroluongo @niklongstone
Nicola Pietroluongo @niklongstone
TDD CYCLE
“It s a’ process of restructuring an existing code,
altering its internal structure without changing its
external behavior.”
Nicola Pietroluongo @niklongstone
DEFINITION OF REFACTORING
Code will look clean
Easy to understand
Easy to edit
Easy to mantain
Nicola Pietroluongo @niklongstone
WHY REFACTORING?
Nicola Pietroluongo @niklongstone
WHAT TO DO BEFORE
Announce the refactoring
Build a sandbox environment
Use a version control system (Git)
Remember coding standards
Remember OO design principle SOLID
Write or improve the test
Nicola Pietroluongo @niklongstone
COMMON REFACTORINGS
Change Signature
Extract (Method, Variable, ...)
Pull Members up
Push Members down
Rename
Safe Delete
UNDERSTAND THE BEHAVIOUR
Nicola Pietroluongo @niklongstone
HOW TO START A REFACTORING
USE AN IDE
Nicola Pietroluongo @niklongstone
HOW TO START A REFACTORING
START TO INSPECT
Nicola Pietroluongo @niklongstone
HOW TO START A REFACTORING
Nicola Pietroluongo @niklongstone
LEVEL OF REFACTOR
FUNCTIONS/
METHODS
CLASSES/OBJECTS
APPLICATION/DESIGN
AT FUNCTION / METHOD LEVEL
Nicola Pietroluongo @niklongstone
REFACTORING
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...? LONG MEHTOD
CODE DUPLICATION
NO STANDARDS
…..
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
NAMING
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
NAMING
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
COMPLICATE CONDITIONALS
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
COMPLICATE CONDITIONALS
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
SWITCH
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
SWITCH
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
ERROR CODES
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
ERROR CODES
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
PARAMETERS
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
PARAMETERS
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
GHOST PARAMETERS
WHY THIS IS BAD?
Nicola Pietroluongo @niklongstone
GHOST PARAMETERS
AT CLASSES / OBJECTS LEVEL
Nicola Pietroluongo @niklongstone
REFACTORING
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
DIVERGENT CHANGE
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
DIVERGENT CHANGE
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
CONSTRUCTORS
WHY THIS IS BAD?
Nicola Pietroluongo @niklongstone
CONSTRUCTORS
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
PRIMITIVE OBSESSION
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
PRIMITIVE OBSESSION
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
MESSAGE CHAIN
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
MESSAGE CHAIN
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
INAPPROPRIATE INTIMACY
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
INAPPROPRIATE INTIMACY
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
REFUSED BEDQUEST
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
REFUSED BEDQUEST
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
TRAITS FREAK
TRICK OR TRAIT
WHAT'S WRONG...?
Nicola Pietroluongo @niklongstone
TRAITS FREAK
TRICK OR TRAIT
AT APPLICATION / DESIGN LEVEL
Nicola Pietroluongo @niklongstone
REFACTORING
LAZY CLASS
Nicola Pietroluongo @niklongstone
A CLASS DOESN'T DO ENOUGH COULD BE
REMOVED
SHOTGUN SURGERY
Nicola Pietroluongo @niklongstone
DATA ABSTRACTION
Nicola Pietroluongo @niklongstone
DATA ACCESS LAYER
DB
BUSINESS LOGIC LAYER
PRESENTATION LAYER
PARALLEL INHERITANCE HIERARCHIES
Nicola Pietroluongo @niklongstone
Mover
Driver
+openWindows()
Vehicle
+getOperator(Mover)
Car
Motorbike
Rider
+moveHandGrip()
PARALLEL INHERITANCE HIERARCHIES
Nicola Pietroluongo @niklongstone
Mover
Driver
+openWindows()
Vehicle
+getOperator(Mover)
Car
Motorbike
Rider
+moveHandGrip()
Pilot
+moveFlaps()
Plane
PARALLEL INHERITANCE HIERARCHIES
Nicola Pietroluongo @niklongstone
Mover
+accelerate()
+brake()
Vehicle
+setFuel()
CarTrip
__construct(Driver)
+accelerate()
+openWindow()
...
MotorbikeTrip
__construct(Rider)
+accelerate()
+brake()
...
RESPONSIBILITY
Nicola Pietroluongo @niklongstone
Check constructors, extends and implements
Composition over inheritance
Check namespacing and folder structure
SOME TOOL
Nicola Pietroluongo @niklongstone
REFACTORING
$ sed -i 's/UglyClass/PrettyClass/g' /folder/myfile.php
$ find /folder/src -type f -exec 
sed -i 's/UglyClass/PrettyClass/g' {} ;
Nicola Pietroluongo @niklongstone
SED
Nicola Pietroluongo @niklongstone
PHPCS (and phpcbf)
Nicola Pietroluongo @niklongstone
PHPUNIT + XDEBUG
(Change Risk Analyzer and Predictor)
Nicola Pietroluongo @niklongstone
XHPROF
$ phpmd src/ text design,codesize
$ /src/File.php:26 The class File has 12 methods.
Consider refactoring File to keep number of methods
under 10.
Nicola Pietroluongo @niklongstone
PHPMD
Nicola Pietroluongo @niklongstone
PhpMetrics
● Scisr (http://iangreenleaf.github.io/Scisr/)
● Rephactor (http://rephactor.sourceforge.net/user-guide.php)
● phpdepend.org
● phpqatools.org
● https://github.com/ziadoz/awesome-php#code-analysis
Nicola Pietroluongo @niklongstone
OTHER RESOURCES
Nicola Pietroluongo @niklongstone
THANKS
IMPROVE THE CODE, IMPROVE YOURSELF
Twitter: @niklongstone
Github: github.com/niklongstone
Website: www.nicolapietroluongo.com
Code: github.com/niklongstone/php-refactoring
Nicola Pietroluongo @niklongstone
THANKS
1 of 71

More Related Content

Featured(20)

How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC3.7K views
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy82K views
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani30.2K views
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking6.9K views
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago25K views
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation
Weekdone.com7.1K views
I Rock Therefore I Am. 20 Legendary Quotes from PrinceI Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from Prince
Empowered Presentations142.8K views
How to Map Your FutureHow to Map Your Future
How to Map Your Future
SlideShop.com275.1K views
Read with Pride | LGBTQ+ ReadsRead with Pride | LGBTQ+ Reads
Read with Pride | LGBTQ+ Reads
Kayla Martin-Gant1.1K views

Refactor, the third commandment