I have recently added a HasValue function to our internal javascript library:
function HasValue(item) {
return (item !== undefined && item !== null);
}
A during a convorsation with a coworker, we came up with the idea of also adding another function that would basically just be the inverse: perhaps HasNoValue, or IsNothing If we ended up doing that we would have:
function HasNoValue(item) {
return (item === undefined || item === null);
}
function HasValue(item) {
return !HasNoValue(item);
}
However, we're not sure whether it is more readable to have both, or HasValue. Which is more readable/preferred?
A:
if (HasValue(x) && !HasValue(y))
B:
if (HasValue(x) && HasNoValue(y))
I vastly prefer A to B. "!" is a programming idiom that should be understood by all.
If !HasValue(y)
and HasNoValue(y)
are guaranteed to be logically equivalent over the entire input range of y
, then I would greatly prefer !HasValue(y)
.
I would hesitate to even have a function named HasNoValue(y)
because inevitably someone will write !HasNoValue(y)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With