1

I'm working on android studio and I got a function that gets the values from different EditText.

fun changeUserAccount(
    userName: String,
    password: String,
    confirmPassword: String,
    realName: String,
    accountEmail: String,
    applicationContext: Context
) {


}

I need to check if these strings are empty and if possible, which of them is. Except for the creation of different "if" cases or a "when", which is the best and most optimal way to do that?

5
  • Use isEmpty() for example: userName.isEmpty() kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-empty.html Commented May 16, 2020 at 13:37
  • But anyway you need to check them one by one, I want to know if it's possible to check them all at once. Commented May 16, 2020 at 13:48
  • Why are you interested in doing this? Commented May 16, 2020 at 13:53
  • Compact code, efficiency,and understandable code. Commented May 16, 2020 at 13:56
  • For compact and understandable code you can use a method that do all checks you needs. For the efficiency you cannot do better than check all variables one by one Commented May 16, 2020 at 14:02

1 Answer 1

2

You can make one generic function to check all string are valid or not.

You can take n number of strings as vararg and can check each of them by making one util or extension function.

Here is Util class

object StringUtils {

fun isAllValid(vararg args: String?) : Boolean {

    //checking all strings passed and if a single string is not valid returning false.
    args.forEach {
        if(isNotValid(it))
            return false
    }
    return true
}

fun isValid(string: String?): Boolean {
    return string != null && string.isNotEmpty() && string.isNotBlank()
}

fun isNotValid(string: String?) : Boolean {
    return isValid(string).not()
}

}

and you can use like this

fun changeUserAccount(
        userName: String,
        password: String,
        confirmPassword: String,
        realName: String,
        accountEmail: String,
        applicationContext: Context
    ) {

        //You can pass n number of strings.
        val isAllStringsValid = StringUtils.isAllValid(userName,password,confirmPassword,realName,accountEmail)

       //this returns true
       StringUtils.isAllValid("hi","how","are","you","?")


       //this returns false
       StringUtils.isAllValid("","how","are","you","?")


       //this returns false
       StringUtils.isAllValid(null,"how","are","you","?")


       //this returns false
       StringUtils.isAllValid("","how","are","you","?")
    }
Sign up to request clarification or add additional context in comments.

3 Comments

But anyway you need to check them one by one, I want to know if it's possible to check them all at once. It is possible?
yes, we can.I have some idea. I'll try and update if it is working
If you need to check which parameter is not valid, you can use return type one data class which will have one string and one boolean. and you can pass return the data class with boolean false and a string that was not valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.