mrmans0n / compose-rules

Lint rules for ktlint/detekt aimed to contribute to a healthier usage of Compose. Actively maintained and evolved fork of the Twitter Compose rules.
https://mrmans0n.github.io/compose-rules
Other
525 stars 19 forks source link

Add check for incorrect lambda names i.e. onClicked or onCheckChanged #266

Closed alexjlockwood closed 1 month ago

alexjlockwood commented 1 month ago

In composable functions, lambda callbacks shouldn't be named in the past tense. For example:

This doesn't seem to be mentioned in Google's official style guide, but I know it's a pattern they practice just based on what I see in their source code... and I remember reading it somewhere in the documentation that it's something they encourage but I can't seem to find it 😓

mrmans0n commented 1 month ago

Yep, it also rings a bell -- but can't find where they mention it 😅

Anyway, I had it in the back on my mind for a while so I just went and added the rule.

jamesscott-zaptec commented 1 month ago

Can anyone provide a source or logical justification for this rule? Imposing it because it "rings a bell" doesn't feel significant enough for inclusion.

The docs say "It a common mistake to use the past tense in some of these events..." without saying why it's a mistake to do this, or linking to a relevant argument; which is not conducive to eager adoption.

mrmans0n commented 1 month ago

It's mainly an opinionated style / consistency choice, like most of other the *Naming rules already present. I am all in in this one in particular, stemming from multiple discussions at work in the past two years about the naming of lambdas.

I agree that the language in the docs can be improved (as it's not really a mistake, just a style recommendation), I'll update that when I have a minute.

And as always, If you don't want a particular rule to run in your codebase you can always disable it in your linter of choice 😄

jamesscott-zaptec commented 1 month ago

Thanks @mrmans0n. I don't disagree with the rule and I'm sure there's good reasons, but there would be value in exposing the justification for other's benefit when you have chance to update the docs to make it feel less arbitrary 👍.