Skip to content

Latest commit

 

History

History
65 lines (52 loc) · 1.26 KB

rule.adoc

File metadata and controls

65 lines (52 loc) · 1.26 KB

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

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 the nested class, should be moved there
    {
        Console.WriteLine(num);
    }

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

Compliant Solution

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);
        }
    }
}

Implementation Specification

(visible only on this page)