Moving data around your code – different approaches

I was thinking about this the other day, and I realized I can't particularly think of a label to attach to this concept.  Anyone out there got a name for this?  This could be due to my lack of formal computer science training, so I thought I'd ask, at the risk of looking stupid (I'm having an off day, so that's a very real possibility).

If you have a series of functions, say: a(x), which in turn calls b(x), which calls c(x), and so on, and you end up with a faily long call chain, out to say m(x), and you subsequently decide that m(x) needs an additional piece of information that is contained in a local variable in the body of the a(x) function – how do you get it to m(x) ?

  • You could simply make it a global variable!  That's easy and generally a pretty bad idea.
  • You could modify every function signature between a(x) and m(x) to look like b(x, y), c(x, y).  That's a lot of work though.  And what happens if something *else* needs to get passed around?
  • In an object oriented language, you could make x an object, and attach y to it, so that you mostly pass it around just as before.
  • Along the same lines, you could make x a hash table, so that you can look arguments up by their name, to have some idea what they are.
  • You could just make x a list, and remember what position everything is in.  That's not so nice though if you have to revisit the code and you didn't document where everything is, and why it's there.
  • Other things?  You could make a(x)'s scope visible to m(x), I suppose, although that sounds like it's a fairly involved, and potentially hairy answer.

It seems the answers are basically either 1) add the parameter to all the functions and everything that calls them, or 2) use some kind of composite type to stash the one or more values in.


It seems that this is a reasonable common thing to have to do, so it must have a name.  How well does your language handle that kind of refactoring?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s