https://kotlinlang.org logo
#compose
Title
# compose
d

Daniel

06/16/2021, 1:20 AM
Should I name a composable function that "returns" values through out parameters with a uppercase or lowercase name? I have a function like so
Copy code
fun updatePreconditions(activity: Activity, playServicesPreconditionMetState: MutableState<Boolean?>, settingsPreconditionMetState: MutableState<Boolean?>): Unit
It "returns" by assigning to the states, which I think is cleaner in this case than having generic callbacks. Android Studio tells me I should write it
UpdatePreconditions
, but I'd assume something named like that affected the visible ui
a

Adam Powell

06/16/2021, 3:31 AM
the naming convention is one of declarative entities; in this case you're declaring the presence of a kind of actor. Something like
PreconditionUpdater
might be appropriate.
I would also give the state of this thing its own type rather than accepting several generic
MutableState<T>
objects - it's kind of like how in most cases it's a bit janky to declare functions that accept or return
Pair<X, Y>
- yes it technically works, but it doesn't really carry a useful semantic meaning
alternatively, yes, use callbacks to pass new data back up
but depending on the nature of the preconditions themselves and how they change, updating state may be more semantically appropriate
d

Daniel

06/16/2021, 4:19 AM
Thanks, that's really helpful. I like the idea of thinking of it as an actor. I think I'll split it up into two composables instead of making a type because the preconditions are logically separate and don't consitutue all the preconditions of the caller
a

Adam Powell

06/16/2021, 1:12 PM
Something else to keep in mind is to avoid backwards writes during composition itself. If your update function here is mutating state directly while composition is running as opposed to in Effects or similar callbacks after composition completes, you can get into trouble if you have code reading those same state values earlier in the composition
Composition doesn't backtrack
This may be a reason to have this function behave more like updateTransition and return something read-only instead, since that kind of shape precludes the possibility of backwards writes without really going out of your way
Ideally you should avoid writing this state while composition is executing anyway