4 ms·
errno was one of the worst ideas ever.
by tzahola 9y ago
errno was one of the worst ideas ever.
- gok 9y agoDon’t know why people are downvoting this; errno is truly horrible.
- oneweekwonder 9y agoBecause no alternative is suggested. What could have been, give a url? Just saying {x} sucks... well sucks.
- asveikau 9y agoErrors as return codes are harder to clobber as a side effect. Some examples, on Windows you have HRESULT or NTSTATUS, on Mac you have osstatus. Some posix functions also return errno values, eg. pthread_create, likely due to early perceptions that errno cannot be made thread safe. (Fixed these days by making errno into a macro that resolves to a function that does thread local storage.) Probably fans of higher level languages might prefer exceptions, but this would be inapplicable to C. Some frameworks (glib, parts of cocoa) have a concept of "error object" which is an extra pointer to a struct parameter that can receive rich error context.
- mwfunk 9y agoOK, put another way, errno is using a global variable to return an error code from a function call. Alternative: don't use a global variable to return an error code for a function call.
- emmelaich 9y agoBad implementation*, not so much a wholly bad idea. Being sometimes a macro, sometimes not, not threadsafe? And more.
- tzahola 9y agoWell... Yeah, there's some merit to errno: it lets you handle errors. Otherwise it's a horrible piece of crap: - It's a global (sorry, thread-local) variable, updated behind the scenes. - You can't tell whether a function will update errno or not by its signature. You have to consult the documentation. - You can't tell the range of possible errno values unless it's explicitly documented (and the documentation is kept in sync with the implementation). - Errno only communicates error metadata; the success/error bit has to be communicated in-band via the return value (e.g. printf returning < 0). - Except for some functions which return void (or like readdir, which returns NULL when reaching the end of the directory, or on error) and set errno on error. - In which case you better set errno to 0 before calling the function, because while these functions set errno on failure, they not necessarily set it to 0 on success...