3 ms·
Null is a valid value for pointers, but it is absolutely ordinary and normal for functions to refuse to accept null on the pain of crashing and burning.
by UncleMeat 3y ago
Null is a valid value for pointers, but it is absolutely ordinary and normal for functions to refuse to accept null on the pain of crashing and burning.
- jcalvinowens 3y agoNo it isn't. In my experience, there are few higher quality signals a codebase is utter garbage than having `if (arg == NULL)` checks at the top of every function... Almost nothing in libc does that, it is entirely unnecessary. I realize that wasn't your point, but this is an enormous pet peeve of mine...
- UncleMeat 3y agoA dereference is an implicit "I will crash and burn if you hand me null" contract.
- jcalvinowens 3y agoYes, exactly. Nobody should be surprised that calling `strlen(NULL)` crashes. Checking for NULL at the top of strlen() would be stupid, which is why it doesn't do that. I've seen a lot of so-called "enterprise" code does that, and it's absolutely idiotic.
- UncleMeat 3y agoOkay. But that's totally unrelated to my original point, which is that we are already very used to functions that do not accept the full domain of a given type. "I don't accept nullptr" is not too fundamentally different from "I don't accept "0x8000..."
- kazinator 3y ago"I don't accept {nullptr, 0x8000}" is fundamentally different from "{nullptr, 0x8000} is a trap representation". If it's a trap representation, you can't even pass it to me so that I could accept or reject it.
- kazinator 3y agoNull being a valid value means it is not a trap representation. Functions in C code bases checking for null is the rare exception rather than the rule. When they do check, that doesn't turn null into a trap representation.