❔ How to handle a group of variables that need to stay together within a class?
How should I handle a group of data that needs to stay together within a class? The data is a set of ~20 variables from an external object that holds a large amount of data both necessary and unnecessary for the task that the class is doing (the object has ~50 variables total), so I'd like for the class to only take the data that it needs from the object. The class also needs to be able to have multiple sets of this info at once while handling each set individually, so I need to be able to make an array that has each entry contain all of the neccessary data. Also, the data needs to be able to be changed by the class, but doesn't need to be able to be changed by other classes.
What's the best way of doing the above? I've been using a struct to hold the info so far, but when I try to find a solution to this problem created by other refactoring, I keep seeing that this is apparently an awful solution and that I should turn it into a class, but since the class is never refrenced by another class outside of initially running it, I'd really like to keep the data within the class that uses it, if possible.
14 Replies
What is paragraphs
How would I make this into paragraphs? I don't see a place where I'd be able to split it.
I don't want to read it as is. That's the meaning of my comment. I'm not going to read it so that I can tell you how to make it better
Is that better? Sorry for the problem.
50 variables is crazy. Its highly unlikely whatever it's doing couldn't be split up.
"I've been using a struct, apparently bad, I should use a class"
Why is that going to make anything better?
It can be split up, but I don't get why I would. All of the data that's contained within the object needs to be attached to the object at some point or another, and is only used by that object, so I don't see the point in splitting things up when I can just have whichever classes need whatever variables just pull the ones they need from the object, and leave the rest alone.
you would because 50 fields in one type is crazy
it sounds like you already have a logical group of data that can be split out into another type
if it needs to be kept together, you can have a type composed of other types that logically separate the data
Why is it crazy? Then again, I do kind of see your point. I do use structs a couple of times within the object to make things easier to access from other classes. But then I just loop back to preferring to keep the data contained within the main object, since that specific set of data is only ever held by that specific object.
i don't know what kind of data you're working with, but i don't think i've ever had a project with that much data that belongs in the same "level"
structs don't make things like this easier, especially if they're so large you might be causing unnecessary performance overhead with the copies
Cognitive load
I'm working on an RPG project. The object I'm working with represents a playable character. It holds their stats, specific characteristics, level up data, skill up data, and references to all of the assets they use.
those all sound like things that can be broken down into more focused classes
especially for gamedev you're going to run into the fact that composition is just better when it comes to designing game objects
(this is why lots of game engines have entities built up from components)
I guess I can see that. Thanks for the help.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.