Share your thoughts about repeating multiple times...
# compose
v
Share your thoughts about repeating multiple times same things across the app/composable. In particular Dp values. We repeat quite often some very common cases, like
16.dp
e.g. (
Spacer(Modifier.height(16.dp)
,
Modifier.padding(top = 16.dp)
etc). So I have 2 questions: 1. Would it be good idea to create some global constants for such commonly used stuff and reuse them. Basically following M3 tokens design aka:
Copy code
internal object TypeScaleTokens {
    val BodyLargeFont = TypefaceTokens.Plain
    val BodyLargeLineHeight = 24.0.sp
    val BodyLargeSize = 16.sp
    val BodyLargeTracking = 0.5.sp
    ...
}
2. And second though, would it be good performance vice? I mean, it computes something every time you call
16.dp
, I understand it is fast sure, but when you have it 100 hundred times at the same screen.
a
Many things in Compose are value classes, so it's not "computing" anything, it essentially compiles to
16f
In any case, to reduce possibilities of human error, it's better to put your common values contained somewhere. I wish M3 token objects were not marked internal; there are many times I wish to re-use existing definitions instead of tracking my own.
🙌 1
v
Interesting, I didn't know that. Then it doesn't make that much sense as I was overthinking it. Although I am seeing case to add common padding value to the AppTheme, so if we could replace all the tokens for large screen, for example (aka default horizontal padding from the edges for screen from 16.dp for small screens to 32.dp to bigger ones). But that is a little bit different topic really