DEV Community

fr_tandu
fr_tandu

Posted on

C# internal parameters

So, let's consider a class, with a public method, that calls private methods:

//Parameter method

public class Doer
{
   public bool somethingDoer(int x)
   {
      doSomething(x);
      doAnotherThing(x);
      doYetAnotherThing(x);
      doSomethingElse(x);
      return true;
   }
   private void doSomething (int y)
   {
      //does something
   }
   private void doAnotherThing(int z)
   {
      //does another thing
   }
   private void doYetAnotherThing(int u)
   {
      //does Yet another thing
   }
   private void doSomethingElse(int p)
   {
      //does something else
   }
}

Is it better to have the x variable as a parameter passed, or a class variable assigned when calling when calling the method? Example:

//Class Variable Method

public bool somethingDoer(int x)
{
   _internalInt = x;
   doSomething();
   doAnotherThing();
   doYetAnotherThing();
   doSomethingElse();
   return true;
}
private void doSomething ()
{
   //does something with _internalInt
}
etc...

When you call `Doer.somethingDoer(5);', you'll get the same result, I'm just wondering from the perspective of design/testing, which is a better way to go? Parameter, or Class Variable?

Top comments (0)