r/C_Programming • • 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.

17 Upvotes

37 comments sorted by

View all comments

2

u/WittyStick 12h ago edited 11h ago

I would use nested structures. It's likely you're going to need struct position elsewhere, and it would be pointless typing (struct position){ ent.x, ent.y } each time to wrap it, when you could just write ent.position.

struct entity {
    struct position { int x; int y; } position;
    struct stats { int health; int armor; } stats;
};

Also, designated initializers will make it easier to initialize your entity without having to remember the order of fields.

struct entity ent = { .position = { 50, 50 }, .stats = {  .armor = 3, .health = 100 } };

There will be no difference in performance either way, so don't think that matters. Just use whatever you'd expect to see if it weren't your own project for example - because in 6 or 12 months, you probably won't remember what order you defined things in.

If you don't want to write ent.stats.health you can use a macro or inline function:

static inline int health(struct entity ent) { return ent.stats.health; }

printf("%d\n", health(ent));

Macros may be more suitable if you don't want separate get and set functions.

#define health(ent) ((ent).stats.health)

printf("%d\n", health(ent));

health(ent) = 200;

There's also a more boilerplate option - an opaque pointer - where you define it once and no longer need to care how the structure is defined because it is not accessible to other modules. This option lets you change the structure without breaking code that uses the header.

position.h

#ifndef POSITION_H
#define POSITION_H

typedef struct { int x; int y; } Position;

#endif

entity.h

#ifndef ENTITY_H
#define ENTITY_H

#include "position.h"

typedef struct entity_t *Entity;

Entity      entity_create (Position pos, int health, int armor);
void        entity_delete (Entity);
Position    entity_get_position (Entity);
void        entity_set_position (Entity, Position);
int         entity_get_health (Entity);
void        entity_set_health (Entity, int health);
int         entity_get_armor (Entity);
void        entity_set_armor (Entity, int armor);

#endif

entity.c

#include "entity.h"
#include <stdlib.h>

struct entity_t {
    int x;
    int y;
    int armor;
    int health; 
};

Entity entity_create (Position pos, int health, int armor) {
    Entity result = malloc(sizeof(struct entity_t));
    result->x = pos.x;
    result->y = pos.y;
    result->health = health;
    result->armor = armor;
    return result;
}

void entity_delete (Entity entity) { free(entity); }

Position entity_get_position (Entity entity) { 
    return (Position){ entity->x, entity->y };  
}

void entity_set_position (Entity entity, Position pos) { 
    entity->x = pos.x; 
    entity->y = pos.y; 
}

int entity_get_health (Entity entity) { return entity->health; }

void entity_set_health (Entity entity, int health) { entity->health = health; }

int entity_get_armor (Entity entity) { return entity->armor; }

void entity_set_armor (Entity entity, int armor) { entity->armor = armor; }

3

u/imaami 10h ago

I'd advise against typedefing a pointer like that. There are few things more annoying and counterproductive in C than types that obscure the fact it's a pointer. It's both unnecessary and unhelpful.

1

u/WittyStick 3h ago edited 2h ago

I've heard this before, and I'd agree for complete types - but for opaque pointers it really makes no difference - there is no case you're going to be deciding whether to use -> or . outside the entity.c file. There's not really any context in which it is going to matter, except maybe if you need to typedef a const version of it.

If you use pseudo-generics via macros, you're going to need a typedef pointer anyway. Eg, say you want an ArrayList(Entity). ArrayList(struct entity_t *) will most likely not work due to concatenation.

1

u/inighthawki 2h ago

I don't see why being a complete type changes what they are saying. There are still other reasons it's nice to know the real type of something. It is simply convenient and provides information to the user/developer.

Is the "Entity" type a pointer? Or is at maybe a uint32_t, which is extremely common in most ECS architectures? How does one even know that it's not a complete type? Entity could very well be defined in a header somewhere as a huge complex/complete type. You may happen to know in your specific example, but that isn't necessarily going to be true universally or other consumers of your code.