Our Java code use in-house developed helper functi...
# stdlib
n
Our Java code use in-house developed helper functions
hasContent
and
noContent
which are overloaded for strings, collections, maps, stringbuilders, ... and treat
null
as "no content". While switching to Kotlin, we try to use as much of Kotlin stdlib methods as possible. Our
noContent
easily translates to
isNullOrEmpty
, but
hasContent
results in
!isNullOrEmpty
which is not pretty, especially if that is at the end of a chain because it separates the
!
from the method:
!foo.bar.baz.isNullOrEmpty()
. Is there a place for
isNotNullOrEmpty()
in stdlib? (And yes, I realize that once we finished our journey from Java to Kotlin, we can drastically reduce the usage of nullable types and thus often use
isNotEmpty()
. But that is still a long and windy road). Update: I should have searched for this in YouTrack before asking: was rejected in KT-33689 because it claimed we could "shortly" write `foo.bar.baz.!isNullOrEmpty()`as described in KT-5351 . I guess that time will only come though after K2 went GA.
j
Your proposed
isNotNullOrEmpty
requires mental parenthesis which makes it open to interpretation
In other words, is it
(!null) || empty
or
!(null or empty)
? Different people will read it different ways.
Of course reading it as the former is wrong because the first condition always produces a deterministic value for the second, but you have to think about it.
n
I'm struggling to find a use case for the first interpretation, but of course we could also solve that using a different name like
hasContent
. But anyway, that proposal was already rejected 2 years ago.
Also, the same argument could be made for the recently added
firstNotNullOfOrNull
which could be parsed as `firstNot(NullOfOrNull)`or
first(NotNullOfOrNull)
. Only the latter makes sense (to me), but thinking is required...
j
It's actually
(firstNotNullOf) || null
there
n
yeah, right. I confused myself...
u
Not exactly my taste, but you can always write
foo.bar.baz.isNullOrEmpty().not()
n
Possible but not really "tasty" for me either