
Gentle readers, in the spirit of
X-mas, I'd like to sing you a
carol. This jolly tune
(based on a popular
old English
carol) enumerates ways that
Refactor! Pro
can warm your installation of
Visual Studio 2008
this holiday season. In contrast to some of our... ahem...
<whisper>competition</whisper>,
the features I'll be showing can be used today. In fact, most of
these features have been shipping since Visual Studio 2008 was still a wee child
known by its code name, Orcas.
But, hey, enough of my yammering! Stoke the fire and put on a kettle! Let the merry-making
begin!
"On the first day of X-mas my true love (DevExpress)
gave to me..."
Make Implicit
Visual Studio 2008 introduced a welcome addition to both the
C# and
Visual Basic
languages: implicitly-typed local variables. While this feature is necessary to
properly support another feature,
anonymous types, it can
also be used to enhance the readability of client code—especially when generic types
are being used. Consider the following code:
private static
Dictionary<string,
Guid> BuildIdTable()
{
Dictionary<string,
Guid> map =
new Dictionary<string,
Guid>();
// Fill map with entries...
return map;
}
That sort of code can be a bit frustrating. Dictionary<TKey, TValue> is extremely
powerful, but can be awkward to use. The developer is forced to fully declare the
type name (which is often quite long) on both the left and right sides of the local
variable declaration. Thankfully, an implicitly-typed local variable can alleviate
this problem:
private static
Dictionary<string,
Guid> BuildIdTable()
{
var map = new Dictionary<string,
Guid>();
// Fill map with entries...
return map;
}
Now for the really good news: Refactor! Pro provides Make Implicit, a refactoring
which easily converts explicitly-declared local variables to implicit ones. Check
out the preview hint for Make Implicit:

View
Screencast of Make Implicit in Action!
Of course, like most of our refactorings, Make Implicit works in Visual Basic
as well.

Some critics might be thinking, "What's the point? It just deletes text and inserts
a keyword! I could write a macro to do that!" Well, before those skeptics get too
comfortable with their new macro, consider what Make Implicit must do with code
like the following:
ulong number = 42;
If Make Implicit were simply to swap
ulong for
var, there would be a serious problem. Instead of
ulong, the type of
number
would be inferred as
int, changing the meaning of the code. To fix this problem, a minor
adjustment is made to prevent shooting the code in its proverbial foot.
var number = 42ul;
So, throw your macro away! Let Make Implicit handle the edge cases
intelligently.
Ho, ho, ho! That's quite a gift under the tree! Come back tomorrow when I
continue my jaunty tune and look at another Visual Studio 2008-specific
refactoring that is available to you today: Make
Explicit.