``` site = <http://dto.site|dto.site> ...
# announcements
x
Copy code
site = <http://dto.site|dto.site> ?: <http://this.site|this.site>!!
is this the best way to say use first if not null else use second if that also is null thow?
k
i would use:
site = <http://dto.site|dto.site> ?: <http://this.site|this.site> ?: throw FooEx()
i suppose that's a matter of style, but any time I use
!!
and it fails it's a programming error
x
fair, in this case it would be a configuration error
k
do you have a better exception to throw than an NPE?
👆 1
s
that’s really the crux of it, if you can’t proceed any further past that point if the value is null, throw an exception such that a human can understand what went wrong
c
I’ve been growing fond of using the
check
or
checkNotNull
stdlib functions for this, to give some more info in stacktraces
site = checkNotNull(<http://dto.site|dto.site> ?: <http://this.site|this.site>) { "Site cannot be null" }
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/check-not-null.html
💯 2
k
I like those where in Java I'd be using something like
Preconditions
from guava
s
Or, if 'null' is a proper and expected, but incorrect, input, you may want to properly model both good result and the possible errors. I would use something like an
Either<E,T>
as the type of this expression.
o
you can do
?: error("Message describing what was expected")
too, if you don't mind throwing an IllegalStateException