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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

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?

History
Why does this post require attention from curators or moderators?
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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »