220 likes | 436 Views
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
E N D
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 Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
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) Delphi 2005 Refactoring
Refactoring Safely • How do we ensure that our refactorings have not altered or broken the original functionality? • Tests • Repeatable Tests • Automated, Repeatable Tests • Ideally, we should write tests before we refactor our code… • 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) Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
Available Refactorings Visual Studio 2005 (Whidbey) • Rename • Extract Method • Encapsulate Field • Extract Interface • Promote Local Variable to Parameter • Remove Parameters • Reorder Parameters Delphi 2005 • Rename Parameter/Variable • Extract Method • Declare Variable • Declare Field • Extract Resource String • Sync Edit • Find Unit Delphi 2005 Refactoring
demo Refactoring Using Delphi 2005 Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
Summary “Things should be made as simple as possible, but no simpler.” Albert Einstein Delphi 2005 Refactoring
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 Delphi 2005 Refactoring
Resources (Books) Refactoring: Improving the Design of Existing Code Martin Fowler Addison-Wesley, 1999 ISBN 0-201-48567-2 _____________________________ Refactoring To Patterns Joshua Kerievsky Addison-Wesley, 2004 ISBN 0-321-21335-1 Delphi 2005 Refactoring
Resources (Books) Refactoring Workbook William Wake Addison-Wesley, 2004 ISBN 0-321-10929-5 Delphi 2005 Refactoring
Resources (Books) test-driven development: A Practical Guide Dave Astels Prentice-Hall/Pearson Education, 2003 ISBN 0-13-101649-0 Reviewed BUG developers’ magazine, Nov/Dec 2003 http://www.craigmurphy.com/bug/tdd/review.htm ______________________________________ Test-Driven Development: By Example Kent Beck Addison-Wesley, 2003 ISBN 0-321-14653-0 ______________________________________ Test-Driven Development in Microsoft.NET James W. Newkirk; Alexei A. Vorontsov Microsoft Press, 2004 ISBN 0-7356-1948-4 Delphi 2005 Refactoring
Contact and Update Information Craig Murphy Updated slides, notes and source code: http://www.CraigMurphy.com Delphi 2005 Refactoring
Questions? Delphi 2005 Refactoring