Syspect
Syspect

Reputation: 921

The better technique in this refactoring case?

I need to do some refactoring (actually it's A LOT, but this small step will be very helpful for the whole process). So, let's say I've got this code snippet:

If xmlDoc.SelectSingleNode("/dang") IsNot Nothing Then
    universalNode = xmlDoc.SelectSingleNode("/dang")
    Type = "dang"
ElseIf xmlDoc.SelectSingleNode("/nang") IsNot Nothing Then
    universalNode = xmlDoc.SelectSingleNode("/nang")
    Type = "nang"
ElseIf xmlDoc.SelectSingleNode("/lang") IsNot Nothing Then
    universalNode = xmlDoc.SelectSingleNode("/lang")
    Type = "lang"
ElseIf xmlDoc.SelectSingleNode("/tang") IsNot Nothing Then
    universalNode = xmlDoc.SelectSingleNode("/tang")
    Type = "tang"
ElseIf xmlDoc.SelectSingleNode("/xtang") IsNot Nothing Then
    universalNode = xmlDoc.SelectSingleNode("/xtang")
    Type = "xtang"
End If

It's in the body of a big function and I want to take it out to a separate function. So, what I was wondering is whether it will be better to pass both universalNode and Type by value and just assign to them the values, without the need of returning anything? Is that going to work well or it is risky?

If it was just the Type that I'm working on, i.e., then I would just return it, but it's more than 1 variable that is being changed, and both are local variables for the big function, from which I am taking out this code snippet.

Any other suggestions maybe?

Upvotes: 1

Views: 40

Answers (1)

SysDragon
SysDragon

Reputation: 9888

You can pass the variables by reference (not by value) to the function, it's not a bad practice and should work:

Public Sub MyFunc(ByRef node As MyNode, ByRef typ As String) 
    node = ...
    typ = ...
End Sub

Or you can return some complex data holder:

Public Class MyParams
   Public node As MyNode
   Public typ As String
End Class

Public MyParams MyFunc() 
    Dim result As New MyParams()
    result.node = ...
    result.typ = ...
    Return result
End Sub

Upvotes: 1

Related Questions