rspec/rules/S3451/csharp/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
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.
2023-05-25 14:18:12 +02:00

76 lines
2.0 KiB
Plaintext

== Why is this an issue?
The use of ``++[DefaultValue]++`` with ``++[Optional]++`` has no more effect than ``++[Optional]++`` alone. That's because ``++[DefaultValue]++`` doesn't actually do anything; it merely indicates the intent for the value. More than likely, ``++[DefaultValue]++`` was used in confusion instead of ``++[DefaultParameterValue]++``.
=== Noncompliant code example
[source,csharp]
----
class MyClass
{
public void DoStuff([Optional][DefaultValue(4)]int i, int j = 5) // Noncompliant
{
Console.WriteLine(i);
}
public static void Main()
{
new MyClass().DoStuff(); // prints 0
}
}
----
=== Compliant solution
[source,csharp]
----
class MyClass
{
public void DoStuff([Optional][DefaultParameterValue(4)]int i, int j = 5)
{
Console.WriteLine(i);
}
public static void Main()
{
new MyClass().DoStuff(); // prints 4
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use "[DefaultParameterValue]" instead.
=== Highlighting
``++[DefaultValue]++``
'''
== Comments And Links
(visible only on this page)
=== on 8 Dec 2015, 09:55:38 Tamas Vajk wrote:
\[~ann.campbell.2] LGTM, but I'm not 100% happy with the title. ``++[DefaultValue]++`` can be used with ``++[Optional]++``, you might build your own tooling around it, and read the value specified in ``++[DefaultValue]++``. So we should probably not say that you shouldn't use it. But I have no better ideas than the current one.
=== on 8 Dec 2015, 15:05:14 Ann Campbell wrote:
\[~tamas.vajk] I struggled with this title myself, and I recognize that ``++[DefaultValue]++`` + ``++[Optional]++`` is a valid usage. But the best alternate title gets into intent, which I don't want to do: '[DefaultValue]" should not be used with "[Optional]" when "[DefaultParamterValue]" was intended'
And if you're building tooling around ``++[DefaultValue]++`` then you'll turn this rule off.
endif::env-github,rspecator-view[]