@efemoney for some reason I thought
checkNotNull(x)
was a shortcut
check(x != null)
, but it does in fact return the non-null value, my bad. Both
checkNotNull
and
error
throw ISE, so that is not a distinguishing factor (although I agree in case we need IAE,
require
variants would be nessary).
Still, in the case where ISE is needed, I do prefer this:
val sha = System.getenv("GITHUB_SHA") ?: error("GITHUB_SHA is missing")
Over this:
val sha = checkNotNull(System.getenv("GITHUB_SHA")) { "GITHUB_SHA is missing" }
Because we can more clearly see the interesting expression in the happy path, as the error case is handled separately, while using
checkNotNull
kinda mixes both. Similarly, a git diff showing the addition or removal of the check would be more readable (IMO) using the elvis because the whole error case is contiguous in a single piece of code.