When to use an object instance variable versus passing an argument to the method

VariablesMethodsParameter PassingDeclaration

Variables Problem Overview


How do you decide between passing arguments to a method versus simply declaring them as object instance variables that are visible to all of the object's methods?

I prefer keeping instance variables in a list at the end of the Class, but this list gets longer as my program grows. I figure if a variable is passed often enough it should just be visible to all methods that need it, but then I wonder, "if everything is public there will be no need for passing anything at all!"

Variables Solutions


Solution 1 - Variables

Since you're referring to instance variables, I'm assuming that you're working in an object-oriented language. To some degree, when to use instance variables, how to define their scope, and when to use local variables is subjective, but there are a couple of rules of thumb you can follow whenever creating your classes.

  • Instance variables are typically considered to be attributes of a class. Think of these as adjectives of the object that will be created from your class. If your instance data can be used to help describe the object, then it's probably safe to bet it's a good choice for instance data.

  • Local variables are used within the scope of methods to help them complete their work. Usually, a method should have a purpose of getting some data, returning some data, and/or proccessing/running an algorithm on some data. Sometimes, it helps to think of local variables as ways of helping a method get from beginning to end.

  • Instance variable scope is not just for security, but for encapsulation, as well. Don't assume that the "goal should be to keep all variables private." In cases of inheritance, making variables as protected is usually a good alternative. Rather than marking all instance data public, you create getters/setters for those that need to be accessed to the outside world. Don't make them all available - only the ones you need. This will come throughout the development lifecycle - it's hard to guess from the get go.

When it comes to passing data around a class, it's difficult to say what you're doing is good practice without seeing some code . Sometimes, operating directly on the instance data is fine; other times, it's not. In my opinion, this is something that comes with experience - you'll develop some intuition as your object-oriented thinking skills improve.

Solution 2 - Variables

Mainly this depends on the lifetime of the data you store in the variable. If the data is only used during a computation, pass it as a parameter. If the data is bound to the lifetime of the object use an instance variable.

When your list of variables gets too long, maybe it's a good point to think about refactoring some parts of the class into a new class.

Solution 3 - Variables

In my opinion, instance variables are only necessary when the data will be used across calls.

Here's an example:

myCircle = myDrawing.drawCircle(center, radius);

Now lets imaging the myDrawing class uses 15 helper functions to create the myCircle object and each of those functions will need the center and the radius. They should still not be set as instance variables of the myDrawing class. Because they will never be needed again.

On the other hand, the myCircle class will need to store both the center and radius as instance variables.

myCircle.move(newCenter);
myCircle.resize(newRadius);

In order for the myCircle object to know what it's radius and center are when these new calls are made, they need to be stored as instance variables, not just passed to the functions that need them.

So basically, instance variables are a way to save the "state" of an object. If a variable is not necessary to know the state of an object, then it shouldn't be an instance variable.

And as for making everything public. It might make your life easier in the moment. But it will come back to haunt you. Pease don't.

Solution 4 - Variables

IMHO:

If the variable forms part of the state of the instance, then it should be an instance variable - classinstance HAS-A instancevariable.

If I found myself passing something repeatedly into an instance's methods, or I found that I had a large number of instance variables I'd probably try and look at my design in case I'd missed something or made a bad abstraction somewhere.

Hope it helps

Solution 5 - Variables

Of course it is easy to keep one big list of public variables in the class. But even intuitively, you can tell that this is not the way to go.

Define each variable right before you are going to use it. If a variable supports the function of a specific method, use it only in the scope of the method.

Also think about security, a public class variable is susceptible to unwanted changes from "outside" code. Your main goal should be to keep all variables private, and any variable which is not, should have a very good reason to be so.

About passing parameters all they way up the stack, this can get ugly very fast. A rule of thumb is to keep your method signatures clean and elegant. If you see many methods using the same data, decide either if it's important enough to be a class member, and if it's not, refactor your code to have it make more sense.

It boils down to common sense. Think exactly where and why you are declaring each new variable, what it's function should be, and from there make a decision regarding which scope it should live in.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZiggyView Question on Stackoverflow
Solution 1 - VariablesTomView Answer on Stackoverflow
Solution 2 - VariablesH-Man2View Answer on Stackoverflow
Solution 3 - Variablesng.mangineView Answer on Stackoverflow
Solution 4 - VariablesbrabsterView Answer on Stackoverflow
Solution 5 - VariablesYuval AdamView Answer on Stackoverflow