the power of refactoring (4developers krakow)

39
4developers, Krakow, March 7 2009 Stefan Koopmanschap The Power of Refactoring

Upload: stefan-koopmanschap

Post on 11-Jun-2015

1.824 views

Category:

Technology


2 download

DESCRIPTION

The Power of Refactoring presentation I gave at 4developers conference in Krakow, Poland om March 7th.

TRANSCRIPT

Page 1: The Power Of Refactoring (4developers Krakow)

4developers, Krakow, March 7 2009Stefan Koopmanschap

The Power of Refactoring

Page 2: The Power Of Refactoring (4developers Krakow)

What will we do?

What is refactoring?

Why refactoring?

Requirements

HOWTO

2

Page 3: The Power Of Refactoring (4developers Krakow)

Stefan KoopmanschapWho is he?

3

Page 4: The Power Of Refactoring (4developers Krakow)

About me

Professional Services Consultant at Ibuildings

10 years of PHP experience

auto didact

married, 2 kids, 6 cats, a bunch of fish

Apple lover

music addict

symfony advocate

4

Page 5: The Power Of Refactoring (4developers Krakow)

RefactoringWhat is it?

5

Page 6: The Power Of Refactoring (4developers Krakow)

Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a 'refactoring') does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it's less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.(Martin Fowler - Refactoring.com)

6

Page 7: The Power Of Refactoring (4developers Krakow)

Code refactoring is the process of changing a computer program's code to make it amenable to change, improve its readability, or simplify its structure, while preserving its existing functionality.(Wikipedia)

7

Page 8: The Power Of Refactoring (4developers Krakow)

What

Code refactoring is the process of changing a computer program's internal structure without modifying its external behavior or existing functionality.

8

Page 9: The Power Of Refactoring (4developers Krakow)

Why

This is usually done to improve code readability, simplify code structure, change code to adhere to a given programming paradigm, improve maintainability, or improve extensibility.

9

Page 10: The Power Of Refactoring (4developers Krakow)

Improve readability

10

Page 11: The Power Of Refactoring (4developers Krakow)

Improve maintainability

11

Page 12: The Power Of Refactoring (4developers Krakow)

Extend/change/implement

12

Page 13: The Power Of Refactoring (4developers Krakow)

Is it the holy grail?

13

Page 14: The Power Of Refactoring (4developers Krakow)

Rewrite

A rewrite in computer programming is the act or result of re-implementing a large portion of existing functionality without re-use of its source code. When the rewrite is not using existing code at all, it is common to speak of a rewrite from scratch. When instead only parts are re-engineered, which have otherwise become complicated to handle or extend, then it is more precise to speak of code refactoring.(Wikipedia)

14

Page 15: The Power Of Refactoring (4developers Krakow)

Refactor vs Rewrite

15

Page 16: The Power Of Refactoring (4developers Krakow)

Refactor vs Rewrite

Refactor whenever it is possible

15

Page 17: The Power Of Refactoring (4developers Krakow)

Refactor vs Rewrite

Refactor whenever it is possible

Big change? Try to cut it into smaller units

15

Page 18: The Power Of Refactoring (4developers Krakow)

Refactor vs Rewrite

Refactor whenever it is possible

Big change? Try to cut it into smaller units

Still have to break the API? Go ahead, and rewrite!

15

Page 19: The Power Of Refactoring (4developers Krakow)

Refactor vs Rewrite

Refactor whenever it is possible

Big change? Try to cut it into smaller units

Still have to break the API? Go ahead, and rewrite!

Don’t take the decision in 5 minutes

15

Page 20: The Power Of Refactoring (4developers Krakow)

Refactor vs Rewrite

Refactoring takes less effortNice for you

Better for the quality of your work

Breaking the API is more workEnsure all calling code is altered as well

Update your unit tests

Update your documentation

16

Page 21: The Power Of Refactoring (4developers Krakow)

Requirementsfor successful refactoring

17

Page 22: The Power Of Refactoring (4developers Krakow)

Requirements

Codebase knowledge

Structured API

Unit testing

18

Page 23: The Power Of Refactoring (4developers Krakow)

Codebase knowledge

Know what to change quickly

Be aware of your changes

Be aware of impact of your changes on other components

19

Page 24: The Power Of Refactoring (4developers Krakow)

Structured API

Refactoring DOES NOT REPLACE design!

You need a good API that allows internal changes

Additions to existing API’s should not change existing API

Keep small “units”

20

Page 25: The Power Of Refactoring (4developers Krakow)

Unit testing

21

Page 26: The Power Of Refactoring (4developers Krakow)

Unit testing

Unit testing is your first point of QA

They safeguard you from unexpected side effects

Test - Change - Test

22

Page 27: The Power Of Refactoring (4developers Krakow)

An exampleuser authentication

23

Page 28: The Power Of Refactoring (4developers Krakow)

A test-driven approach

24

Page 29: The Power Of Refactoring (4developers Krakow)

My first implementation

25

Page 30: The Power Of Refactoring (4developers Krakow)

My first implementation

25

Page 31: The Power Of Refactoring (4developers Krakow)

A more dynamic version

26

Page 32: The Power Of Refactoring (4developers Krakow)

A more dynamic version

26

Page 33: The Power Of Refactoring (4developers Krakow)

Tips and tricks

27

Page 34: The Power Of Refactoring (4developers Krakow)

Some tips and tricks

Write unit tests before you start refactoring

Code documentation (phpDoc) is your friend

Don’t trust your IDE’s refactoring options

28

Page 35: The Power Of Refactoring (4developers Krakow)

Will you refactor now?

29

Page 36: The Power Of Refactoring (4developers Krakow)

Looking back

What is refactoring?

Why do we want to refactor?

Is it the holy grail?

Refactoring vs Rewriting

Requirements

How to do it

Tips and tricks

30

Page 37: The Power Of Refactoring (4developers Krakow)

Readability: http://www.flickr.com/photos/vipulmathur/471634239/ (by-nc-sa/2.0)Maintainability:http://www.flickr.com/photos/comicbase/2117680144/ (by-nc-sa/2.0)Extend:http://www.flickr.com/photos/auntiep/221951988/ (by-nc-sa/2.0)Holy Grail:http://www.flickr.com/photos/joebeone/153921142/ (by/2.0)Würst test ever:http://www.flickr.com/photos/pingles/1341229619/ (by-nc-nd/2.0)

31

Creative Commons Rocks!

Page 38: The Power Of Refactoring (4developers Krakow)

Questions ?

32

Page 39: The Power Of Refactoring (4developers Krakow)

Thank you!

Contact details:

Stefan Koopmanschaphttp://www.leftontheweb.com/

http://www.ibuildings.com/http://www.twitter.com/skoop

http://www.slideshare.net/skoop