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.

Post History

83%
+8 −0
Q&A Why storing variables inside a variable?

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...

posted 3y ago by Dana‭

Answer
#1: Initial revision by user avatar Dana‭ · 2021-07-28T23:07:00Z (over 3 years ago)
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.