Hi guys. Is there a way to make this code become ...
# codereview
a
Hi guys. Is there a way to make this code become shorter and efficient?
Copy code
if (TextUtils.isEmpty(name)) {
        textInputName.isErrorEnabled = true
        textInputName.error = "Name cannot be empty."
    }
    if (TextUtils.isEmpty(email)) {
        textInputEmail.isErrorEnabled = true
        textInputEmail.error = "Email cannot be empty."
    }
    if (TextUtils.isEmpty(password)) {
        textInputPassword.isErrorEnabled = true
        textInputPassword.error = "Password cannot be empty."
    }
    if (TextUtils.isEmpty(password)) {
        textInputConfirmPassword.isErrorEnabled = true
        textInputConfirmPassword.error = "Confirm password cannot be empty."
    }
    if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
        textInputEmail.isErrorEnabled = true
        textInputEmail.error = "Email format does not valid."
    }
    if (!(password.length >= 8)) {
        textInputPassword.isErrorEnabled = true
        textInputPassword.error = "Password length must be greater than 8 character."
    }
    if (!(confirmPassword.length >= 8)) {
        textInputConfirmPassword.isErrorEnabled = true
        textInputConfirmPassword.error = "Confirm password length must be greater than 8 character."
    }
    if (!(password.equals(confirmPassword))) {
        textInputConfirmPassword.isErrorEnabled = true
        textInputConfirmPassword.error = "Confirm Password does not match with Password"
    }
}
g
My suggestion Do not use TextUtils utility method
I would rewrite this completely and use some abstraction instead. Like define validator abstraction that checks passed field and return error message, than you can configure all fields with list of validators and get error without this hand-written boiler plate code and do this in much more clean and flexible way
a
@gildor would you mind to show me any sample to reference?
g
Just try to abstract this behaviour
a
Okay.
@gildor is this the method I should follow? https://www.tutorialkart.com/kotlin/kotlin-abstraction/
g
a
@gildor I'm trying to understand your code now.
Need some time to disguise.
@gildor why you need to create
interface
first then just create
abstract
class?
Can we just create
abstract
class directly?
g
Yes, you can, but interface allows you to have validator not only for single EditText field, but for anything else, for example another views or even got multiple views at the same time, or not for views at all This is just an example, main idea to abstract validators and define them instead of just copy paste the same code multiple times. Real approach highly depends on your needs and always trade off between complexity, amount of code etc Also depends on view features, for example implementation for TextInputLayout and EditText can be different I just want to show approach, where if you think about your case you often can find some way how to simplify your code and abstract inplementation from behavior