Use the following rules:
For any non-static method of a class which a) does not access private fields directly and b) only calls public methods, move the method to a static helper class and turn it into an extension method.
Any public static method can also be moved to a helper class.
By convention, a helper is a new static class with the same name as the original class but with the word Helper appended to it.
Example:
ORIGINAL CODE:
public class Foo
{
int _intField;
public void Method1()
{
doing something with _intField;
}
public void Method2()
{
Method1();
}
public static void Method3()
{
....
}
private void Method4()
{
....
}
public void Method5()
{
Method4();
}
}
PROPOSED TRANSOFORMATION:
public class Foo
{
int _intField;
public void Method1()
{
doing something with _intField;
}
private void Method4()
{
....
}
public void Method5()
{
Method4();
}
}
public static class FooHelper
{
public static void Method2(this Foo foo)
{
foo.Method1();
}
public static void Method3()
{
....
}
}
- Method 1 cannot move because it's using a private field
- Method 2 can move because it's not using a private field and it's calling a public method. It can be turned into an extension method.
- Method 3 can be moved because it's a static public method
- Method 4 cannot move because it's a private method
- Method 5 cannot move because it's calling a private method.