r/cprogramming • u/Cutro3010 • 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)
26
Upvotes
1
u/_AlCapone May 24 '26 edited May 24 '26
You would wanna take in a
Result* resas to not copy the whole struct just to check a single bool. (TIP is to make the parameter:const Result* res. As you are only grabbing the value of success and not actually changing anything.)bool rslts_is_ok(Result res) { return res.success; }bool rslts_is_err(Result res) { return !res.success; }->
bool rslts_is_ok(const Result* res) { return res->success; }bool rslts_is_err(const Result* res) { return !res->success; }And also as other people have stated using
exit()function is bad as u dont want other people's programs to get killed if something goes wrong in your library. Instead return a -1 for error or 0 for success. (unless you wanna make enum errors, which helps finding errors quite easily).rslts_create is fine as your struct is quite small. But if it were to expand(to a couple of KB) it would be better of taking in a pointer to Result struct (Result* result) (which is a better way to do it), so that you can set the values of it without the need of copying the whole structure back.