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
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions rules/S3398/cfamily/rule.adoc
@@ -1,3 +1,4 @@
include::../description.adoc[]
include::../rule.adoc[]
ifdef::env-github,rspecator-view[]

Expand Down
57 changes: 55 additions & 2 deletions rules/S3398/csharp/rule.adoc
@@ -1,4 +1,57 @@
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' members, but the outer class will be clearer and less cluttered.
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

== Noncompliant Code Example

[source,csharp]
----
public class Outer
{
public void OuterMethod()
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine("Called from an outer method.");
}

private static void Print(int num) // Noncompliant - method is only used by the nested class, should be moved there
{
Console.WriteLine(num);
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}

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

== Compliant Solution

[source,csharp]
----
public class Outer
{
public void OuterMethod()
{
Console.WriteLine("Called from an outer method.");
}

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

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

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

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

include::../highlighting.adoc[]

endif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]
1 change: 1 addition & 0 deletions rules/S3398/description.adoc
@@ -0,0 +1 @@
When a ``++private++`` method is only invoked by an inner class, there's no reason not to move it into that class. It will still have the same access to the outer class' members, but the outer class will be clearer and less cluttered.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you actually, but I would not use the word inner class, but nested class.

Nested is the standard way "types within types" are defined (c.p. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/nested-types).

While calling them nested or inner doesn't make any difference in C#, it does in Java (where the rule was first implemented), where inner classes have a reference to the outer class this and nested classes (which are called static class) don't.

The example in the Java code shows that they actually meant inner, since Outie.this.increment(); is used. So using nested in C# would in my opinion avoid some confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't agree more. Renamed it.

1 change: 1 addition & 0 deletions rules/S3398/java/rule.adoc
@@ -1,3 +1,4 @@
include::../description.adoc[]
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
include::../rule.adoc[]
ifdef::env-github,rspecator-view[]

Expand Down
3 changes: 0 additions & 3 deletions rules/S3398/rule.adoc
@@ -1,6 +1,3 @@
When a ``++private++`` method is only invoked by an inner class, there's no reason not to move it into that class. It will still have the same access to the outer class' members, but the outer class will be clearer and less cluttered.
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved


== Noncompliant Code Example

[source,text]
Expand Down