
Greetings friends! I bring tidings of comfort and joy! That is, you can rest
comfortably and joyously, knowing that you don't have to wait for refactorings
that leverage the new language features of
Visual Studio
2008. These refactorings are available today.
I'm very excited about today's verse. As promised
yesterday, I'm back to share some more refactoring possibilities for Visual
Basic XML Literals.
By the end of the verse, you should have a sense of how powerful these
refactorings truly are. If you're a Visual Basic developer, you
definitely won't want to miss this!
"On the eleventh day of X-mas my true love (DevExpress)
gave to me..."
More Refactoring in XML Literals
Yesterday, we saw how our
bread-and-butter refactorings can be used on the inner text of XML
tags to create new embedded expressions. This is extremely helpful when trying
to make the contents of an XML literal more dynamic. Today, we'll take things a
step further to see how we can use
Refactor!
Pro to manipulate the XML tags themselves.
Module TwelveDaysOfXmas
Sub Main()
Dim aPrice As Decimal
= 0
Dim lBook = <book isbn="12252007">
<title>Refactoring: The True Meaning of X-mas</title>
<price><%=
aPrice.ToString("C") %></price>
<author>
<first-name>Dustin</first-name>
<last-name>Campbell</last-name>
</author>
</book>
End Sub
End Module
Consider the code above. Since
Refactor! Pro
works on XML tags, we can select the entire <price>
tag and apply Extract Method to get the following:
Module TwelveDaysOfXmas
Private Function GetPrice(ByVal aPrice As Decimal) As XElement
Return <price><%=
aPrice.ToString("C") %></price>
End Function
Sub Main()
Dim aPrice As Decimal
= 0
Dim lBook = <book isbn="12252007">
<title>Refactoring: The True Meaning of X-mas</title>
<%=
GetPrice(aPrice) %>
<author>
<first-name>Dustin</first-name>
<last-name>Campbell</last-name>
</author>
</book>
End Sub
End Module
A potential complication is the use of
aPrice in the embedded expression.
Fortunately, Extract Method intelligently analyzes this and declares it as a
parameter of the new method.
View Screencast to See Extract Method in Action!
Refactor!
Pro's ability to manipulate XML tags makes it easy to dynamically build XML.
In fact it can save minutes of menial coding labor.
Take
another look at the first code example above. Go ahead. I'll wait.
Now, imagine how much effort it
would take to transform that code into this:
Module TwelveDaysOfXmas
Private Function GetTitle(ByVal aTitle As String) As XElement
Return <title><%= aTitle %></title>
End Function
Private Function GetPrice(ByVal aPrice As Decimal) As XElement
Return <price><%= aPrice.ToString("C") %></price>
End Function
Private Function GetAuthor(ByVal aFirstName As String, _
ByVal aLastName As String) As XElement
Return <author>
<first-name><%= aFirstName %></last-name>
<last-name><%= aLastName %></last-name>
</author>
End Function
Private Function GetBook(ByVal aPrice As Decimal, _
ByVal aIsbn As String, _
ByVal aTitle As String, _
ByVal aFirstName As String, _
ByVal aLastName As String) As XElement
Return <book isbn=<%= aIsbn %>>
<%= GetTitle(aTitle) %>
<%= GetPrice(aPrice) %>
<%= GetAuthor(aFirstName, aLastName) %>
</book>
End Function
Sub Main()
Dim lBook = GetBook(0D, _
"12252007", _
"Refactoring: The True Meaning of X-mas", _
"Dustin", _
"Campbell")
End Sub
End Module
That's pretty insane, isn't it? Well, with
Refactor! Pro,
this is a snap. In fact, most of the effort is spent
typing the names of new variables and methods. The refactorings themselves
take only seconds to apply.
Don't Believe Me? Check Out This Screencast!
One point I've saved until now is that
Refactor! Pro
is the very first tool to offer refactorings for Visual Basic
XML Literals.
That's just one more compelling reason that
Refactor! Pro
should be a part of your
Visual Studio
2008 installation this holiday season.
Happy Holidays!