SlideShare a Scribd company logo
Refactoring



             Presentation for:
      South Florida PHP Users Group
                     By:
               Adam Culp
              Twitter: @adamculp
           http://www.geekyboy.com
Refactoring

   Based on popular “Refactoring” Improving The
    Design of Existing Code book, by Martin
    Fowler.
   We will start in the code with an example.
   Tips and descriptions will be handled during the
    example.
   In the example we are tasked with creating an
    HTML representation of the customer
    statement for a movie rental.
Refactoring

   Movie class
       With a new project we take
        a look at the current
        application a customer has
        requested to be changed.
       We start with the Movie
        class.
Refactoring

   Rental class
       Next we look at the Rental
        class.
Refactoring

   Customer class p1
       With a new project we take
        a look at the current app
        that is about to be
        changed.
       Next we look at part 1 of
        the Customer class.
Refactoring

   Customer class p2
       With a new project we take
        a look at the current app
        that is about to be
        changed.
       Next we look at part 2 of
        the Customer class.
Refactoring

   Not terrible for a small quick and dirty application, but
    if part of a larger application we have problems.
   Customer class doing far too much, and statement
    method is too long.
   Impossible to reuse the statement for an HTML
    representation.
   The customer also wants to be able to change how
    they classify movies.
   As experienced developers we know that customers
    will always come back six months from now and want
    it changed in some way.
Refactoring

   Rewrite vs Refactor
       Budget constraint
       Time constraint
       Rewrite is an excuse to NOT dig in someone's code
       Refactor is the best teacher
Refactoring

   First step
       Build tests based on current “working” application,
        so we can verify if we break something.
       Pre-load database with data or create fixtures
        needed for tests, to ensure we have a constant to
        compare test results.
       If we are not starting with a working application you
        must first get the application working prior to
        refactoring. Do not try to refactor a broken
        application or you risk breaking it further, and end
        up with a total rewrite.
Refactoring
   Step 1
       Extract Method to move switch statement to its own method.
Refactoring
   Step 2
       Variable names should make sense. (each > rental, thisAmount > result)
Refactoring
   Step 3
       Extract Method amountFor() to Rental class. It uses information from
        Rental, but no information from Customer. We rename it to more
        meaningful getCharge().
Refactoring
   Step 4
       Call getCharge() directly from statement() instead of using amountFor() as
        a middle man method.
Refactoring
   Step 5
       Replace Temp with Query - Cleanup of call to getCharge(). Eliminate
        temp variables by calling method direct. Less maintenance.
Refactoring
   Step 6
       Extract Method and move frequentRenterPoints to its own method in
        Rental class where it logically belongs.
Refactoring
   Step 7
       We do Replace Temp with Query for totalAmount. Temp variables can be
        a problem and really only serve a purpose within their own routine and
        encourage long, complex routines.
Refactoring
   Step 8
       Also do Replace Temp with Query on frequentRentalPoints to eliminate
        more temp variables.
Refactoring
   Step 9
       We are now able to create the HTML version of the statement, and we
        rename the original to represent a Text version.
Refactoring

   Recap
       Most refactoring reduces code, while we increased it for
        this example.
       We also duplicated a loop to make it happen 4 times
        instead    of     once.     (getFrequentRenterPoints,
        getTotalCharge, getTotalFrequentRenterPoints)
       Optimizing and refactoring are different actions.
        Refactor first, then optimize later if needed.
       More refactoring could further clean up Text and HTML
        statements to DRY it up. (split out header/footer, etc.)
       Still need more refactoring to allow classification
        changes by the customer.
Refactoring
   Step 10
       Move Method on getCharge() because it uses data in Movie, so should be
        in the same class as the data.
Refactoring
   Step 11
       Replace Conditional with Polymorphism on getCharge() because it uses
        data in Movie, so should be in the same class as the data.
Refactoring

   Two hats
       Adding Function Hat
       Refactoring Hat
First we add functionality, they we refactor it, then
add more functionality, ...
Refactoring

   Why Refactor?
       Without refactoring code decays
       As code is changed it loses structure, making it
        harder to see design
                  Regular refactoring preserves design
       Poorly designed = more code due to duplication
                  Reduced code = easier to maintain
                  Reduced code = easier to understand
       Helps find bugs
       Helps us program faster
Refactoring

   When to Refactor?
       Should not set aside time to refactor, it is just
        something we do in short bursts
       Refactor because you want to do something, and
        refactoring helps you do it
                  Often a quick refactor can help developing new
                    functionalities move faster
       Rule of Three → see next slide
Refactoring

   Rule of Three
       When you add function
                  Helps to learn code you need to modify
                  Changes code that prevents the addition
       When you need to fix a bug
                  Make code more understandable
                  Usually highlights the bug
       During code review
                  Code looks good to developer, but maybe not to
                    the team.
                  More concrete results
Refactoring

   What do I tell my manager?
       For a tech saavy manager it may not be hard to
        explain the benefits
       Quality centric manager stress quality aspects
                   Perhaps introduce it as a review process
                   There are many resources on Google about
                     value of reviews, inspections, or software
                     development process
       Schedule driven … Don't tell (controversial?)
                   Find a way to work it into scheduling.
                   Overall it saves time, but some will never “see” it
Refactoring

   Code “smells” as indicators
       Bad code “smells” and is a great way of indicating it
        is time for a refactor.
                   Duplicate Code
                   Long Method
                   Large Class
                   Long Parameter (argument) List
                   Divergent Change - if change is needed in
                     multiple areas to accommodate a change
                   Shotgun Surgery – change causes ripple of other
                     needed changes
                   Feature Envy – a method seems to use another
                     class instead of the one it is in
Refactoring

   Code “smells” as indicators
       More “smells”
                  Data Clumps – if data items tend to accompany
                    one another
                  Primitive Obsession
                  Switch statements
                  Parallel Inheritance Hierarchies – caused when
                    you need to create a subclass of one object
                    because you created a subclass of another
                  Lazy Class – classes that do not do much and do
                    not pay for their extra weight
                  Speculative Generality – constructs built for the
                    sake of possibility later
Refactoring

   Code “smells” as indicators
       More “smells”
                  Temporary Field
                  Message Chains – object asking for object that
                     asks for another object...
                  Middle Man – directors in place but serving no
                     real purpose
                  Inappropriate Intimacy – classes should not deal
                     too much with each others private parts
                  Data Class – getters and setters, but nothing else
                  Comments – where comments are used as
                     deodorant to cover a bad smell
Refactoring

   Tests and Refactoring
       You cannot properly refactor without tests in place.
       Writing tests is the first step of refactoring, and
        should happen as you write the code in the first
        place. (or before, as with TDD)
Refactoring

   Thank you


                   Adam Culp
            http://www.geekyboy.com
           http://github.com/adamculp
                Twitter @adamculp

More Related Content

PPTX
Azure Availability Options
PPTX
Cloud computing
PPTX
Mapa conceptual Arquitectura Cliente/Servidor SAIA Pag. Web
PPTX
Virtualization
PDF
MVVM in iOS presentation
PPTX
CCI2018 - Azure Network - Security Best Practices
PDF
Azure SQL Database
PPTX
Cloud migration
Azure Availability Options
Cloud computing
Mapa conceptual Arquitectura Cliente/Servidor SAIA Pag. Web
Virtualization
MVVM in iOS presentation
CCI2018 - Azure Network - Security Best Practices
Azure SQL Database
Cloud migration

Similar to Refactoring PHP (20)

PPTX
Refactoring workshop
PPT
Code Refactoring
PDF
The Power Of Refactoring (4developers Krakow)
PPTX
Clean Code
PDF
The Power Of Refactoring (php|tek 09)
PPTX
Code smells
PDF
The Power Of Refactoring (PHPCon Italia)
PDF
Refactoring 2 The Max
PDF
Refactoring
PDF
Refactoring 101
PDF
high performance mysql
PPT
Refactoring Tips by Martin Fowler
PPTX
Code Refactoring
PDF
Refactoring 2TheMax (con ReSharper)
PPTX
Code reviews
PPTX
Code Refactoring using rails
PPTX
Speeding up web_application
PDF
Bade Smells in Code
PDF
Refactoring: Improve the design of existing code
PPTX
mehdi-refactoring.pptx
Refactoring workshop
Code Refactoring
The Power Of Refactoring (4developers Krakow)
Clean Code
The Power Of Refactoring (php|tek 09)
Code smells
The Power Of Refactoring (PHPCon Italia)
Refactoring 2 The Max
Refactoring
Refactoring 101
high performance mysql
Refactoring Tips by Martin Fowler
Code Refactoring
Refactoring 2TheMax (con ReSharper)
Code reviews
Code Refactoring using rails
Speeding up web_application
Bade Smells in Code
Refactoring: Improve the design of existing code
mehdi-refactoring.pptx
Ad

More from Adam Culp (20)

PDF
Hypermedia
PDF
Putting legacy to REST with middleware
PDF
php-1701-a
PDF
Release your refactoring superpower
PDF
Managing Technical Debt
PDF
Developing PHP Applications Faster
PDF
Containing Quality
PDF
Debugging elephpants
PDF
Zend expressive workshop
PDF
Expressive Microservice Framework Blastoff
PDF
Foundations of Zend Framework
PDF
Accidental professional
PDF
Build great products
PDF
Does Your Code Measure Up?
PDF
Practical PHP Deployment with Jenkins
PDF
Virtualizing Development
PDF
Refactoring Legacy Code
PDF
Deprecated: Foundations of Zend Framework 2
PDF
Clean application development tutorial
PDF
Essential git for developers
Hypermedia
Putting legacy to REST with middleware
php-1701-a
Release your refactoring superpower
Managing Technical Debt
Developing PHP Applications Faster
Containing Quality
Debugging elephpants
Zend expressive workshop
Expressive Microservice Framework Blastoff
Foundations of Zend Framework
Accidental professional
Build great products
Does Your Code Measure Up?
Practical PHP Deployment with Jenkins
Virtualizing Development
Refactoring Legacy Code
Deprecated: Foundations of Zend Framework 2
Clean application development tutorial
Essential git for developers
Ad

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Digital-Transformation-Roadmap-for-Companies.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Building Integrated photovoltaic BIPV_UPV.pdf
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Monthly Chronicles - July 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks

Refactoring PHP

  • 1. Refactoring Presentation for: South Florida PHP Users Group By: Adam Culp Twitter: @adamculp http://www.geekyboy.com
  • 2. Refactoring  Based on popular “Refactoring” Improving The Design of Existing Code book, by Martin Fowler.  We will start in the code with an example.  Tips and descriptions will be handled during the example.  In the example we are tasked with creating an HTML representation of the customer statement for a movie rental.
  • 3. Refactoring  Movie class  With a new project we take a look at the current application a customer has requested to be changed.  We start with the Movie class.
  • 4. Refactoring  Rental class  Next we look at the Rental class.
  • 5. Refactoring  Customer class p1  With a new project we take a look at the current app that is about to be changed.  Next we look at part 1 of the Customer class.
  • 6. Refactoring  Customer class p2  With a new project we take a look at the current app that is about to be changed.  Next we look at part 2 of the Customer class.
  • 7. Refactoring  Not terrible for a small quick and dirty application, but if part of a larger application we have problems.  Customer class doing far too much, and statement method is too long.  Impossible to reuse the statement for an HTML representation.  The customer also wants to be able to change how they classify movies.  As experienced developers we know that customers will always come back six months from now and want it changed in some way.
  • 8. Refactoring  Rewrite vs Refactor  Budget constraint  Time constraint  Rewrite is an excuse to NOT dig in someone's code  Refactor is the best teacher
  • 9. Refactoring  First step  Build tests based on current “working” application, so we can verify if we break something.  Pre-load database with data or create fixtures needed for tests, to ensure we have a constant to compare test results.  If we are not starting with a working application you must first get the application working prior to refactoring. Do not try to refactor a broken application or you risk breaking it further, and end up with a total rewrite.
  • 10. Refactoring  Step 1  Extract Method to move switch statement to its own method.
  • 11. Refactoring  Step 2  Variable names should make sense. (each > rental, thisAmount > result)
  • 12. Refactoring  Step 3  Extract Method amountFor() to Rental class. It uses information from Rental, but no information from Customer. We rename it to more meaningful getCharge().
  • 13. Refactoring  Step 4  Call getCharge() directly from statement() instead of using amountFor() as a middle man method.
  • 14. Refactoring  Step 5  Replace Temp with Query - Cleanup of call to getCharge(). Eliminate temp variables by calling method direct. Less maintenance.
  • 15. Refactoring  Step 6  Extract Method and move frequentRenterPoints to its own method in Rental class where it logically belongs.
  • 16. Refactoring  Step 7  We do Replace Temp with Query for totalAmount. Temp variables can be a problem and really only serve a purpose within their own routine and encourage long, complex routines.
  • 17. Refactoring  Step 8  Also do Replace Temp with Query on frequentRentalPoints to eliminate more temp variables.
  • 18. Refactoring  Step 9  We are now able to create the HTML version of the statement, and we rename the original to represent a Text version.
  • 19. Refactoring  Recap  Most refactoring reduces code, while we increased it for this example.  We also duplicated a loop to make it happen 4 times instead of once. (getFrequentRenterPoints, getTotalCharge, getTotalFrequentRenterPoints)  Optimizing and refactoring are different actions. Refactor first, then optimize later if needed.  More refactoring could further clean up Text and HTML statements to DRY it up. (split out header/footer, etc.)  Still need more refactoring to allow classification changes by the customer.
  • 20. Refactoring  Step 10  Move Method on getCharge() because it uses data in Movie, so should be in the same class as the data.
  • 21. Refactoring  Step 11  Replace Conditional with Polymorphism on getCharge() because it uses data in Movie, so should be in the same class as the data.
  • 22. Refactoring  Two hats  Adding Function Hat  Refactoring Hat First we add functionality, they we refactor it, then add more functionality, ...
  • 23. Refactoring  Why Refactor?  Without refactoring code decays  As code is changed it loses structure, making it harder to see design  Regular refactoring preserves design  Poorly designed = more code due to duplication  Reduced code = easier to maintain  Reduced code = easier to understand  Helps find bugs  Helps us program faster
  • 24. Refactoring  When to Refactor?  Should not set aside time to refactor, it is just something we do in short bursts  Refactor because you want to do something, and refactoring helps you do it  Often a quick refactor can help developing new functionalities move faster  Rule of Three → see next slide
  • 25. Refactoring  Rule of Three  When you add function  Helps to learn code you need to modify  Changes code that prevents the addition  When you need to fix a bug  Make code more understandable  Usually highlights the bug  During code review  Code looks good to developer, but maybe not to the team.  More concrete results
  • 26. Refactoring  What do I tell my manager?  For a tech saavy manager it may not be hard to explain the benefits  Quality centric manager stress quality aspects  Perhaps introduce it as a review process  There are many resources on Google about value of reviews, inspections, or software development process  Schedule driven … Don't tell (controversial?)  Find a way to work it into scheduling.  Overall it saves time, but some will never “see” it
  • 27. Refactoring  Code “smells” as indicators  Bad code “smells” and is a great way of indicating it is time for a refactor.  Duplicate Code  Long Method  Large Class  Long Parameter (argument) List  Divergent Change - if change is needed in multiple areas to accommodate a change  Shotgun Surgery – change causes ripple of other needed changes  Feature Envy – a method seems to use another class instead of the one it is in
  • 28. Refactoring  Code “smells” as indicators  More “smells”  Data Clumps – if data items tend to accompany one another  Primitive Obsession  Switch statements  Parallel Inheritance Hierarchies – caused when you need to create a subclass of one object because you created a subclass of another  Lazy Class – classes that do not do much and do not pay for their extra weight  Speculative Generality – constructs built for the sake of possibility later
  • 29. Refactoring  Code “smells” as indicators  More “smells”  Temporary Field  Message Chains – object asking for object that asks for another object...  Middle Man – directors in place but serving no real purpose  Inappropriate Intimacy – classes should not deal too much with each others private parts  Data Class – getters and setters, but nothing else  Comments – where comments are used as deodorant to cover a bad smell
  • 30. Refactoring  Tests and Refactoring  You cannot properly refactor without tests in place.  Writing tests is the first step of refactoring, and should happen as you write the code in the first place. (or before, as with TDD)
  • 31. Refactoring  Thank you Adam Culp http://www.geekyboy.com http://github.com/adamculp Twitter @adamculp