Hello folks, in case i have a function which check...
# naming
g
Hello folks, in case i have a function which checks for a precondition based on authorization access should i postfix it with
orThrow
or it's ok to omit it? For example:
Copy code
private val CspAccount.retrieveAuthenticatedDevice: CspDevice
    get() = this.authenticatedDevice.getOrElse {
        throw WebApplicationException(
            "Authenticated device for account: ${this.number} was not found",
            Response.Status.UNAUTHORIZED
        )
    }
It would be better if i renamed it to getAutheticatedDeviceOrThrow()? Thanks in advance for any answers !
m
Maybe a val?
account.authenticatedDeviceOrThrow
s
definitely a function -- vals shouldn't throw
g
i would add the
orThrow
part as it is. If you wanted to omit it i would expect the return type to be
Result<CspDevice>
k
I would go with consistency with existing libraries. For example kotlin has a
Map.getValue()
- it's not getValueOrThrow. I would therefore not add OrThrow.
s
the
getValue
is for consistency with Java, or for compatibility with delegates
k
I was referring specifically to the overload
fun <K, V> Map<K, V>.getValue(key: K): V
- this is unrelated to delegates, and I don't see any consistency with Java, since the Java Map interface doesn't have a "get" that throws.