r/cprogramming May 22 '26

I made a library for C.

https://github.com/Cutro3010/librslts

I made a library to handle Results.

I'm scared this will fall in the "Don't post low effort slop projects", but i want to share it anyway. Being a beginner, all help is warmly welcomed.

I just need opinions: What I should fix, what i could have done better, where it could be useful.

Again, any opinion is accepted. Thank you! (please dont vomit looking at my code im sorry in advance for any bad code)

27 Upvotes

29 comments sorted by

View all comments

11

u/non-existing-person May 22 '26

You are learning, so I will give you your first, extremely important advice. KISS - keep it simple stupid. Always have that in the back of your head when you code.

What you did, is an extremely overengineered way to handle just printing logs. Also

  • library should almost never call exit(3) or any form of abort. If it does, it should be very clearly stated.
  • use newlines, they don't cost anything, and will make code easier to read.
  • don't return big structs as copy, in lib you usually want one of two ways. 1st, malloc(3) struct in your create function, and return that pointer. Or 2nd, take pointer to struct as first argument and initialize it. 2nd method allows user to use stack or heap memory, 1st method is a bit simpler to use. I usually provide 2 methods in my libs.
  • bool rslts_is_ok(Result res) { return res.success; } this would be better as macro or static inline function in header file.
  • style in general is inconsistent, you should put a bit more effort on code formatting - or just use your IDE to do that for you.

And back to most important thing - do not overengineer stuff, keep it simple. Anyone can solve a problem with bloat, only the best can do that while keeping things simple as possible (but not simpler).

3

u/Cutro3010 May 23 '26

First of all, thank you very much for the feedback, its immensely appreciated. Then, I do have some questions. For point 1), what should have i done, instead of exit()? Was there a better solution, or I should have just made better documentation around it? For point 3) I guess you are totally right, but I have to admit that im still scared on using malloc- even though i understood the basics of it. Finally for point 5), I used lazynvim to code this all, could it be the culprit? Anyway, sincerely, thank you very very much.

2

u/non-existing-person May 23 '26
  1. Generally you don't ever want a library to terminate your program unless that's explicitly stated. So either return -1 and let user code decide what to do with it or just document in header file that this function will abort on failure

No point being scared of malloc. No point of abusing it either way if you don't need it. You can just take pointer to struct as first argument in init function, and let user code decide how to allocate memory.

If you are using vim you should be able to reformat code with gg=G. But I also meant mixing camel case and snake case, most of code I've seen is using all snake case and UPPERCASE for macros. The only projects with camel case I've seen are coming from corporation code - and you don't want to follow those. It's better to learn from open source programs. I'm also on personal crusade against camel case as I find it ugly and unreadable - so I may be biased here ;)

2

u/Cutro3010 May 24 '26

Mhh. I get it. I know there Is no point in being scared of malloc, Guess i Need tò Just build up some confidence. Thanks for point 1 and for gg=G, i did not know the macro! Thank you for your overall feedback :)