Everything you Wanted to Know About Refactoring

39
Everything you Wanted to Know About Refactoring but were Afraid to Ask! By Gary Short Technical Evangelist Developer Express

description

This presentations will tell you everything you every wanted to know about refactoring... probably :-)

Transcript of Everything you Wanted to Know About Refactoring

Page 1: Everything you Wanted to Know About Refactoring

Everything you Wanted to Know About Refactoring but were Afraid to Ask!

By Gary ShortTechnical EvangelistDeveloper Express

Page 2: Everything you Wanted to Know About Refactoring

Agenda

• What is Refactoring?• Why Should I Refactor?• How do I know that I have to refactor?• How Should I Refactor?• How can Tooling Help Me?• Advanced Refactoring.

Page 3: Everything you Wanted to Know About Refactoring

Why do I Need to Know this?

• Refactoring is a professional dev skill• Therefore it’s a must have skill for you!• Helps to keep technical debt under control• Technical debt kills projects.

Page 4: Everything you Wanted to Know About Refactoring

Introduction

• Gary Short– Technical Evangelist for Developer Express– Community Evangelist for... Well, me really! – Microsoft MVP C#

• Where do I hang out?– http://www.garyshort.org (blog)– http://www.twitter.com/garyshort– http://www.sodthis.com (podcast).

Page 5: Everything you Wanted to Know About Refactoring

How About you Guys?

Page 6: Everything you Wanted to Know About Refactoring

What is Refactoring?

• Knowledge says refactoring is...– the process of changing a computer program's internal

structure • without modifying its external functional behavior • or existing functionality,

– in order to improve • internal non-functional properties of the software,

– for example to improve code readability, – to simplify code structure, – to change code to adhere to a given programming paradigm,– to improve maintainability, – or to improve extensibility.

Page 7: Everything you Wanted to Know About Refactoring

Ooo, Hold on, How’d you do That?

• the process of changing a computer program's internal structure – without modifying its external functional behavior – or existing functionality,

• Unit Testing– is a software design and development method where the

programmer gains confidence that individual units of source code are fit for use

– modern applications include the use of a test framework such as nUnit

– each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation.

Page 8: Everything you Wanted to Know About Refactoring

Quick Example

•October 17, 1989, the 7.1 magnitude Loma Prieta earthquake causes 68 deaths•The earthquake reminded the world that the San Francisco Bay region remains vulnerable•If a Richter magnitude 8.0 or greater earthquake centered near the Bridge, there would be a substantial risk of impending collapse •Retrofit starts 1997•Still ongoing today•Cost ~ half a billion dollars.

Biggest Refactoring Eva!!

Page 9: Everything you Wanted to Know About Refactoring

Why Should I Refactor?

• Refactoring is our way of paying off our “Technical Debt”

• Technical what?!– is a metaphor developed by Ward Cunningham• doing things the quick and dirty way sets us up with a

technical debt, which is similar to a financial debt• incurs interest payments, which come in the form of

the extra effort that we have to do in future development.

Page 10: Everything you Wanted to Know About Refactoring

Financial Debt Isn’t All Bad

Borrow

Purchase

Build

Sell

Repay

Page 11: Everything you Wanted to Know About Refactoring

Technical Debt Isn’t all Bad Either

Borrow

Design

Build

Deploy

Refactor

Page 12: Everything you Wanted to Know About Refactoring

Refactoring Pays Back this Debt

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec0

102030405060708090

100110120130

No Refactoring

Page 13: Everything you Wanted to Know About Refactoring

Refactoring Pays Back this Debt

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec0

0.5

1

1.5

2

Refactoring

Page 14: Everything you Wanted to Know About Refactoring

How Do I Know When To Refactor?

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov0

20

40

60

80

100

Burn Down Graph

Page 15: Everything you Wanted to Know About Refactoring

How Do I Know When To Refactor?

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov0

20

40

60

80

100

Burn Down Graph

Page 16: Everything you Wanted to Know About Refactoring

How Do I Know When To Refactor?

0 2 4 6 8 10 120

2

4

6

8

10

12

14

Backlog Items Per Iteration

Page 17: Everything you Wanted to Know About Refactoring

How Do I Know When To Refactor?

0 2 4 6 8 10 120

2

4

6

8

10

12

14

Backlog Items Per Iteration

Page 18: Everything you Wanted to Know About Refactoring

How Do I Know When To Refactor?

0 2 4 6 8 10 120

2

4

6

8

10

12

14

Number of Tests Per Back Log Item

Page 19: Everything you Wanted to Know About Refactoring

How Do I Know When To Refactor?

0 2 4 6 8 10 120

2

4

6

8

10

12

14

Number of Tests Per Back Log Item

Page 20: Everything you Wanted to Know About Refactoring

How Should I Refactor?

• Ask yourself the following questions– Is my code readable? – Is my code abstract?

• Anything more is rearranging the deck chairs on the Titanic– It gives you a sense of doing something– But ultimately it’s pointless.

Page 21: Everything you Wanted to Know About Refactoring

Refactoring for Readability #1

Page 22: Everything you Wanted to Know About Refactoring

Refactoring for Readability #1

Page 23: Everything you Wanted to Know About Refactoring

Refactoring for Readability #2

Page 24: Everything you Wanted to Know About Refactoring

Refactoring for Readability #3

Page 25: Everything you Wanted to Know About Refactoring

Refactoring for Readability #4

Page 26: Everything you Wanted to Know About Refactoring

Refactoring for Readability #5

Page 27: Everything you Wanted to Know About Refactoring

Refactoring to Make Abstract #1

Page 28: Everything you Wanted to Know About Refactoring

Refactoring to Make Abstract #2

Page 29: Everything you Wanted to Know About Refactoring

Refactoring to Make Abstract #2

Page 30: Everything you Wanted to Know About Refactoring

Refactoring to Make Abstract #3

Page 31: Everything you Wanted to Know About Refactoring

Can Tooling Help me?

• Yes, in the following ways– Refactoring can be repetitive• Repetitive tasks bore humans

– Resulting in mistakes

– Solutions can contain many projects• Many classes with many projects can contain the same

method calls– This makes them hard to find and easy to miss

– Tools can suggest refactors that you might not know exist.

Page 32: Everything you Wanted to Know About Refactoring

Tooling Demo

Page 33: Everything you Wanted to Know About Refactoring

Refactoring to Patterns #1

Page 34: Everything you Wanted to Know About Refactoring

Refactoring to Patterns #1

Page 35: Everything you Wanted to Know About Refactoring

Refactoring to Patterns #2

Page 36: Everything you Wanted to Know About Refactoring

Refactoring to Patterns #3

Page 37: Everything you Wanted to Know About Refactoring

Summary

• Refactoring is changing the structure, but not the functionality of your application

• Refactoring is done to pay back the “Technical Debt”

• Refactor by asking is your code– Readable– Abstract

• Invest in some tooling.

Page 38: Everything you Wanted to Know About Refactoring

Further Reading

• Fowler, Martin (1999). Refactoring. Improving the Design of Existing Code. Addison-Wesley. ISBN 0-201-48567-2.

• Wake, William C. (2003). Refactoring Workbook. Addison-Wesley. ISBN 0-321-10929-5.

• Feathers, Michael C (2004). Working Effectively with Legacy Code. Prentice Hall. ISBN 0-13-117705-2.

• Kerievsky, Joshua (2004). Refactoring To Patterns. Addison-Wesley. ISBN 0-321-21335-1.

• Arsenovski, Danijel (2008). Professional Refactoring in Visual Basic. Wrox. ISBN 0-47-017979-1.

Page 39: Everything you Wanted to Know About Refactoring

Questions

• http://www.garyshort.org• http://www.twitter.com/garyshort• http://www.sodthis.com• [email protected]