I am trying to improve FxCop compliance in my code (for the first time in history), but I am a little fixated on the ethical issue. I have a GetText() method that returns a string from a remote server, but may throw an exception under certain circumstances. That's why I also have a TryGetText(ByRef text As String) method that returns a boolean indicating whether the call was successful. The return value is assigned to the text variable if true.
I thought this design was perfectly acceptable given that even Microsoft does this (e.g. Integer.TryParse ). FxCop tricks me into dictating, "You will not follow the link!"
To get around this warning (and there are quite a few), I replaced the StringBuilder parameter. But, despite the fact that now I am compatible, I do not think that he really improved my code.
Before:
Public Function TryGetText(ByRef text As String) As Boolean Dim command As New GetTextCommand(Me) Dim result As CommandResult = ProcessCommand(command, True) If result.CommandStatus <> Constants.Status.Failed Then text = result.Text Return True Else Return False End If End Function
After:
Public Function TryGetText(builder As Text.StringBuilder) As Boolean Dim command As New GetTextCommand(Me) Dim result As CommandResult = ProcessCommand(command, True) If result.CommandStatus <> Constants.Status.Failed Then builder.Clear() builder.Length = result.Text.Length builder.Append(result.Text) Return True Else Return False End If End Function
Is this an acceptable use of ByRef, or should I use an alternative to stringbuilder? Itβs not very convenient for me to suppress this warning for every method where I use this construct. I don't feel the stringbuilder option improves code usability.
source share