Using a class variable vs Sending a local variable to functions / methods

When it's a good form to move a local variable to a function / method as a parameter, instead of using a class variable instead of a function / method variable.

For example, I may 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, for example:

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

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

When is it in good shape? I know this is a loaded question, but I am trying to make an argument for my argument with a collaborator, and they state that I will add more code to the function / method signatures, instead of supporting less function / method signatures.

In my opinion, I make the code cleaner and easier to maintain by clicking on the class variables on the corresponding functions / methods, rather than making them rely on / aware of the existence of the class variable.

Please inform.

+6
java c ++ function object design
source share
3 answers

The only answer in blue is for any general case: it depends on your particular case. Data elements, static members, and function arguments serve different purposes. Of course, there are a few key tips we can give for the types of characters you should look for in order to choose one or the other.

Typical cases:

  • Data element : this value is part of the state of the object (as in the case of a class). You want other method calls to reflect this particular state.
  • Static member : the value has the same value for all instances of the class. Usually this is used only for constants (even during initialization at runtime, for example, in single mode), but in some cases it is necessary to change the state of the class.
  • Function argument : value makes sense only for the specific execution of the function / method. This value can be changed from one call to another.

There are some common symptoms of poor choice.

Consider the following questions:

  • Do you always pass the same value to a method, no matter where you call it from? Imagine that the argument is constant and hides the argument. Consider the definition of overloads: one with no argument for the general case and one with an argument for flexibility.
  • Do you need to set a data member (via the setter) every time you call a function? Consider making a value an argument to a function. There is no need to save functions on the signature if you need to replace each call with two lines in order to set the value before hand.

I got the impression that you and your colleague were in a simple misunderstanding of the nature of this parameter. Make sure you clearly understand your colleague's arguments and make it clear. Try rephrasing what you are trying to say.

+10
source share

I consider this from the point of view of dependency, that is, who depends on the variable (in your case var ), is this a method or a class?

For example, JavaBeans have class variables that depend on the class, so if the class needs these variables, then DoSomething() better.

Alternatively, if the class does not care about var and does not need it anywhere, and only DoSomething() requires var , then DoSomething(int var) is important.

+2
source share

This should not be about giving more or less code, or requiring more input. It is about what is more logical in the context of this class and function. Saving things isolated from each other, like you, in general, is good, but do not do this. When it is clear from the point of view of the function that it should work on the value contained in the var class, it should do this and not receive the value through the parameter.

0
source share

All Articles