Say you have some (state) class `Foo` and you want...
# compose
e
Say you have some (state) class
Foo
and you want to create a
@Composable fun
to build some UI for
Foo
instances. How would you name it? • `Foo`: note that this is the same name as the
Foo
class. This is not an error (if class and function are in different namespaces), but it could be confusing. •
FooView
FooComposable
Something else? Are there guidelines for composable naming?
👀 1
c
I call my state class as
FooState
. Then the composable is just
Foo
. But that's just me. I'm interested in how others approach this naming.
1
e
Sometimes you also see
FooItem
,
FooRow
or
FooColumn
when the composable is to be used in some list/grid/column/row
But I don't like that, because it couples the name to its callsite, while it might be reused in a different composable or on its own
f
Not sure how well would it work but what about
invoke
operator function? 🤔 If you have instance of your Foo class, you would just call
foo()
.
e
As an extension to the class that might work, but I wouldn't put it in the class code as that would put UI code in a class that holds some state.
It would break the convention of naming composable functions with a capital letter, though
f
No, I don’t think it would. If you declare a composable lambda function or parameter, you also name it with small letter. This would be the same.
s
Personally, one of
class Foo / fun Foo
class FooState / fun Foo
In general,
@Composable State.invoke()
is fine if weird, though if you do this be aware of the @Composable <=> class isomorphism • remember = val prop • remember { mutableState } = var prop w/ mutable state • fun params = ctor args
Basically, a composable function invocation creates an instance of a thing that is not a class but has all the same features.