https://kotlinlang.org logo
#detekt
Title
# detekt
p

Peter

11/15/2023, 8:15 AM
is there a rule, that would check for unnecessary functions in final classes? For example
isInputValid()
:
Copy code
data class EmailSignUpState(
    val emailValid: Boolean = false,
    val passwordValid: Boolean = false,
) {
    fun isInputValid(): Boolean {
        return emailValid && passwordValid
    }
}
b

Brais Gabin

11/15/2023, 8:17 AM
There is a rule that forbids functions on data classrs
m

marschwar

11/15/2023, 8:17 AM
Why do you consider this function unnecessary?
👍 1
p

Peter

11/15/2023, 8:18 AM
hm, I think there are some use cases for functions, sometimes
because all fields are final?
Copy code
val isInputValid = emailValid && passwordValid
I felt like that makes sense 🤔
m

marschwar

11/15/2023, 8:21 AM
That is true but to me this feels very nitpicky to prohibit this. How big is the performance impact really?
p

Peter

11/15/2023, 8:23 AM
probably no performance gain. Maybe more like a code style choice. More compact and less brackets, dunno 🙂
b

Brais Gabin

11/15/2023, 8:25 AM
But even if they are not final you can do the same using
val ... get()
. Any function without parameters can be replaced with a property
But, anyways I feel that something like that would be a very controversial role so a third party rule seems the place for it.
p

Peter

11/15/2023, 8:34 AM
I guess it could still be a rule that is disabled by default, if my use case seems valid to you
b

Brais Gabin

11/15/2023, 10:01 AM
To me it is not valid. I mean, I don't see any problem with that function. Probably I would write it as an extension function or extension property.
👍 1