8

I'm writing several methods that call other methods.

To pass the information I have a couple of choices:

  1. Pass the information as parameters

  2. Set instance variables so that other methods can access them

When should I choose one option over the other?

It seems that the first option is good as it is very specific about what is being passed. the downside seems to be that a lot of values are being passed around.

The second method doesn't require passing all the values around but seems to lead to a lot of magic where methods set instance variables 'somewhere'

Should I always be very explicit about gets passed to other methods in the class? Are there exceptions so this?

  • 2
    Many times I've had an ocassion to make changes or fix a bug in some code someone has written using option #2. It is usually a tangled mess and the changes become very time consuming. Even once I have it all sorted out, it still takes a lot of mental effort to make changes because I have to keep straight exactly when and where variables are initialized or updated. – Dr. Wily's Apprentice Oct 18 '13 at 15:08
12

It depends on the role of those variables with respect to the entire class.

If your class is small, and the information that these variables transport is relevant to all or almost all of it, then it makes sense to have them as instance variables. A bank account object probably needs to know its holder for most actions anyway, so it makes sense to hold on to it indefinitely.

If the data are very specific to this method and its helpers, then they should travel as parameters. The date of a transaction is not so much a feature of the account as of a single action taken on that account. (If you have more than one of these parameters, and they tend to be passed around as unit (e.g. date, time of day and time zone), then it is also wise to bundle them into a helper object to keep the number of parameters down.)

14

Let me misquote you to make my point clear:

I'm writing several functions that call other functions. To pass the information I have two choices:

  1. Pass the info as arguments.
  2. Use global variables.

When should I choose one option over the other?

It is generally acknowledged that non-local state is a bad idea. You should therefore generally pass the information using parameters, not via variables in a common scope. This becomes more manageable if you group related information together into objects or structs.

Passing too many parameters is a code smell, and it can probably be refactored to use a few more classes. Assume a pathological case where you are writing an address book and have a method add_contact(surname, firstname, fullname, ...). It would be better to group that into a Name class, likewise for parts of an address: add_contact(name, address).

If however the necessary parameters to your function are completely unrelated, it may be that your method is trying to do too much, and should be refactored into smaller parts.

I do not think that using instance variables is a good solution: it is needless mutation of state, and brings with it all the problems of global variables, on a smaller scale. This way lies un-debuggable spaghetti code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.