Open sindrenm opened 8 years ago
Even if the second looks more natural, since we use kotlin I think we should go for the real stuff which is the first one. For an easier use, we should create an extension like this :
fun String?.isNotNullorBlanck() : Boolean {
return this?.isNotBlank() ?: false
}
and then use it like this :
if (swag?.isNotNullorBlanck()) {
println("Hello, world!")
}
We could create an extension like that, but I don't really think it's even necessary to do so, the swag?.isNotBlank() ?: false
version is readable enough for me. Might be because I'm used to doing something like this in JavaScript, though, since in JavaScript, null
is “falsy” when used in conditionals:
if (swag || swag.isNotBlank()) {
...
}
It's not the same, but it “feels” similar. :smile:
As far as I can see, there are two main ways of doing
null
checks inif
statements and the like:Which one do you prefer? Albeit a teensy bit strange, I definitely think I prefer the second example with the Elvis operator.