Why is this an issue?
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant code example
Public Class Foo
Private Name As String = "foobar" ' Noncompliant
Public ReadOnly Property DefaultName As String = "foobar" ' Noncompliant
Public Sub New(Optional Value As String = "foobar") ' Noncompliant
Dim Something = If(Value, "foobar") ' Noncompliant
End Sub
End Class
Compliant solution
Public Class Foo
Private Const Foobar As String = "foobar"
Private Name As String = Foobar
Public ReadOnly Property DefaultName As String = Foobar
Public Sub New(Optional Value As String = Foobar)
Dim Something = If(Value, Foobar)
End Sub
End Class
Exceptions
The following are ignored:
- literals with fewer than 5 characters
- literals matching one of the parameter names
- literals used in attributes