HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Still pulling information from XML to insert into Word Document inside 3rd party application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
insertpullingapplication3rdintopartyxmlworddocumentinside

Problem

Follow up to This Question

I took some very good advice and changed my code around a little bit and eliminated some If statements.

I am not retrieving very much information but it looks so skinny now.

Is this a good thing? Is there something that I should add to the code?

Dim phoneNode
Dim phoneNodeList
ReturnData = ""

Set phoneNodeList = XmlDoc.SelectNodes("/Record/Case/CaseFormsLoad/PartyLoad/Party/PartyPhones/Phone")
If phoneNodeList.Length > 0 Then
    For Each phoneNode In phoneNodeList
        If phoneNode.GetAttribute("ConfidentialFlag") = "True" Then
            ReturnData = ReturnData & phoneNode.Getattribute("PhoneNum") & VbCrLf
        End If
    Next
End If


This code is very readable and simple. Is there anything that I can do to make it shorter, and should I make it shorter? it is a Script and not a fully compiled code.

Solution

I see one point that seems almost obvious enough that I'm worried I might be missing something. A For Each is "smart" enough that its body isn't executed at all for an empty collection, so you can eliminate the test for the list having a length of 0:

Dim phoneNode
Dim phoneNodeList
ReturnData = ""

Set phoneNodeList = XmlDoc.SelectNodes("/Record/Case/CaseFormsLoad/PartyLoad/Party/PartyPhones/Phone")
For Each phoneNode In phoneNodeList
    If phoneNode.GetAttribute("ConfidentialFlag") = "True" Then
        ReturnData = ReturnData & phoneNode.Getattribute("PhoneNum") & VbCrLf
    End If
Next

Code Snippets

Dim phoneNode
Dim phoneNodeList
ReturnData = ""

Set phoneNodeList = XmlDoc.SelectNodes("/Record/Case/CaseFormsLoad/PartyLoad/Party/PartyPhones/Phone")
For Each phoneNode In phoneNodeList
    If phoneNode.GetAttribute("ConfidentialFlag") = "True" Then
        ReturnData = ReturnData & phoneNode.Getattribute("PhoneNum") & VbCrLf
    End If
Next

Context

StackExchange Code Review Q#49824, answer score: 6

Revisions (0)

No revisions yet.