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
13
u/pjl1967 May 22 '26
You might consider putting a better description in this post. "Handle results"? Results of what? Handle how?
12
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
exit(3)or any form of abort. If it does, it should be very clearly stated.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.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).
2
u/Afraid_Lie_9340 May 23 '26
Hey, I tried sth similar, and you can look at it...hopefully u can benefit from it : https://github.com/UniquePython/betterr
2
u/PressWearsARedDress May 22 '26
Might be useful for propagating errors up a stack...
Speaking of that... I would want to chain this up a stack but your structure consumes over 80B and it lacks the ability to combine results from other functions in the stack. But there is a problem ... I cannot chain up the stack because of lifetimes being associated with the lifetime of the function so you would need explicit lifetime controls with allocations.
Otherwise you are competing against a integer style error code. I can put 231 codes in the return that specifies the non-trivial branch that the code took.
1
u/Ngtuanvy May 23 '26
It is completely fine and you don't have to be self concious about your code. One thing I want to tell you in advance though, utility C libraries almost never have their place, since having dependency is complex, so most C libraries are very essential ones. Though it doesn't really matter as a learning project, I used to do those too.
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.
0
u/No_Recognition_2275 May 24 '26
Actually a good project tho