rspec/rules/S1192/csharp/rule.adoc

82 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
include::../description.adoc[]
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
private string name = "foobar"; // Noncompliant
public string DefaultName { get; } = "foobar"; // Noncompliant
2021-01-23 04:07:47 +00:00
public Foo(string value = "foobar") // Noncompliant
2020-06-30 12:47:33 +02:00
{
var something = value ?? "foobar"; // Noncompliant
}
}
----
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public class Foo
{
private const string Foobar = "foobar";
private string name = Foobar;
public string DefaultName { get; } = Foobar;
2021-01-23 04:07:47 +00:00
public Foo(string value = Foobar)
2020-06-30 12:47:33 +02:00
{
var something = value ?? Foobar;
}
}
----
=== Exceptions
2020-06-30 12:47:33 +02:00
The following are ignored:
2020-06-30 12:47:33 +02:00
* literals with fewer than 5 characters
* literals matching one of the parameter names
* literals used in attributes
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Define a constant instead of using the literal "{string}" {number} times.
include::../parameters.adoc[]
=== Highlighting
primary: the class
secondaries: all instances of the string literal
'''
== Comments And Links
(visible only on this page)
=== on 21 Mar 2018, 15:11:26 Amaury Levé wrote:
Could you review this sub-task please?
=== on 21 Mar 2018, 15:33:08 Ann Campbell wrote:
I think a brief code example would be helpful for the second item, [~amaury.leve].
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]