Hi it possible to simplify this code in Kotlin ``...
# android
a
Hi it possible to simplify this code in Kotlin
Copy code
private fun getAmountLength(currency: CharSequence?, amount: CharSequence?): Int {
    return when {
        !currency.isNullOrEmpty() && !amount.isNullOrEmpty() -> currency.length + amount.length + 1
        !amount.isNullOrEmpty() && currency.isNullOrEmpty() -> amount.length
        amount.isNullOrEmpty() && !currency.isNullOrEmpty() -> currency.length
        else -> 0
    }
}
a
This might be more readable:
Copy code
private fun getAmountLength(currency: CharSequence?, amount: CharSequence?): Int {
    val currencyLength = currency?.length ?: 0
    val amountLength = amount?.length ?: 0
    // Add one if both curreny and amount are not empty
    if (currencyLength > 0 && amountLength > 0) 
        return currencyLength + amountLength + 1
    return currencyLength + amountLength
}
But why do you need this
getAmountLength
?
2
💯 2