Professional Refactoring

Mayflower GmbH
Mayflower GmbHSoftware developers, PHP folks at Mayflower GmbH
Professional Refactoring
International PHP Conference 2009
Spring Edition




                                    Mayflower GmbH 2009   1
• Senior Developer / Team Lead at
  Mayflower GmbH
 • Reporting and Rating Apps
 • QA and PHP 5 Migration
   consultings
• PHP since 1999 (3.0.16)
• phpMyFAQ since 2001



                                    Mayflower GmbH 2009   2
Who are you?

 • What are you doing?
 • What‘s your team size?
 • Using MVC?
 • Who‘s using Continous Integration?
   • PHPUnit?
   • 80% code coverage?




                                        Mayflower GmbH 2009   3
Your projects...
• What‘s your average project lifetime?
• Is there PHP code more than 5 years old?
• How many lines of code?
• How many change requests per year?
• Has there been a specification?
• Were all features in the first
  release as specified?




                                             Mayflower GmbH 2009   4
What is Refactoring?


 „Refactoring is a disciplined technique for
 restructuring an existing body of code, altering
 its internal structure without changing its
 external behavior.“
                        -Martin Fowler, www.refactoring.com




                                                              Mayflower GmbH 2009   5
Why especially PHP?

                      (c) www.medrehab.com




        Code Aging!
                                   Mayflower GmbH 2009   6
A PHP Project in 2000 ...
• no coding standards, no PHPDoc
• no MVC, no Design Patterns
• if you were lucky, someone used a
  template system
• nobody cared about XSS or CSRF
  a lot of changes in business logics
• never got refactored,
  documentated or even tested ...



                                        Mayflower GmbH 2009   7
... because it worked!



                     Mayflower GmbH 2009   8
In the year 2009
• change requests get more and
  more expensive
• the bug rate is always
  increasing
• the development team
  motivation is decreasing
• requirement changes are
  almost impossible
• new team members need a lot
  of time to be productive
                                 Mayflower GmbH 2009   9
Management point of view

Costs per Change Request



                           rising frequency




                                         Dead end!




                                          Benefit per Change Request
                                                                      Mayflower GmbH 2009   10
Start refactoring now!
                    Mayflower GmbH 2009   11
But hey, stop!
                 Mayflower GmbH 2009   12
Don‘t refactor ...
• weeks before a
  important release
• only with a lot of junior
  developers
• parallel with
  development tasks




                              Mayflower GmbH 2009   13
Before starting refactoring

• define a coding standard
 • avoids spaghetti code
 • speeds up maintainability
 • improves productivity
• fix your API specs
• complete your documentation




                                Mayflower GmbH 2009   14
During the refactoring...
       • stay calm
       • take a lot of good developers and
         a bunch of juniors
       • write tests, tests, tests
       • don‘t let developer refactor their
         own code
       • don‘t let junior developers
         refactor alone

                                          Mayflower GmbH 2009   15
About Unittests
• Testing is essential during refactoring
• Problems
  • most of old code isn‘t
    „unittestable“
  • API breaks during refacotoring
• Solution
  • Selenium tests instead
  • iterative refactoring


                                            Mayflower GmbH 2009   16
(c) BMW AG




             Okay, let‘s start!
                              Mayflower GmbH 2009   17
Back to Martin Fowler


 „Refactoring is a disciplined technique for
 restructuring an existing body of code, altering
 its internal structure without changing its
 external behavior.“
                        -Martin Fowler, www.refactoring.com




                                                              Mayflower GmbH 2009   18
Forms of refactoring

•Renaming
•Extraction
•Changing signature
•Pull up / Pull down

                       Mayflower GmbH 2009   19
Renaming

     /**
       * Remove a word from the stop word dictionary
       *
       * @param integer $id
       *
       * @return void
       */
     public function remove($id)
     {
          $sql = sprintf(quot;DELETE FROM $this->tablename WHERE id = %d AND lang =
 '%s'quot;, $id, $this->language);

         $this->db->query($sql);
     }




                                                                           Mayflower GmbH 2009   20
Renaming

     /**
       * Remove a word from the stop word dictionary
       *
       * @param integer $stopword_id ID of the stop word
       *
       * @return void
       */
     public function remove($stopword_id)
     {
          $delete = sprintf(quot;DELETE FROM $this->tablename WHERE id = %d AND lang =
 '%s'quot;, $stopword_id, $this->language);

         $this->db->query($delete);
     }




                                                                           Mayflower GmbH 2009   21
Restructuring

      /**
        * Remove a word from the stop word dictionary
        *
        * @param integer $stopword_id ID of the stop word
        *
        * @return void
        */
      public function remove($stopword_id)
      {
           $delete = sprintf(quot;DELETE FROM $this->tablename WHERE id = %d AND lang =
  '%s'quot;, $stopword_id, $this->language);

          $this->db->query($delete);
      }




                                                                            Mayflower GmbH 2009   22
Restructuring
   /**
     * Remove a word from the stop word dictionary
     *
     * @param integer $stopword_id ID of the stop word
     *
     * @return void
     */
   public function remove($stopword_id)
   {
        $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;,
            $this->tablename,
            $stopword_id,
            $this->language);

       $this->db->query($delete);
   }




                                                                            Mayflower GmbH 2009   23
Extraction
   /**
     * Remove a word from the stop word dictionary
     *
     * @param integer $stopword_id ID of the stop word
     *
     * @return void
     */
   public function remove($stopword_id)
   {
        $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;,
            $this->tablename,
            $stopword_id,
            $this->language);

       $this->db->query($delete);
   }




                                                                            Mayflower GmbH 2009   24
Extraction


   public function remove($stopword_id)
   {
       $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;,
           $this->tablename,
           $stopword_id,
           $this->language);

       $this->_execute($delete);
   }

   private function _execute($query)
   {
       return $this->db->query($query)
   }




                                                                           Mayflower GmbH 2009   25
Changing signature
   /**
     * Remove a word from the stop word dictionary
     *
     * @param integer $id ID of the stop word
     *
     * @return void
     */
   public function remove($stopword_id)
   {
        $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;,
            $this->tablename,
            $stopword_id,
            $this->language);

       $this->db->query($delete);
   }




                                                                            Mayflower GmbH 2009   26
Changing signature
   /**
     * Remove a word from the stop word dictionary
     *
     * @param integer $stopword_id ID of the stop word
     * @param boolean $logging     Log removal? Default: false
     *
     * @return void
     */
   public function remove($stopword_id, $logging = false)
   {
        $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;,
            $this->tablename,
            $stopword_id,
            $this->language);

       if ($logging) {
           $this->_logAction('removal', $stopword_id);
       }

       $this->db->query($delete);
   }

                                                                            Mayflower GmbH 2009   27
Pull up / Pull down




            x



                      Mayflower GmbH 2009   28
Pull up / Pull down




            x



                      Mayflower GmbH 2009   29
Pull up / Pull down




           x


       x       x



                      Mayflower GmbH 2009   30
Tips & Tricks
• Always add PHPDoc if it‘s missing
• Never trust automatic refactoring of IDEs
• Don‘t do refactoring for fun
• Write as much unittests as possible




                                              Mayflower GmbH 2009   31
Any questions?   Mayflower GmbH 2009   32
Thank you very much for your attention!



  Thorsten Rinne
  Mayflower GmbH
  Mannhardtstraße 6
  D-80538 München
  +49 (0) 89 24 20 54 - 31
  thorsten.rinne@mayflower.de

                                     Mayflower GmbH 2009   33
1 of 33

More Related Content

Similar to Professional Refactoring(20)

Joomla Template DevelopmentJoomla Template Development
Joomla Template Development
Linda Coonen1.8K views
Things to consider for testable CodeThings to consider for testable Code
Things to consider for testable Code
Frank Kleine1.2K views
A Z Introduction To Ruby On RailsA Z Introduction To Ruby On Rails
A Z Introduction To Ruby On Rails
railsconf1.2K views
A-Z Intro To RailsA-Z Intro To Rails
A-Z Intro To Rails
Robert Dempsey25.4K views
Intro To DjangoIntro To Django
Intro To Django
Udi Bauman2.6K views
Practical Groovy Domain-Specific LanguagesPractical Groovy Domain-Specific Languages
Practical Groovy Domain-Specific Languages
Guillaume Laforge1.2K views
GaeGae
Gae
guest0e51364600 views
The Dojo Build SystemThe Dojo Build System
The Dojo Build System
klipstein1.7K views
Bti1022 lab sheet 3Bti1022 lab sheet 3
Bti1022 lab sheet 3
alish sha301 views
How to build the WebHow to build the Web
How to build the Web
Simon Willison6.4K views
FuzzyDebugger.pdfFuzzyDebugger.pdf
FuzzyDebugger.pdf
ritviktanksalkar11 view
SatchmoSatchmo
Satchmo
DjangoCon2008557 views
AngularJS in practiceAngularJS in practice
AngularJS in practice
jhoguet749 views

More from Mayflower GmbH(20)

Why and what is goWhy and what is go
Why and what is go
Mayflower GmbH1.2K views
Agile Anti-PatternsAgile Anti-Patterns
Agile Anti-Patterns
Mayflower GmbH10.2K views
JavaScript Days 2015: SecurityJavaScript Days 2015: Security
JavaScript Days 2015: Security
Mayflower GmbH5.1K views
Vom Entwickler zur FührungskraftVom Entwickler zur Führungskraft
Vom Entwickler zur Führungskraft
Mayflower GmbH3.3K views
Produktive teamsProduktive teams
Produktive teams
Mayflower GmbH2K views
Usability im webUsability im web
Usability im web
Mayflower GmbH2.1K views
Rewrites überlebenRewrites überleben
Rewrites überleben
Mayflower GmbH2K views
JavaScript SecurityJavaScript Security
JavaScript Security
Mayflower GmbH3.3K views
Responsive WebdesignResponsive Webdesign
Responsive Webdesign
Mayflower GmbH2.3K views
Pair Programming MythbustersPair Programming Mythbusters
Pair Programming Mythbusters
Mayflower GmbH4.1K views
Shoeism - Frau im GlückShoeism - Frau im Glück
Shoeism - Frau im Glück
Mayflower GmbH2.4K views
Bessere Software schneller liefernBessere Software schneller liefern
Bessere Software schneller liefern
Mayflower GmbH1.8K views
Von 0 auf 100 in 2 SprintsVon 0 auf 100 in 2 Sprints
Von 0 auf 100 in 2 Sprints
Mayflower GmbH1.8K views
Piwik anpassen und skalierenPiwik anpassen und skalieren
Piwik anpassen und skalieren
Mayflower GmbH1.7K views

Recently uploaded(20)

Skilled Landscape ContractorSkilled Landscape Contractor
Skilled Landscape Contractor
EmmanuelRyker10 views
Car license plate holder.pdfCar license plate holder.pdf
Car license plate holder.pdf
JAWADIQBAL4027 views
Embracing Future ShockEmbracing Future Shock
Embracing Future Shock
Emiliano Soldi184 views
Digamma Company Presentation.pdfDigamma Company Presentation.pdf
Digamma Company Presentation.pdf
Jovan Vlajic30 views
Mythic Forge - catálogo online.pdfMythic Forge - catálogo online.pdf
Mythic Forge - catálogo online.pdf
mythicstlforge26 views
terms_2.pdfterms_2.pdf
terms_2.pdf
JAWADIQBAL4070 views
Effective Supervisory SkillEffective Supervisory Skill
Effective Supervisory Skill
Seta Wicaksana9 views
Project & Portfolio 1Project & Portfolio 1
Project & Portfolio 1
FullSail University33 views
ENT PPP.pptx.Undergraduate htttc.pptxENT PPP.pptx.Undergraduate htttc.pptx
ENT PPP.pptx.Undergraduate htttc.pptx
National Higher polytechnic institute, university of Bamenda 13 views
Greece opens countless opportunities for techiesGreece opens countless opportunities for techies
Greece opens countless opportunities for techies
Abhinav Immigration Services Pvt. Ltd.15 views
The Business Tycoons(March-2023) - Womens Day MagazineThe Business Tycoons(March-2023) - Womens Day Magazine
The Business Tycoons(March-2023) - Womens Day Magazine
Global India Business Forum14 views
How to use radio advertising effectively.pdfHow to use radio advertising effectively.pdf
How to use radio advertising effectively.pdf
Group Buy Seo Tools11 views
23BBA10204-ASHNISH KUMAR.pdf23BBA10204-ASHNISH KUMAR.pdf
23BBA10204-ASHNISH KUMAR.pdf
shreyajha73121 views
PROGRAMME.pdfPROGRAMME.pdf
PROGRAMME.pdf
HiNedHaJar63 views

Professional Refactoring

  • 1. Professional Refactoring International PHP Conference 2009 Spring Edition Mayflower GmbH 2009 1
  • 2. • Senior Developer / Team Lead at Mayflower GmbH • Reporting and Rating Apps • QA and PHP 5 Migration consultings • PHP since 1999 (3.0.16) • phpMyFAQ since 2001 Mayflower GmbH 2009 2
  • 3. Who are you? • What are you doing? • What‘s your team size? • Using MVC? • Who‘s using Continous Integration? • PHPUnit? • 80% code coverage? Mayflower GmbH 2009 3
  • 4. Your projects... • What‘s your average project lifetime? • Is there PHP code more than 5 years old? • How many lines of code? • How many change requests per year? • Has there been a specification? • Were all features in the first release as specified? Mayflower GmbH 2009 4
  • 5. What is Refactoring? „Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.“ -Martin Fowler, www.refactoring.com Mayflower GmbH 2009 5
  • 6. Why especially PHP? (c) www.medrehab.com Code Aging! Mayflower GmbH 2009 6
  • 7. A PHP Project in 2000 ... • no coding standards, no PHPDoc • no MVC, no Design Patterns • if you were lucky, someone used a template system • nobody cared about XSS or CSRF a lot of changes in business logics • never got refactored, documentated or even tested ... Mayflower GmbH 2009 7
  • 8. ... because it worked! Mayflower GmbH 2009 8
  • 9. In the year 2009 • change requests get more and more expensive • the bug rate is always increasing • the development team motivation is decreasing • requirement changes are almost impossible • new team members need a lot of time to be productive Mayflower GmbH 2009 9
  • 10. Management point of view Costs per Change Request rising frequency Dead end! Benefit per Change Request Mayflower GmbH 2009 10
  • 11. Start refactoring now! Mayflower GmbH 2009 11
  • 12. But hey, stop! Mayflower GmbH 2009 12
  • 13. Don‘t refactor ... • weeks before a important release • only with a lot of junior developers • parallel with development tasks Mayflower GmbH 2009 13
  • 14. Before starting refactoring • define a coding standard • avoids spaghetti code • speeds up maintainability • improves productivity • fix your API specs • complete your documentation Mayflower GmbH 2009 14
  • 15. During the refactoring... • stay calm • take a lot of good developers and a bunch of juniors • write tests, tests, tests • don‘t let developer refactor their own code • don‘t let junior developers refactor alone Mayflower GmbH 2009 15
  • 16. About Unittests • Testing is essential during refactoring • Problems • most of old code isn‘t „unittestable“ • API breaks during refacotoring • Solution • Selenium tests instead • iterative refactoring Mayflower GmbH 2009 16
  • 17. (c) BMW AG Okay, let‘s start! Mayflower GmbH 2009 17
  • 18. Back to Martin Fowler „Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.“ -Martin Fowler, www.refactoring.com Mayflower GmbH 2009 18
  • 19. Forms of refactoring •Renaming •Extraction •Changing signature •Pull up / Pull down Mayflower GmbH 2009 19
  • 20. Renaming /** * Remove a word from the stop word dictionary * * @param integer $id * * @return void */ public function remove($id) { $sql = sprintf(quot;DELETE FROM $this->tablename WHERE id = %d AND lang = '%s'quot;, $id, $this->language); $this->db->query($sql); } Mayflower GmbH 2009 20
  • 21. Renaming /** * Remove a word from the stop word dictionary * * @param integer $stopword_id ID of the stop word * * @return void */ public function remove($stopword_id) { $delete = sprintf(quot;DELETE FROM $this->tablename WHERE id = %d AND lang = '%s'quot;, $stopword_id, $this->language); $this->db->query($delete); } Mayflower GmbH 2009 21
  • 22. Restructuring /** * Remove a word from the stop word dictionary * * @param integer $stopword_id ID of the stop word * * @return void */ public function remove($stopword_id) { $delete = sprintf(quot;DELETE FROM $this->tablename WHERE id = %d AND lang = '%s'quot;, $stopword_id, $this->language); $this->db->query($delete); } Mayflower GmbH 2009 22
  • 23. Restructuring /** * Remove a word from the stop word dictionary * * @param integer $stopword_id ID of the stop word * * @return void */ public function remove($stopword_id) { $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;, $this->tablename, $stopword_id, $this->language); $this->db->query($delete); } Mayflower GmbH 2009 23
  • 24. Extraction /** * Remove a word from the stop word dictionary * * @param integer $stopword_id ID of the stop word * * @return void */ public function remove($stopword_id) { $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;, $this->tablename, $stopword_id, $this->language); $this->db->query($delete); } Mayflower GmbH 2009 24
  • 25. Extraction public function remove($stopword_id) { $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;, $this->tablename, $stopword_id, $this->language); $this->_execute($delete); } private function _execute($query) { return $this->db->query($query) } Mayflower GmbH 2009 25
  • 26. Changing signature /** * Remove a word from the stop word dictionary * * @param integer $id ID of the stop word * * @return void */ public function remove($stopword_id) { $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;, $this->tablename, $stopword_id, $this->language); $this->db->query($delete); } Mayflower GmbH 2009 26
  • 27. Changing signature /** * Remove a word from the stop word dictionary * * @param integer $stopword_id ID of the stop word * @param boolean $logging Log removal? Default: false * * @return void */ public function remove($stopword_id, $logging = false) { $delete = sprintf(quot;DELETE FROM %s WHERE id = %d AND lang = '%s'quot;, $this->tablename, $stopword_id, $this->language); if ($logging) { $this->_logAction('removal', $stopword_id); } $this->db->query($delete); } Mayflower GmbH 2009 27
  • 28. Pull up / Pull down x Mayflower GmbH 2009 28
  • 29. Pull up / Pull down x Mayflower GmbH 2009 29
  • 30. Pull up / Pull down x x x Mayflower GmbH 2009 30
  • 31. Tips & Tricks • Always add PHPDoc if it‘s missing • Never trust automatic refactoring of IDEs • Don‘t do refactoring for fun • Write as much unittests as possible Mayflower GmbH 2009 31
  • 32. Any questions? Mayflower GmbH 2009 32
  • 33. Thank you very much for your attention! Thorsten Rinne Mayflower GmbH Mannhardtstraße 6 D-80538 München +49 (0) 89 24 20 54 - 31 thorsten.rinne@mayflower.de Mayflower GmbH 2009 33