-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create rule S3063: Add vbnet language #1524
Changes from 7 commits
e1e1be2
a0461f4
9de270d
cc40ef6
e8a60e4
6d2843b
be8b428
d8fde13
2d36200
5c2f745
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,16 +1,49 @@ | ||||||
include::../rule.adoc[] | ||||||
`StringBuilder` instances that are not accessed through a `ToString()` invocation needlessly 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<string> 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<string> strings) { | ||||||
foreach(var str in strings) { | ||||||
// ... | ||||||
} | ||||||
} | ||||||
---- | ||||||
or | ||||||
[source,csharp] | ||||||
---- | ||||||
public void DoSomething(List<string> strings) { | ||||||
var sb = new StringBuilder(); | ||||||
sb.Append("Got: "); | ||||||
foreach(var str in strings) { | ||||||
sb.Append(str).Append(", "); | ||||||
// ... | ||||||
} | ||||||
_logger.LogInformation(sb.toString); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the
Suggested change
|
||||||
} | ||||||
---- | ||||||
|
||||||
include::../comments-and-links.adoc[] | ||||||
endif::env-github,rspecator-view[] | ||||||
== Exceptions | ||||||
|
||||||
No issue is reported when `StringBuilder` is: | ||||||
|
||||||
* Accessed through a `CopyTo()` or `GetChunks()` invocation. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add the Length access and the indexer access? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know; I was wondering the same. I decided to skip them to simplify the exception paragraph since both are not that common, but I might be wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe like this?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we get rid of the exception examples it's not that complicated anymore so maybe we can add them for clarity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I only just saw your comment. I created a sub-list for them, but your suggestion may be better indeed, let me change that real quick. |
||||||
* Passed as method argument, on the grounds that it will likely accessed through a `ToString()` invocation there. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* A parameter of the current method. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* Retrieved by a custom function (`var sb = GetStringBuilder();`). | ||||||
* Returned by the method. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
`StringBuilder` instances that are not accessed through a `ToString()` invocation needlessly 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 a `CopyTo()` or `GetChunks()` invocation. | ||
* Passed as method arguments, on the grounds that it will likely accessed through a `ToString()` invocation there. | ||
* A parameter of the current method. | ||
* Retrieved by a custom function (`Dim sb As StringBuilder = GetStringBuilder()`). | ||
* Returned by the method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's better, thanks