How to write this method in better way? ```/** Ret...
# codereview
k
How to write this method in better way?
Copy code
/** Returns the signed in users ID as a `String` or `null` if no ID in shared preferences found (or `0`). */

fun SharedPreferencesManager.userId(): String? {
    val userIdLong: Long = this.loadCurrentAdminPsid()
    return if (userIdLong == 0L) null else userIdLong.toString()
}
e
implementation looks reasonable if that's the specified interface. if you want to abbreviate it, you could
Copy code
fun SharedPreferencesManager.userId() =
    loadCurrentAdminPsid().takeIf { it != 0L }?.toString()
👍 1
k
Thank you let me try it
e
IMO original is easier to read and understand for me. I would probably drop type in declaration and that would be it.
k
Yeah 👍
m
Copy code
fun SharedPreferencesManager.userId() = loadCurrentAdminPsid().let { 
    if (it == 0L) null : it.toString()
}
Of course it would be helpful if you could refactor
loadCurrentAdminPsid()
to return null if the id is invalid. And ideally you would use a value type for the id.
👍 1