Immutability used to be a prohibitively costly lux...
# feed
e
Immutability used to be a prohibitively costly luxury in the old days of slow and expensive computers. Not anymore https://medium.com/@elizarov/immutability-we-can-afford-10c0dcb8351d
K 3
👏 21
t
Talking about immutability and states, what's your take about complex states handling? For example take a player that want to expose it's state (active item, playing or not, position, ...) . Pushing a large immutable object with all properties may force listeners to update part of the state that does not change. Should clients compare each substate with previous one and decide, or publish 1 state per property or something else?
e
With a reactive UI framework like Compose it sorts itself out automatically. When you push an update to a large immutable object you’ve copied only the parts of that object that did change. Now, when the object is shuffled down the UI composition pipeline the state object gets decomposed into its parts and the pieces of UI that were driven by unchanged parts of the object would detect it, avoiding being recomposed.
t
Thanks, unfortunately Compose is still far away 😞 But so I should go the full object as a base for everything and until Compose is out, split that object into smaller UI stateflows that each UI components observe.
e
Yes. That’s a way to mimic in code what compose does for you.
👍 1
c
great post. what do you think about immutability and (sql)orms? i wrote an orm with immutable data classes and i found out that locking becomes really hard. anytime someone updates an object they just make a copy so when they save it again its impossible to find out if they were using a stale copy. i guess the root problem is that the sql database is mutable.
e
That’s a complicated question. I have some hands-on experience with it I belive that is where’s an approach that is similar to Kotlin’s `List`/`MutableList` separation is helpful. E.g. have a
MutableXxx
class that you’d use to model database, but elsewhere (in caches, in data processing pipelines) send
Xxx
classes that are immutable.
c
immutable data classes work pretty well for short transactions like in a http server.