8 ms·
What If I Don't Actually Like My Users? (2008)
- crispytx 10y agolol
- deleted 10y ago[deleted]
- B1FF_PSUVM 10y ago> because I know I'm not as smart as I think I am. They should print that into every mirror manufactured.
- metaphor 10y agoReminds me of a comment included in code that I wrote a couple weeks back: // ... Function strcmp() returns 0 on match, therefore, !strcmp() is TRUE on match. I deliberated with my inner voice for a minute on whether to include a comment or not. Ended up including the above thinking 18 !strcmp() statements looked strangely illogical in context even after just writing it.
- MaulingMonkey 10y agoI always go with strcmp(...) == 0 instead of !strcmp(...). This better reinforces my mental model of it (like many comparison functions) having 3 possible return values: <0, =0, or >0. !strcmp(...) may be terser, but it is illogical in that logical not isn't an operator. I dig using it on booleans, or "booleanish" values (null vs non-null pointers), but this conceptual tri-state isn't even "booleanish". But C didn't have a proper bool type for awhile, and C++ inherited it's baggage, so they'll compile it. Meanwhile C# doesn't even think my "booleanish" tests are logical enough to allow to compile - no null reference tests with "operator!" for me.
- hcs 10y ago> -10. It's impossible to get right. char * gets(char * s) ?
- JoshTriplett 10y agoAbsolutely. Also any interface in C or POSIX specified to return a pointer to local static memory, such as "char * basename(char * path)" (and dirname), or "char * ptsname(int fd)", or "char * asctime(const struct tm * tm);".
- wyldfire 10y agoDon't most implementations at least use TLS for their implementations to preserve a modicum of sanity there?
- JoshTriplett 10y agoSome of them do, though not all. And that also doesn't fix problems with signals and similar. But for all of those functions, glibc and some other C libraries provide reentrant variants like "basename_r" and "ptsname_r".
- Someone 10y agoThere's also the write call that succeeds as soon as it can write a single byte, regardless of the byte count passed in. For example, https://en.m.wikipedia.org/wiki/Write_(system_call)#POSIX_usage https://en.m.wikipedia.org/wiki/Write_(system_call)#POSIX_us... warns "The write function returns the number of bytes successfully written into the array, which may at times be less than the specified nbytes" Yet, the example following it (https://en.m.wikipedia.org/wiki/Write_(system_call)#Usage_Example https://en.m.wikipedia.org/wiki/Write_(system_call)#Usage_Ex...) doesn't handle that case or error codes such as EINTR. I remember reading a text describing how to properly use that call, but cannot find it anymore. Faulty examples like the one above are easy to find, though. For example, http://stackoverflow.com/questions/14417806/are-posix-read-and-write-system-calls-atomic http://stackoverflow.com/questions/14417806/are-posix-read-a... and http://stackoverflow.com/questions/10650861/atomicity-of-write2-to-a-local-filesystem http://stackoverflow.com/questions/10650861/atomicity-of-wri... are good questions, but no answer points out that, even if write were atomic, it would not do you any good, as you may have to call it multiple times. It probably is not that difficult, but I think it would take me a day to wrap this function in something that doesn't explode in your face, and be confident that I did it correctly, and even then, I would wake up in bed wondering whether I handled all edge cases correctly.
- busterarm 10y agoI tallied about a -11 using the Youtube Player API today.
- lstamour 10y agoNote the "Hard to Misuse" list is at http://ozlabs.org/~rusty/index.cgi/tech/2008-03-30.html http://ozlabs.org/~rusty/index.cgi/tech/2008-03-30.html
- juped 10y agoI think git usually sits around a -6 or -7.
- kazinator 10y ago> Here's my favorite (now fixed) example, from the glibc snprintf man page: Not fixed is that the ISO C wide character version of the function (swprintf) specifies a gratuitously different return value convention from from that of snprintf.
- stcredzero 10y agoI once had to use a C API where you had to guess the size of of the buffer of data you were going to get back, and keep guessing until your number was compatible with the amount of data. So I did what any good CS person would do, and started with a small, reasonable number, then doubled it until the API was happy. Was this documented? No. Everything but how you were supposed to specify the buffer size was documented. However, this code worked in production, and AFAIK, it's still running in production to this day.
- userbinator 10y agoTo be fair, without knowing what that API does, I could see a situation where it itself doesn't know how long the data would be. Parsing free-form char-terminated data, especially over the network, is one example that comes to mind, and another is decompressing compressed data in a self-delimited stream; in general, data whose length cannot be determined apriori. Furthermore, the API probably doesn't want to allocate and buffer the data itself since it could be very long, nor does it want to impose a fixed limit since the application should decide that.
- stcredzero 10y agoTo be fair, without knowing what that API does, I could see a situation where it itself doesn't know how long the data would be. This was to get a message out of the API. At the point one is making the call, it does know how long the message is. (Having already gotten a callback.) It looks like the author forgot to write the function that would return the length, then improvised. Furthermore, the API probably doesn't want to allocate and buffer the data itself since it could be very long, nor does it want to impose a fixed limit since the application should decide that. This was probably the case.
- xyzzy123 10y agoLots of win32 API calls work this way ><
- mnarayan01 10y ago
- pdkl95 10y ago> -8. The compiler will warn if you get it right. > bind() socket library call ... takes a struct sockaddr [when you are usually using a struct sockaddr_in] Except the compiler is correct. If you want to use a polymorphic type in a generic call that requires the parent type, you should include a cast. From the example in my man page for bind(2): int main(int argc, char *argv[]) { int sfd; struct sockaddr_un my_addr; /* ... */ if (bind(sfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_un)) == -1) handle_error("bind"); /* ,,, */
- sjolsen 10y agoI don't think the complaint is that the compiler won't accept a struct sockaddr_in* where the API expects a struct sockaddr* . I think the complaint is that the API is designed in such a way that obtaining the required struct sockaddr* involves casting between incompatible types. There are more robust ways of doing type erasure, even in C.
- userbinator 10y ago> I've been coding in C for about 20 years, and about five years ago I spent an hour chasing a case where I'd done if (strcmp(arg, "foo")) instead of if (!strcmp(arg, "foo")). This felt completely intuitive to me the first time I saw it: a comparison of characters can result in "less", "greater", or "equal" much like a subtraction would, and if this is expressed as an integer, < 0, > 0, and == 0 are a very sensible set of possible alternatives, and of those the only one that makes most sense as "equality" is the == 0, which is idiomatically expressed as !. I think if(strcmp(...)), if(!strcmp(...)), and ditto for memcmp() and related "3-value" comparison idioms might be one of those things that divide programmers into two groups: one that instantly sees the elegance of this design and thinks it's so natural and obvious, and one that gets eternally confused by it.
- rdtsc 10y agoVisually it is confusing. The goal is to test for equality but there is the "!" (not) operator in there, so my brian translates it quickly to "not equal" That is why I always write == 0 on comparison even if it is longer. It removes a negative and makes it clear.
- userbinator 10y ago> The goal is to test for equality If it helps, you should think of strcmp() as returning a difference. strcmp() means "string comparison", and to compare is to find differences. Thus the "not" means "no difference", just as the idiom "if(!p)" means "there is no (object pointed to by) p". That is why I always write == 0 on comparison even if it is longer. It removes a negative and makes it clear. Seeing "== 0" for that in C for me is actually like seeing the equivalent of "== false" in languages that have booleans: the extra comparison causes a bit of pondering at whether it was really what the programmer intended. The if(...) is already an implicit comparison with 0/nonzero. I've seen things like this too, presumably written by someone who wanted to make the comparison really explicit, but just ends up causing more confusion IMHO: if ((strcmp(a, b) == 0) != false) { /* does this code get executed when the strings are equal or different? */ }