how can I optimize this piece of code?
# codereview
j
how can I optimize this piece of code?
g
Depends how much you want to use this pattern, easiest way to reduce duplicated code is make this function work with input list of `Input`s
and of course extract
apply
block to a function:
Copy code
fun Input.setErrorOnEmpty(@StringRes errorRes: Int) {
   error = if (text.isNullOrEmpty()) {
      getString(errorRes)
   } else {
      null
   }
}
j
Copy code
fun EditText.attachErrorMessage(message: Int) {
    error = if (this.text.isNullOrEmpty()) context.getString(message) else null
}
I did smth similar ))
g
first part of function, before comapring new and old password just may be extracted to own function which doesn’t know about actual fields and you just pass list of them to validate
đź‘Ť 1
j
I like your naming of extension )) it tells more about the purpose of the ext