
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
52 lines
1.4 KiB
Plaintext
52 lines
1.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
`StringBuilder` instances that never build a `string` clutter the code and worse are a drag on performance. Either they should be removed, or the missing `ToString()` call should be added.
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,vbnet]
|
|
----
|
|
Public Sub DoSomething(ByVal strings As List(Of String))
|
|
Dim sb As StringBuilder = New StringBuilder() ' Noncompliant
|
|
sb.Append("Got: ")
|
|
|
|
For Each str As String In strings
|
|
sb.Append(str).Append(", ")
|
|
Next
|
|
End Sub
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,vbnet]
|
|
----
|
|
Public Sub DoSomething(ByVal strings As List(Of String))
|
|
For Each str As String In strings
|
|
Next
|
|
End Sub
|
|
----
|
|
or
|
|
[source,vbnet]
|
|
----
|
|
Public Sub DoSomething(ByVal strings As List(Of String))
|
|
Dim sb As StringBuilder = New StringBuilder()
|
|
sb.Append("Got: ")
|
|
|
|
For Each str As String In strings
|
|
sb.Append(str).Append(", ")
|
|
Next
|
|
|
|
My.Application.Log.WriteEntry(sb.ToString())
|
|
End Sub
|
|
----
|
|
|
|
=== Exceptions
|
|
|
|
No issue is reported when `StringBuilder` is:
|
|
|
|
* Accessed through `sb.CopyTo()`, `sb.GetChunks()`, `sb.Length`, or `sb(index)`.
|
|
* Passed as a method argument, on the grounds that it will likely be accessed through a `ToString()` invocation there.
|
|
* Passed in as a parameter to the current method, on the grounds that the callee will materialize the string.
|
|
* Retrieved by a custom function (`Dim sb As StringBuilder = GetStringBuilder()`).
|
|
* Returned by the method.
|