Chapter 11: Refactoring and Code Snippets


Overview

Refactoring is the process of changing code retrospectively to make it more comprehensible, maintainable, and efficient. Very often, you perform refactoring to apply good design practice after the fact, once the code has been written; and in that respect, refactoring blurs the edge between design and coding. That edge was getting pretty fuzzy anyway due to Visual Studio 2005's automatic synchronization between class diagrams and code (see Chapter 5); and as you'll see in this chapter, it is perfectly possible to drive some aspects of refactoring straight from Class Diagrams prior to writing a single line of code yourself.

A key principle of refactoring is that it should not change the observable behavior of your code. Following a successful refactoring, your application should work exactly as before; the external behavior should be the same even if the internal implementation is somewhat different.

You can apply refactoring techniques entirely manually, but it can be quite tricky making sure you've taken everything into account — for example, finding every invocation of a method whose parameters you have changed or reordered. It's much better to let the tool — in this case, Visual Studio 2005 — do it for you.

Note

If you've used another IDE, such as the Eclipse, you may already be familiar with automated refactoring.

There are two ways to invoke the various refactoring types: from Class Diagrams (as we've hinted) and from code. We will demonstrate the first two refactoring types — Extract Interface and Implement Abstract Class — using Class Designer, in part because these particular refactorings may be demonstrated very effectively in pictures, and in part to underline the fact that Class Designer, unlike the Distributed System Designers, is not restricted to the Team Architect edition of Visual Studio 2005.

Important

Here's a tip: If you've not already done so, we recommend that even if you identify yourself as a "developer," rather than an "architect," you should read Chapter 5 to familiarize yourself with Class Designer.

As a set of code restructuring techniques, all other refactoring types will be demonstrated solely in code, invoked from within the code editor. We'll note the ones that may also be invoked from Class Diagrams.

Be aware that Visual Studio 2005 supports refactoring of C# and J# code out of the box, but not refactoring of Visual Basic code. We'll settle on C# for consistency in our demonstrations, but rest assured that the techniques applied to J# code would be very similar if not identical.

Note

Refactoring of Visual Basic code is supported in the form of the "Refactor! for Visual Basic 2005" add-in, which you can download free at http://www.msdn.microsoft.com/vbasic/downloads/2005/tools/refactor/. We don't cover that tool here because it's not core to Visual Studio 2005, but the principle is the same so it's worth a look if this chapter whets your appetite for refactoring.

To complete this chapter, we describe another technique for improving the quality of code, called code snippets. As you'll see, code snippets help to lock-down standard implementations, thereby ensuring that the same tried-and-tested technique is used throughout development, such as for error handling or logging tasks, which tend to be all pervasive.

We'll use Visual Basic .NET primarily to demonstrate the code snippets feature because it provides the richest set, but it's worth noting that a similar concept is offered to other .NET languages, such as Visual C# and Visual J#. We say "similar," not "the same," because each language offers its own unique set of snippets; and in fact prior to the Visual Studio 2005 Beta 2 release, the equivalent feature in C# was called expansions, rather than code snippets.

In the examples that follow, you will see our sample classes prefixed with the namespace Refactoring, as in Refactoring.StockDeal. That's because we created a Visual Studio project named Refactoring to contain all our examples. If you try to recreate our examples from scratch, rather than using our download ZIP, we suggest you do the same.



Professional Visual Studio 2005 Team System
Professional Visual Studio 2005 Team System (Programmer to Programmer)
ISBN: 0764584367
EAN: 2147483647
Year: N/A
Pages: 220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net