Professional Refactoring

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    7 Favorites

    Professional Refactoring - Presentation Transcript

    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(\"DELETE FROM $this->tablename WHERE id = %d AND lang = '%s'\", $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(\"DELETE FROM $this->tablename WHERE id = %d AND lang = '%s'\", $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(\"DELETE FROM $this->tablename WHERE id = %d AND lang = '%s'\", $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(\"DELETE FROM %s WHERE id = %d AND lang = '%s'\", $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(\"DELETE FROM %s WHERE id = %d AND lang = '%s'\", $this->tablename, $stopword_id, $this->language); $this->db->query($delete); } Mayflower GmbH 2009 24
    25. Extraction public function remove($stopword_id) { $delete = sprintf(\"DELETE FROM %s WHERE id = %d AND lang = '%s'\", $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(\"DELETE FROM %s WHERE id = %d AND lang = '%s'\", $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(\"DELETE FROM %s WHERE id = %d AND lang = '%s'\", $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

    + Mayflower GmbHMayflower GmbH, 6 months ago

    custom

    1766 views, 7 favs, 8 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1766
      • 1390 on SlideShare
      • 376 from embeds
    • Comments 0
    • Favorites 7
    • Downloads 59
    Most viewed embeds
    • 255 views on http://blog.thinkphp.de
    • 97 views on http://www.planet-php.net
    • 10 views on http://planet-php.org
    • 9 views on http://www.planet-php.org
    • 2 views on http://xss.yandex.net

    more

    All embeds
    • 255 views on http://blog.thinkphp.de
    • 97 views on http://www.planet-php.net
    • 10 views on http://planet-php.org
    • 9 views on http://www.planet-php.org
    • 2 views on http://xss.yandex.net
    • 1 views on http://feed.bmaron.net
    • 1 views on http://www.iweb34.com
    • 1 views on http://planet-php.net

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories