From b89359e508158e640102a90c54cccdb29ef40f9c Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Tue, 14 Feb 2023 02:15:03 +0100 Subject: [PATCH 1/5] Add C# rule description --- rules/S3398/csharp/rule.adoc | 57 ++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/rules/S3398/csharp/rule.adoc b/rules/S3398/csharp/rule.adoc index 8450abb9a4c..a48d199cc8d 100644 --- a/rules/S3398/csharp/rule.adoc +++ b/rules/S3398/csharp/rule.adoc @@ -1,4 +1,57 @@ -include::../rule.adoc[] +include::../description.adoc[] + +== Noncompliant Code Example + +[source,csharp] +---- +public class Outer +{ + public void OuterMethod() + { + Console.WriteLine("Called from an outer method."); + } + + private static void Print(int num) // Noncompliant - method is only used by inner class, should be moved there + { + Console.WriteLine(num); + } + + public class Inner + { + 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 Inner + { + public void SomeMethod() + { + Print(42); + } + + private static void Print(int num) + { + Console.WriteLine(num); + } + } +} +---- + ifdef::env-github,rspecator-view[] ''' @@ -9,4 +62,4 @@ include::../message.adoc[] include::../highlighting.adoc[] -endif::env-github,rspecator-view[] +endif::env-github,rspecator-view[] \ No newline at end of file From b4c8257faed4015107da448c5d12785f601e22f5 Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Tue, 14 Feb 2023 16:50:00 +0100 Subject: [PATCH 2/5] Fix links --- rules/S3398/cfamily/rule.adoc | 1 + rules/S3398/description.adoc | 1 + rules/S3398/java/rule.adoc | 1 + rules/S3398/rule.adoc | 3 --- 4 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 rules/S3398/description.adoc diff --git a/rules/S3398/cfamily/rule.adoc b/rules/S3398/cfamily/rule.adoc index 8450abb9a4c..71e36c88a57 100644 --- a/rules/S3398/cfamily/rule.adoc +++ b/rules/S3398/cfamily/rule.adoc @@ -1,3 +1,4 @@ +include::../description.adoc[] include::../rule.adoc[] ifdef::env-github,rspecator-view[] diff --git a/rules/S3398/description.adoc b/rules/S3398/description.adoc new file mode 100644 index 00000000000..b47e8a8270c --- /dev/null +++ b/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. diff --git a/rules/S3398/java/rule.adoc b/rules/S3398/java/rule.adoc index 8450abb9a4c..71e36c88a57 100644 --- a/rules/S3398/java/rule.adoc +++ b/rules/S3398/java/rule.adoc @@ -1,3 +1,4 @@ +include::../description.adoc[] include::../rule.adoc[] ifdef::env-github,rspecator-view[] diff --git a/rules/S3398/rule.adoc b/rules/S3398/rule.adoc index 791bcd67a39..6f5e2bfecc0 100644 --- a/rules/S3398/rule.adoc +++ b/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. - - == Noncompliant Code Example [source,text] From 932bc54e26a63942ee118266b887da415d0216d9 Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Wed, 15 Feb 2023 15:32:30 +0100 Subject: [PATCH 3/5] Rename inner -> nested --- rules/S3398/csharp/rule.adoc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/S3398/csharp/rule.adoc b/rules/S3398/csharp/rule.adoc index a48d199cc8d..19e76ff88f9 100644 --- a/rules/S3398/csharp/rule.adoc +++ b/rules/S3398/csharp/rule.adoc @@ -1,4 +1,4 @@ -include::../description.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. == Noncompliant Code Example @@ -11,12 +11,12 @@ public class Outer Console.WriteLine("Called from an outer method."); } - private static void Print(int num) // Noncompliant - method is only used by inner class, should be moved there + private static void Print(int num) // Noncompliant - method is only used by the nested class, should be moved there { Console.WriteLine(num); } - public class Inner + public class Nested { public void SomeMethod() { @@ -37,7 +37,7 @@ public class Outer Console.WriteLine("Called from an outer method."); } - public class Inner + public class Nested { public void SomeMethod() { From 0431919eef64d64eadab55cef0eb0c89c7fac76c Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Tue, 28 Feb 2023 10:30:21 +0100 Subject: [PATCH 4/5] Update code sample --- rules/S3398/csharp/rule.adoc | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/rules/S3398/csharp/rule.adoc b/rules/S3398/csharp/rule.adoc index 19e76ff88f9..2725d9280c1 100644 --- a/rules/S3398/csharp/rule.adoc +++ b/rules/S3398/csharp/rule.adoc @@ -1,4 +1,4 @@ -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. +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 @@ -6,21 +6,18 @@ When a ``++private static++`` method is only invoked by a nested class, there's ---- public class Outer { - public void OuterMethod() - { - Console.WriteLine("Called from an outer method."); - } + private const int base = 42; - private static void Print(int num) // Noncompliant - method is only used by the nested class, should be moved there + private static void Print(int num) // Noncompliant - static method is only used by the nested class, should be moved there { - Console.WriteLine(num); + Console.WriteLine(num + base); } public class Nested { public void SomeMethod() { - Outer.Print(42); + Outer.Print(1); } } } @@ -32,21 +29,18 @@ public class Outer ---- public class Outer { - public void OuterMethod() - { - Console.WriteLine("Called from an outer method."); - } + private const int base = 42; public class Nested { public void SomeMethod() { - Print(42); + Print(1); } private static void Print(int num) { - Console.WriteLine(num); + Console.WriteLine(num + base); } } } From 22b1744ced7f891f54dff82be2c7a20e1e8f0ee0 Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Tue, 28 Feb 2023 10:36:48 +0100 Subject: [PATCH 5/5] Revert unneeded changes to adoc files --- rules/S3398/cfamily/rule.adoc | 1 - rules/S3398/description.adoc | 1 - rules/S3398/java/rule.adoc | 1 - rules/S3398/rule.adoc | 3 +++ 4 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 rules/S3398/description.adoc diff --git a/rules/S3398/cfamily/rule.adoc b/rules/S3398/cfamily/rule.adoc index 71e36c88a57..8450abb9a4c 100644 --- a/rules/S3398/cfamily/rule.adoc +++ b/rules/S3398/cfamily/rule.adoc @@ -1,4 +1,3 @@ -include::../description.adoc[] include::../rule.adoc[] ifdef::env-github,rspecator-view[] diff --git a/rules/S3398/description.adoc b/rules/S3398/description.adoc deleted file mode 100644 index b47e8a8270c..00000000000 --- a/rules/S3398/description.adoc +++ /dev/null @@ -1 +0,0 @@ -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. diff --git a/rules/S3398/java/rule.adoc b/rules/S3398/java/rule.adoc index 71e36c88a57..8450abb9a4c 100644 --- a/rules/S3398/java/rule.adoc +++ b/rules/S3398/java/rule.adoc @@ -1,4 +1,3 @@ -include::../description.adoc[] include::../rule.adoc[] ifdef::env-github,rspecator-view[] diff --git a/rules/S3398/rule.adoc b/rules/S3398/rule.adoc index 6f5e2bfecc0..791bcd67a39 100644 --- a/rules/S3398/rule.adoc +++ b/rules/S3398/rule.adoc @@ -1,3 +1,6 @@ +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. + + == Noncompliant Code Example [source,text]