rspec/rules/S3451/csharp/rule.adoc

76 lines
2.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
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[]