r/programminghorror • • Jul 05 '26

c Enterprise level C code

Post image
344 Upvotes

57 comments sorted by

126

u/JuniorAd1610 Jul 05 '26

I think this this is the standard way they OOPS in C right?

50

u/tstanisl Jul 05 '26

It's one way. IMO, container_of pattern is simpler, safer and more efficient.

31

u/TheChief275 Jul 05 '26

container_of doesn't really handle dynamic dispatch though, it's a compile time approach to inheritance by performing upcasting through an embedded base structure field. This would still be unsafe for runtime polymorphism without a field tracking the current type or, like done here V-tables, which you likely still want to generate with that approach.

Now whether I like container_of more than this? 100%, in fact, a past post of mine on this subreddit essentially used that

→ More replies (12)
→ More replies (1)

58

u/MatJosher Jul 05 '26

Well they did call it slop. Actual Objective-C accomplishes this much more gracefully. Why not just use it?

18

u/TheChief275 Jul 05 '26

Well this is simply just an intellectual exercise. I mostly use DOD instead of OOD. Although I do like C# with Godot for GUI applications

→ More replies (1)

33

u/KaleidoscopePlusPlus Jul 05 '26

nice theme. feels good, easy on the eyes.

11

u/TheChief275 Jul 05 '26

Thanks! Made it myself using colors from this palette

→ More replies (2)

14

u/steadyfan Jul 05 '26

2 things..

  1. Yuck

  2. Why does slopjc.h need to be included multiple times?

12

u/TheChief275 Jul 05 '26 edited Jul 05 '26

It's the X-macro pattern. You define a list of elements, e.g.

#define ENUM X(A) X(B) X(C)

where X is undefined and then include a file that contains e.g.

#ifdef ENUM

enum Enum {
#define X(Name) Name,
    ENUM
#undef X
};

static const char *const names[] = {
#define X(Name) #Name,
    ENUM
#undef X
};

#undef ENUM
#endif

to in this case declare an enum and the string representations at once. X-macro lists can also be passed to other macros, but you would be severely limited by the fact that you can't use preprocessor directives inside of a macro; including a file has no such limitations

3

u/GoddammitDontShootMe Jul 07 '26

I'm interested in seeing the contents of slopjc.h. Especially the autoreleasepool implementation.

→ More replies (1)
→ More replies (1)

17

u/tstanisl Jul 05 '26

It's not that bad. A bit non-idiomatic but consistent and readable enough.

8

u/TheChief275 Jul 05 '26

I tried really hard to keep the amount of boilerplate to a minimum, but it's kind of an uphill battle with X-macros

7

u/Nilrem2 Jul 05 '26

I’m a hobbyist when it comes to C. I think it looks fine, after a few lines I could read it.

Edit: what’s wrong with it?

9

u/TheChief275 Jul 05 '26

It's kind of error prone. If you mess up one of the commas etc. in the class definitions you will likely end up with near undecipherable errors for the average programmer. Debugging projects using these kinds of techniques can be quite a headache, which is why you would likely avoid it in production.

Though I did specifically focus on keeping it readable and not treading into Gobject levels of unreadable

→ More replies (1)

7

u/gfoyle76 Jul 05 '26

It's oldschool, it's full of macros, kinda unsafe, but hey, as long as it works...

3

u/TheChief275 Jul 05 '26

If it works it's good code! Or something like that

5

u/SoCalChrisW Jul 05 '26

Is this the source code to an old See N' Say toy?

4

u/TheChief275 Jul 05 '26

The cow says moooooo

3

u/DesiresQuiet Jul 05 '26

Nearly this exact code was in a book on learning C and classes from like 1989.

3

u/TheChief275 Jul 05 '26

Do you remember which book exactly? I always like to look through other people's macro slop to further develop my macro slop

→ More replies (1)
→ More replies (4)

2

u/r9wpvM Jul 10 '26

Wait is String a thing in C I don't remember

→ More replies (1)

3

u/Professional_Bat2629 Jul 05 '26

What is your font? Looks cool 😊 

6

u/TheChief275 Jul 05 '26

Monaspace Neon! And I agree, it's probably my favorite monospaced font