Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed rule: don't use hashCode in the item key for Lazy* Composables #374

Open
yogurtearl opened this issue Jul 25, 2024 · 3 comments
Open
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@yogurtearl
Copy link

Proposed rule: don't use hashCode in the item key for Lazy* Composables

Item key should be unique.

.hashCode() is not unique.

e.g. these all have the same hash code, even on JS, WASM, etc, same hashcode is unlikely, but not impossible in the wild.

println("aaaa".hashCode())
println("aabB".hashCode())
println("aac#".hashCode())
println("bBaa".hashCode())
println("bBbB".hashCode())
println("c#aa".hashCode())
println("c#bB".hashCode())
println("c#c#".hashCode())
@ZacSweers ZacSweers added the enhancement New feature or request label Jul 29, 2024
@ZacSweers
Copy link
Collaborator

PR welcome!

@ZacSweers ZacSweers added the help wanted Extra attention is needed label Aug 25, 2024
@ZacSweers
Copy link
Collaborator

@yogurtearl do you have a mechanism in mind to statically predict/decide if a type has a valid hashcode?

@yogurtearl
Copy link
Author

yogurtearl commented Oct 1, 2024

Proposing it should just always issue a warning if .hashcode is called from the key lambda for all types.

As Compose docs say that item keys must be unique and LazyColumn/Row throw an exception if it detects a non-unique hashcode and hashcodes are generally never guaranteed to be unique (by design) so it is never a good choice for an key.

Might work just fine until some day data from the server comes along with a dup hashcode and starts causing crashes.

Should instead find some other unique thing to use, an ID from the server, or java.lang.System.identityHashCode (if you aren't doing KMP) or a atomically incremented count fed into the state classes at construction time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants