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