8

When is it good form to push a local variable to a function/method as a parameter, rather than using a class variable in place of the function/method variable.

For instance, I can have a function:

int DoSomething(int var)
{
   if(var == -1)
     return 0;
}

or I can have a class variable "_var" and use it in the same function, like this:

int DoSomething()
{
   if(_var == -1)
     return 0;
}

I'm of the mind to, if we have a class variable to be used in some function/method, called DoSomething in my example above, that I should send the DoSomething function/method the class variable as a parameter, so that the function is easier to read and test.

When is it good form to do either? I know this is a loaded question, but I'm trying to make a case for my argument with a co-worker, and they're stating that I would add more code to the function/method signatures, rather than keeping the function/method signatures smaller.

In my mind, I'm making the code cleaner and easier to maintain by pushing the class variable(s) to the respective functions/methods, rather than forcing them to rely on/know about a class variable's existence.

Please advise.

pajton
  • 14,670
  • 6
  • 50
  • 63
Rick
  • 689
  • 1
  • 6
  • 17

3 Answers3

12

The only answer out of the blue, for a any generic case is: it depends on your specific case. Data members, static members and function arguments all serve different purposes. Of course, there are some key tips we can give for what types of signs you should look for choosing one or the other.

Typical cases:

  • Data member: the value is part of the object's (as in instance of a class) state. You want other calls to methods to reflect this particular state.
  • Static member: the value has simultaneous, identical meaning to all instances of the class. This is typically used only for constants (even when initialized at runtime, like a singleton) but in some cases, there is need for mutable class state.
  • Function argument: the value has meaning only for a specific execution of the function/method. This value is subject to change from one invocation to the next.

There are some common symptoms of bad choice.

Consider the following questions:

  • Do you always pass the same value to a method, no matter where you call it from? Consider making the argument a constant and hiding the argument away. Consider defining overloads: one without argument for the common case and one with argument for flexibility.
  • Do you need to set a data member (via a setter) every single time you invoke the function? Consider making the value an argument to the function. There's no need to save on the function signature if you need to replace each call with two lines to set the value before hand.

I'm under the impression that you and your co-worker are in a simple misunderstanding of the nature of this parameter. Make sure you clearly understand your co-worker's arguments and make yourself clear. Try to rephrase what it is that you're trying to say.

André Caron
  • 41,491
  • 10
  • 58
  • 117
  • Thanks for your feedback Andre. My co-worker's original design involved a dynamic initialization of the class variable, via the constructor, and this class variable was used in several functions in the class. I removed this dependency and instead sent the variable in question to a function call, which then uses it in other function calls. Since the variable is not encapsulating data to then be propagated elsewhere, I thought the DoSomething(int var) function would be more appropriate. Thanks again for your thoughtful response. – Rick Apr 04 '11 at 15:42
2

I look at it in terms of dependency, i.e. who is dependent on the variable (in your case var), is it a method or a class?

For e.g. JavaBeans have class variables that are dependent by the class, so if the class needs these variables then DoSomething() is best.

Alternatively, if you class doesn't care about var and doesn't need it anywhere else, and only DoSomething() requires var, then DoSomething(int var) is essential.

Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220
  • Thanks for your response! Yes. This is exactly the argument I am preparing, outlined in my comment to Andre above. – Rick Apr 04 '11 at 15:43
0

It shouldn't be about what gives more or less code or what takes more effort to type. It is about what is more logical within the context of that class and function. Keeping things isolated from each other like you do is in general a good thing but don't over do it. When it is clear from the purpose of a function that it should be working on a value contained in a class var it should do so and not receive the value through a parameter.

Eelke
  • 17,682
  • 3
  • 40
  • 68