diff --git a/rules/S3063/csharp/rule.adoc b/rules/S3063/csharp/rule.adoc index d4f3cbe4fa7..270a65b642a 100644 --- a/rules/S3063/csharp/rule.adoc +++ b/rules/S3063/csharp/rule.adoc @@ -1,16 +1,49 @@ -include::../rule.adoc[] +`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. -ifdef::env-github,rspecator-view[] +== Noncompliant Code Example -''' -== Implementation Specification -(visible only on this page) +[source,csharp] +---- +public void DoSomething(List strings) { + var sb = new StringBuilder(); // Noncompliant + sb.Append("Got: "); + foreach(var str in strings) { + sb.Append(str).Append(", "); + // ... + } +} +---- -include::../message.adoc[] +== Compliant Solution -''' -== Comments And Links -(visible only on this page) +[source,csharp] +---- +public void DoSomething(List strings) { + foreach(var str in strings) { + // ... + } +} +---- +or +[source,csharp] +---- +public void DoSomething(List strings) { + var sb = new StringBuilder(); + sb.Append("Got: "); + foreach(var str in strings) { + sb.Append(str).Append(", "); + // ... + } + logger.LogInformation(sb.ToString()); +} +---- -include::../comments-and-links.adoc[] -endif::env-github,rspecator-view[] +== 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 (`var sb = GetStringBuilder();`). +* Returned by the method. \ No newline at end of file diff --git a/rules/S3063/vbnet/metadata.json b/rules/S3063/vbnet/metadata.json new file mode 100644 index 00000000000..7a73a41bfdf --- /dev/null +++ b/rules/S3063/vbnet/metadata.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/rules/S3063/vbnet/rule.adoc b/rules/S3063/vbnet/rule.adoc new file mode 100644 index 00000000000..abbbc103ea8 --- /dev/null +++ b/rules/S3063/vbnet/rule.adoc @@ -0,0 +1,49 @@ +`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. \ No newline at end of file