3 ms·
My favorite Java code I've ever seen is: @Nullable Optional<Boolean> foo; For when 3 values for a boolean just aren't enough. Here are two rules I learne
by jmyeet 1y ago
My favorite Java code I've ever seen is:
@Nullable Optional<Boolean> foo;
For when 3 values for a boolean just aren't enough.
Here are two rules I learned from data modelling and APIs many years ago:
1. If you don't do arithmetic on it, it's not a number. ID int columns and foreign keys are excluded from this. But a phone number or an SSN or a employee ID (that is visible to people) should never be a number; and
2. It's almost never a boolean. It's almost always an enum.
Enums are just better. You can't accidentally pass a strong enum into the wrong parameter. Enums can be extended. There's nothing more depressing than seeing:
do_stuff(id, true, true, false, true, false, true);
This goes for returning success from a function too.
- kelnos 1y ago> @Nullable Optional<Boolean> foo; To be (somewhat facetiously) fair, that's just JSON. The key can be not-present, present but null, or it can have a value. I usually use nested Options for that, not nulls, but it's still annoying to represent. In Rust I could also do enum JsonValue<T> { Missing, Null, Present(T), } But then I'd end up reinventing Option semantics, and would need to do a bunch of conversions when interacting with other stuff.