Do you prefer `if (rank == null) return BadRequest...
# codingconventions
c
Do you prefer
if (rank == null) return BadRequest("Rank is missing")
or
rank ?: return BadRequest("Rank is missing")
?
k
My preference would be the first one if I wanted a statement without a result, or the second one if I wanted to use the value of
rank
.
same 3
d
🦖 is a preference a convention?
r
my preference is to not use
null
to begin with, but rather to model the domain, so domain errors are communicated nicely, using the Either-monad i.e.:
Copy code
sealed interface RankError
data class RankMissingError(val rankId: RankId): RankError

fun getRank(rankId: RankId): Either<RankError, Rank>
that way the model communicates your domain in a clear and consistent way, one starts adding relevant metadata (such as the rank id to the error), it's expandable, without implicit and inconsistent definitions of
null
in general
null
is still an anti pattern, if for no other reason than DDD,
null
is not a domain term in most domains, in those where it is it is almost never the same as the computer science definition
âž• 1