refactoring delphi 2005 win32

22
© 2001-2005 Craig Murphy Refactoring Delphi 2005 Win32

Upload: fayola

Post on 11-Feb-2016

56 views

Category:

Documents


0 download

DESCRIPTION

Refactoring Delphi 2005 Win32. Agenda. Motivation What is Refactoring? When to Refactor Refactoring Safely Refactoring using Delphi 2005 .net example; applies to Win32 Refactoring Using Delphi 5-7 Summary. Motivation. To look at refactoring support in Delphi 2005 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Refactoring Delphi 2005   Win32

© 2001-2005 Craig Murphy

RefactoringDelphi 2005 Win32

Page 2: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 2

Agenda

Motivation What is Refactoring? When to Refactor Refactoring Safely Refactoring using Delphi 2005

.net example; applies to Win32 Refactoring Using Delphi 5-7 Summary

Page 3: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 3

Motivation To look at refactoring support in Delphi 2005

To consider how “new” features can be used in earlier versions of Delphi If you’re not [knowingly] refactoring now, this session

will give you a flavour of what it’s all about Tool support for earlier versions of Delphi

Ultimately…to become more productive inside the IDE Avoid search’n’replace (and undo!) Avoid manually making a small change in more than

one place Avoid “stupid” cut’n’paste bugs

Page 4: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 4

What is Refactoring? “Refactoring is a disciplined technique for

restructuring an existing body of code, altering its internal structure without changing its external behaviour”

--- Martin Fowler

Improving the design of existing code

Minimising the chances of introducing “new” bugs

Page 5: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 5

What is Refactoring? Small, localised changes

…sometimes just changing one occurrence, other times changing many occurrences

…sometimes larger, more sweeping refactorings can be applied too

Can be applied by hand, manually Not recommended

not consistent not guaranteed to be repeatable

It’s very likely that you have already been refactoring, but didn’t realise it Everybody wants to improve the design of their existing

code

Page 6: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 6

When to Refactor When a piece of code is difficult to understand

Code smells, from the eXtreme Programming community, not exhaustive: Comments Duplicated Code Long Method Conditional logic, switch statement (simulated

inheritance) Large class Magic Numbers Name does not convey intent (uncommunicative name)

Page 7: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 7

Refactoring Safely How do we ensure that our refactorings have

not altered or broken the original functionality? Tests Repeatable Tests Automated, Repeatable Tests

Delphi 2005 ships with NUnit and DUnit Offers us an automated testing framework NUnit/DUnit for .net DUnit for Win32 (Delphi 5-7 can use DUnit)

Ideally, we should write tests before we refactor our code…

Page 8: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 8

Problems with Refactoring If it ain’t broke, don’t touch it?

Early design decisions may limit future development Performance issues Code reviews

What do you tell your manager? “Refactoring is a pointless exercise, we’ve shipped”

Analogies Car manufacturer Clean desk

You tell your manager that you are improving the code’s inherent value: code is an asset

For most of us, code pays our salary The ability to keep an eye on your assets is important

Page 9: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 9

Available RefactoringsDelphi 2005

Rename Parameter/Variable

Extract Method Declare Variable Declare Field Extract Resource String

Sync Edit Find Unit

Visual Studio 2005 (Whidbey)

Rename Extract Method Encapsulate Field Extract Interface Promote Local

Variable to Parameter

Remove Parameters Reorder Parameters

Page 10: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 10

demo

Refactoring Using Delphi 2005

Page 11: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 11

Refactoring Using Delphi 5-7 What tool support is there?

Productivity and IDE enhancers: Castalia 3

Supports Delphi 5, 6, 7, 8 and 2005 Faithful to Fowler

ModelMaker’s Code Explorer Supports Delphi 5, 6, 7, 8 and 2005

Page 12: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 12

Castalia 3 Offers a “Refactoring Explorer” similar to Delphi’s

Refactoring dock window:

Find References Rename Class Move Class Rename Method Extract Method

Add Parameter Rename Local Variable Inline Temporary

Variable/Replace temp with query Split Temporary Variable

Page 13: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 14

Summary Code is an asset, refactoring helps us improve

the value of that asset

Refactoring lets us extend the life of an application with very little extra cost

Refactoring and Test-Driven Development go hand-in-hand Bob Swart - Unit Testing with DUnit/NUnit

Page 14: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 15

Summary Delphi 2005’s refactorings are a step in the

right direction Let’s hope Corbin opens up the refactoring API Let’s see more refactorings!

Generally, they work, but currently provide some interesting results Particularly if you are extracting a method that is

part of a IF condition

Look out for an article comparing the refactoring results of:

Delphi 2005, Visual Studio.net 2005 (Whidbey), Castalia 3.x, Code Explorer

Page 15: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 16

Summary

“Things should be made as simple as possible, but no simpler.”

Albert Einstein

Page 16: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 17

Resources (web-sites) Martin Fowler’s refactoring library:

http://www.refactoring.com/

Joshua Kerievsky’s Refactoring To Patterns http://industriallogic.com/xp/refactoring/catalog.html

An Introduction to Test-Driven Development DUnit for Delphi 5-7 csUnit for Delphi 8 and Visual Basic.net http://www.craigmurphy.com/bug

Page 17: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 18

Resources (Books)Refactoring: Improving the

Design of Existing CodeMartin FowlerAddison-Wesley, 1999ISBN 0-201-48567-2

_____________________________

Refactoring To PatternsJoshua Kerievsky Addison-Wesley, 2004ISBN 0-321-21335-1

Page 18: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 19

Resources (Books)

Refactoring WorkbookWilliam WakeAddison-Wesley, 2004ISBN 0-321-10929-5

Page 19: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 20

Resources (Books)test-driven development: A Practical

GuideDave AstelsPrentice-Hall/Pearson Education, 2003ISBN 0-13-101649-0

Reviewed BUG developers’ magazine, Nov/Dec 2003http://www.craigmurphy.com/bug/tdd/review.htm______________________________________

Test-Driven Development: By Example Kent BeckAddison-Wesley, 2003ISBN 0-321-14653-0______________________________________

Test-Driven Development in Microsoft.NETJames W. Newkirk; Alexei A. Vorontsov Microsoft Press, 2004ISBN 0-7356-1948-4

Page 20: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 21

Contact and Update Information

Craig Murphy

Updated slides, notes and source code:http://www.CraigMurphy.com

Page 21: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 22

Questions?

Page 22: Refactoring Delphi 2005   Win32

Delphi 2005 RefactoringDelphi 2005 Refactoring 23