3 ms·
Seems like instead of using boolean result better API should return tri-state: space, no space, bad request
by dvh 3y ago
Seems like instead of using boolean result better API should return tri-state: space, no space, bad request
- bitwize 3y agoSo... True, False, and File Not Found?
- teddyh 3y agoReference: https://thedailywtf.com/articles/What_Is_Truth_0x3f_ https://thedailywtf.com/articles/What_Is_Truth_0x3f_
- rm445 3y agoReminds me of a joke in a John Meaney novel, a physicist discovers faster-than-light travel through a dimension with unusual properties, gaining inspiration from Java's booleans having three states (true, false and NullPointerException).
- Phrodo_00 3y agoHate to point it out, but java booleans can only be true or false. Booleans (capital B) can be true, false or null.
- dfox 3y agoMethod that is declared as only ever returning bool can also raise any exception that is an instance of java.lang.RuntimeException (which includes NullPointerException) this somewhat bizzare design which combines unchecked and checked exceptions is a source of major discussions since at least late 90's and has much to do with the current concept of null-safety.
- hedora 3y agoYeah, but using a raw boolean or Boolean is not considered good practice. They’re up to at least four states now: null, None, Some(TRUE) and Some(FALSE) Of course, if you want more than that, you can have unboundedly many by invoking new Boolean(), which guarantees its return value is unique according to ==
- TeMPOraL 3y agoThis is already the case; "bad request" is usually returned as SIGSEGV / 0xC0000005 and similar.
- asveikau 3y agoNo. Bad request here would be a recoverable, handleable error that doesn't tank the process. You are describing a failure mode for a bad pointer dereference, which generally should not be handled in-process and should take down the program. It is not reasonable for isspace(), a simple function call with no pointer arguments, to fault like that, absent unlikely events such as a hardware issue or a bad stack pointer.
- TeMPOraL 3y ago> It is not reasonable for isspace(), a simple function call with no pointer arguments, to fault like that Well, if it internally uses a lookup table and doesn't do bounds checking, then it can fail this way, and that failure will propagate to the caller. > You are describing a failure mode for a bad pointer dereference, which generally should not be handled in-process and should take down the program. Maybe this is where we went wrong? Bad pointer access isn't fundamentally different from doing a bounds check and throwing an exception. Either of them will tank the process if not handled up the stack. But when you do a bounds check solely to prevent a bad pointer access crash, you're effectively re-doing the work the OS already does for you, paying a cost at runtime just to switch to a slightly different semantics. Maybe instead of doing that, we should have the platform provide better granularity of protection (as to e.g. avoid bad pointer access targeted to overwrite some other memory), and lean on it, instead of bolting extra layers of the same thing, just Invented Here on top?
- deredede 3y agoYou can't exactly lean on the OS to detect bad pointers references because it will only work some of the time. The OS doesn't know where your array end, so the error would have to depend on whether the access is outside the memory allocated to the process. A function specification of "if the input is outside valid range, either return garbage or raise an exception" provides no value over "either return garbage or crash the program", because there is still the possibility of returning garbage.