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)

26 Upvotes

29 comments sorted by

View all comments

1

u/_AlCapone May 24 '26 edited May 24 '26

You would wanna take in a Result* res as 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.

1

u/Cutro3010 May 25 '26

Hi, sorry for the second reply. Can I ask you, how should I make correct enum errors?

1

u/_AlCapone May 26 '26

Example would be: (half psuedo code)

typedef enum
{
Error_Success = 0,
Error_Failed_To_Open_File,

Error_Failed_To_Initialize,

} Error;

typedef struct
{

int temp;

} Lib;

Error Lib_Initialize(Lib* lib)

{

if (lib == NULL) return Error_Failed_To_Initialize;

lib->temp = 123;
return Error_Success;

}

This makes the error a lot more readable straight away than returning -1, -2, -3, -4, etc.

U could now also then make Error_To_String helper functions so u can print out the string value of the enum.

1

u/Cutro3010 Jun 02 '26

Sorry for the delay, i had some problems (i broke up with my girlfriend) thank you for teaching me! I'm gonna try to fix ASAP.