https://kotlinlang.org logo
#codereview
Title
# codereview
j

Jakekudur

07/02/2020, 4:39 AM
how can I optimize this piece of code?
g

gildor

07/02/2020, 5:11 AM
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

Jakekudur

07/02/2020, 5:15 AM
Copy code
fun EditText.attachErrorMessage(message: Int) {
    error = if (this.text.isNullOrEmpty()) context.getString(message) else null
}
I did smth similar ))
g

gildor

07/02/2020, 5:16 AM
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

Jakekudur

07/02/2020, 5:16 AM
I like your naming of extension )) it tells more about the purpose of the ext
2 Views