Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

Why storing variables inside a variable?

+0
−4

In the past I have read about the fact that some programmers use a variable to hold two or more variables (instead holding a mere value or an array).
There might also be cases when programmers use a variable to hold just one other variable.

What could be a good typical simple example for the need in this practice in programming in general? What is a typical problem to solve with this?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

More context might help (2 comments)

1 answer

+8
−0

Some variables make sense to bundle together.

For example, if you have a graphics method to draw an image, you will need to pass in an X and Y coordinate for where to draw the image. Those are two separate variables.

DrawImage(double x, double y, Image img);

But, logically, they're a single point. So it might make sense to have them bundled together into a single variable with an X and Y component.

DrawImage(Point p, Image img); // Point has the x and y

Then you can work with them together, such as applying a translation or other common graphics transform on the point. In fact, in a graphics package, you'll routinely see methods like this having multiple ways to call it depending on whether you want to pass in X and Y as separate variables, bundled together in a point, or possibly bundled with width and height as a 4-part rectangle.

DrawImage(Rectangle r, Image img); // Rectangle has x, y, width, height

I do not recommend passing your x & y together as an array. While simple here, code like that becomes harder to maintain over time as developers need to remember what each index in the array is supposed to represent.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »