Professional Refactoring

33
Mayflower GmbH 2009 Professional Refactoring International PHP Conference 2009 Spring Edition 1

description

 

Transcript of Professional Refactoring

Page 1: Professional Refactoring

Mayflower GmbH 2009

Professional RefactoringInternational PHP Conference 2009Spring Edition

1

Page 2: Professional Refactoring

Mayflower GmbH 2009

• 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

2

Page 3: Professional Refactoring

Mayflower GmbH 2009

Who are you?

• What are you doing?• What‘s your team size?• Using MVC?• Who‘s using Continous Integration?

• PHPUnit?• 80% code coverage?

3

Page 4: Professional Refactoring

Mayflower GmbH 2009

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?

4

Page 5: Professional Refactoring

Mayflower GmbH 2009

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

5

Page 6: Professional Refactoring

Mayflower GmbH 2009

Why especially PHP?

Code Aging!6

(c) www.medrehab.com

Page 7: Professional Refactoring

Mayflower GmbH 2009

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 ...

7

Page 8: Professional Refactoring

Mayflower GmbH 2009

... because it worked!

8

Page 9: Professional Refactoring

Mayflower GmbH 2009

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

9

Page 10: Professional Refactoring

Mayflower GmbH 2009

Management point of view

10

rising frequency

Dead end!

Benefit per Change Request

Costs per Change Request

Page 11: Professional Refactoring

Mayflower GmbH 2009

Start refactoring now!11

Page 12: Professional Refactoring

Mayflower GmbH 2009

But hey, stop!

12

Page 13: Professional Refactoring

Mayflower GmbH 2009

Don‘t refactor ...

13

• weeks before a important release

• only with a lot of junior developers

• parallel with development tasks

Page 14: Professional Refactoring

Mayflower GmbH 2009

Before starting refactoring

14

• define a coding standard• avoids spaghetti code• speeds up maintainability• improves productivity

• fix your API specs• complete your documentation

Page 15: Professional Refactoring

Mayflower GmbH 2009

During the refactoring...

15

• 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

Page 16: Professional Refactoring

Mayflower GmbH 2009

About Unittests

16

• Testing is essential during refactoring• Problems

• most of old code isn‘t „unittestable“

• API breaks during refacotoring• Solution

• Selenium tests instead• iterative refactoring

Page 17: Professional Refactoring

Mayflower GmbH 2009

Okay, let‘s start!17

(c) BMW AG

Page 18: Professional Refactoring

Mayflower GmbH 2009

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

18

Page 19: Professional Refactoring

Mayflower GmbH 2009

Forms of refactoring

19

•Renaming

•Extraction

•Changing signature

•Pull up / Pull down

Page 20: Professional Refactoring

Mayflower GmbH 2009

Renaming

20

/** * 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); }

Page 21: Professional Refactoring

Mayflower GmbH 2009

Renaming

21

/** * 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); }

Page 22: Professional Refactoring

Mayflower GmbH 2009

Restructuring

22

/** * 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); }

Page 23: Professional Refactoring

Mayflower GmbH 2009

Restructuring

23

/** * 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); }

Page 24: Professional Refactoring

Mayflower GmbH 2009

Extraction

24

/** * 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); }

Page 25: Professional Refactoring

Mayflower GmbH 2009

Extraction

25

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) }

Page 26: Professional Refactoring

Mayflower GmbH 2009

Changing signature

26

/** * 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); }

Page 27: Professional Refactoring

Mayflower GmbH 2009

Changing signature

27

/** * 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); }

Page 28: Professional Refactoring

Mayflower GmbH 2009

Pull up / Pull down

28

x

Page 29: Professional Refactoring

Mayflower GmbH 2009

Pull up / Pull down

29

x

Page 30: Professional Refactoring

Mayflower GmbH 2009

Pull up / Pull down

30

xx

x

Page 31: Professional Refactoring

Mayflower GmbH 2009

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

31

Page 32: Professional Refactoring

Mayflower GmbH 2009 32

Any questions?

Page 33: Professional Refactoring

Mayflower GmbH 2009

Thank you very much for your attention!

Thorsten RinneMayflower GmbHMannhardtstraße 6D-80538 München+49 (0) 89 24 20 54 - [email protected]

33