Search code examples
androidkotlintextutils

Kotlin: isNullOrEmpty vs TextUtils.isEmpty


In Kotlin (Android project), which is preferable to use ?

isNullOrEmpty or TextUtils.isEmpty and why

I believe they do the exact thing and are "null" safe, right ?


Solution

  • Based off of my question here

    If your project will be ported to other frameworks, making use of isNullOrEmpty will probably result in a safer experience, because it's based on Kotlin (specifically package kotlin.text) and not on package android.text;

    Why is this safer ?

    Well, the implementation of isNullOrEmpty might change depending on the platform but it will be available to use and is part of Kotlin, compared to TextUtils.isEmpty being only for android.