Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3398: Add C# description #1572

Merged
merged 6 commits into from Mar 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 49 additions & 2 deletions rules/S3398/csharp/rule.adoc
@@ -1,4 +1,51 @@
include::../rule.adoc[]
When a ``private static`` method is only invoked by a nested class, there's no reason not to move it into that class. It will still have the same access to the outer class' static members, but the outer class will be clearer and less cluttered.

== Noncompliant Code Example

[source,csharp]
----
public class Outer
{
private const int base = 42;

private static void Print(int num) // Noncompliant - static method is only used by the nested class, should be moved there
{
Console.WriteLine(num + base);
}

public class Nested
{
public void SomeMethod()
{
Outer.Print(1);
}
}
}
----

== Compliant Solution

[source,csharp]
----
public class Outer
{
private const int base = 42;

public class Nested
{
public void SomeMethod()
{
Print(1);
}

private static void Print(int num)
{
Console.WriteLine(num + base);
}
}
}
----

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -9,4 +56,4 @@ include::../message.adoc[]

include::../highlighting.adoc[]

endif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]