sindrenm05/07/2020, 9:29 AM
Which of the following do people prefer to get either the value or an empty string if it's null?
val string: String? = "possibly-null"
val stringOrEmpty: String = string ?: ""
val stringOrEmpty: String = string.orEmpty()
would be if we wanted to keep chaining stuff on the result. Sort of like
marstran05/07/2020, 10:32 AM
conveys the intention better.
sindrenm05/07/2020, 12:02 PM
Marc Knaup05/08/2020, 7:32 PM
if available for consistency. Makes the code more comprehensible than the cryptic
esp. in chains longer than a simple
. Also, longer chains would require parenthesis with
. Also, in other scenarios like
(a ?: "").foo()…
creating an empty value is quite lengthy.
sindrenm05/10/2020, 1:32 PM
. The same goes for lists (