r/C_Programming • u/yughiro_destroyer • 13h ago
Flat structures VS nested structures?
Hello!
I have this deep conflict that has been with me for months now. How is it better, flat structures or nested structures? Or what do you prefer?
Flat example :struct entity = {int x = 20;int y = 40;string texture = "mario.png";int current_health = 100;int current_armor = 100;}
Nested example :struct position = {int x = 20;int y = 40;}struct stats = {int current_health = 100;int current_armor = 100;}
struct renderable = {string texture = "mario.png"}struct entity = {position position;stats stats;texture texture;}
I have tried to use nested structures as often as possible because that's the norm but they are becoming a pain in my experience. Mostly, it comes down to boilerplate, the risk of getting paths wrong when there's too much nesting and theoretical purity where I find that a property would've worked better in another component.
But with flat structures? I get much better results, more directness, easier debugging and less cognitive load as a property is defined in only one way, even if the names get longer or repetitive. Also, that's how computers like to think and as a CS engineer, I learned how computers think before learning how humans program so I lean towards more old school polling / procedural algorithms.
If I were to make an analogy of how that makes me feel, it would be like owning a big tools box. When I open the box, I can instantly see and pick or place the drill, the screwdriver, the screws and whatever else is there. But with nesting... if feels like after opening the tools box, that the drill has it's own mini-box, that the screws are in another box and so on. I prefer seeing the whole picture at once instead of always having to deal with involvement at micro-scales of a big thing.
Also, I noticed that older games like Doom are closer to flat structs. The thing is, my game isn't very complex in terms of gameplay and it features multiplayer matchmaking. And it's on the server side where I noticed that a flat structure would be much easier for debugging, serialisation and overall faster development speeds.
View all comments
3
u/Business-Decision719 11h ago edited 11h ago
Ideally a structure should represent data snippets that are logically grouped together, to the point you could think of them as a single "thing," with the fields acting as whatever different pieces that kind of "thing" is naturally and obviously expected to have. It's almost like a simple form of OOP basically. The struct is a custom data type so it should make sense to have values of that type (or "objects" of that "class" if you want to think of it that way). If it also makes sense for values of that type to be pieces of a larger struct then so be it.
If you're going out of your way to make a struct nested or not, then you're probably going about it wrong. If all the pieces of a certain kind of thing are fairly distinct then it might make sense as a flat struct and that's fine. You wouldn't make other structs to nest inside it it just because. It can be subjective, and maybe one person would nest something someone else wouldn't, but nested or flat is not really the goal in and of itself.
For example, having a
positiondata type makes sense. A position is a kind of "thing." I can say "give me a position." It's a place somewhere. In two dimensions it would have an x and y coordinate. I can split it apart and say "give me the x coordinate of this position." You don't necessarily have to store an entity's x and y coordinates as a position object but it's a very defensible choice to do so.A "stats" struct is a little more iffy. What are you going to give me if I tell you to "give me a stats?" Its own type name pretty much announces announces that it's just sort of an amorphous cloud of data. Why doesn't an x coordinate belong there, or a renderable texture? They just don't apparently. Someone might want to group together health and armor if there's a reason for them to go together, but it's not nearly as obvious that there's some single unit here apart from the rest of the entity. (It doesn't immediately pop into my mind to say "give me the current health of this stats" as opposed to "give me the current health of this entity.") Of course maybe it could make sense to group certain stats or to have a little namespace inside the entity object to contain them; my point is that forcing
struct entityto only be made up of other structs, to the point that you need a "wastebasket struct" to throw ungrouped data into, is not really a sensible use of structs. That doesn't mean a flat struct is inherently better just by being flat.