rspec/rules/S3874/csharp/rule.adoc

56 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
Passing a parameter by reference, which is what happens when you use the ``++out++`` or ``++ref++`` parameter modifiers, means that the method will receive a pointer to the argument, rather than the argument itself. If the argument was a value type, the method will be able to change the argument's values. If it was a reference type, then the method receives a pointer to a pointer, which is usually not what was intended. Even when it is what was intended, this is the sort of thing that's difficult to get right, and should be used with caution.
2020-06-30 12:48:39 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when ``++out++`` or ``++ref++`` is used on a non-``++Optional++`` parameter in a public method. ``++Optional++`` parameters are covered by S3447.
2020-06-30 12:48:39 +02:00
=== Noncompliant code example
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:39 +02:00
----
public void GetReply(
ref MyClass input, // Noncompliant
out string reply) // Noncompliant
{ ... }
----
=== Compliant solution
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:39 +02:00
----
public string GetReply(MyClass input)
{ ... }
public bool TryGetReply(MyClass input, out string reply)
{ ... }
public ReplyData GetReply(MyClass input)
{ ... }
internal void GetReply(ref MyClass input, out string reply)
{ ... }
----
=== Exceptions
2020-06-30 12:48:39 +02:00
This rule will not raise issues for:
2021-01-06 17:38:34 +01:00
* non-public methods
* methods with only 'out' parameters, name starting with "Try" and return type bool.
* interface implementation methods
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Consider refactoring this method in order to remove the need for this "[out|ref]" modifier.
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]