Ternary operator

I’d suggest you to consider using if expression in Kotlin. This is pretty idiomatic:

return if (!response.isSuccessful()) "fail" else response.body().string()

It becomes slightly better if you flip it:

return if (response.isSuccessful()) response.body().string() else "fail"

And you if are actually designing your own Kotlin API for response object, then you can make body() return null when response is not successful, and the usage is going to be even better with Kotlin’s elvis operator:

return response.body()?.string() ?: "fail"
24 Likes