Is this a best practice? MutableStateList of with ...
# compose
c
Is this a best practice? MutableStateList of with a type that includes mutable snapshot state inside of it?
Copy code
mutableStateListOf<Todo>() and

class Todo {
  val title = mutableStateOf("")
  val completed = mutableStateOf(false)
}
saw it in this video, from someone that works at jetbrains. i thought two mutables nested like that aren't a good idea generally?
a
I would think its not a good practice
1
And I think He was just showcasing the hot reload without thinking much about code quality no 😆
s
I feel like this answer https://kotlinlang.slack.com/archives/CJLTWPH7S/p1729098662018429?thread_ts=1728992701.271499&cid=CJLTWPH7S applies here as well. It does work since both are snapshot aware objects, so it's not the issue that you may be thinking about which is explained here https://blog.zachklipp.com/two-mutables-dont-make-a-right/
c
indeed i think that was the blog post i was thinking of and i def need to read that again. lol.
s
The problem is when you got mutability which can not be tracked by compose.
☝️ 1
c
oooh. thats a good way to put it. thanks stylianos!
so this kinda ties into a question i asked a few weeks ago. I kinda wasn't sure if option 2 was seen as positive/negative. https://kotlinlang.slack.com/archives/CJLTWPH7S/p1729468938667279 but if the guy that built hot reload went with option 2... then I think so will i. 😂
a
Have a mutable property or collection that isn't backed by snapshot state is the thing to definitely avoid: that will cause a lot of unexpected headaches around "why isn't stuff recomposing properly?" Having multiple "degrees of mutability" where everything is backed by snapshot state is technically fine and have understandable behavior: now you're in the realm of what's idiomatic, what's easy to understand, and if those multiple degrees of mutability make the code easier or harder to reason about.
c
thanks Alex! always want to triple check that im not breaking any cardinal rules about compose and snapshot state. personally. i hate
copy()
and so i like the mutableStateListOf<Item> and Item has mutableState* inside of it. makes the most sense to me. 😄