loading...

C# internal parameters

fr_tandu profile image fr_tandu ・1 min read

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?

Discussion

pic
Editor guide