What is "good practice" for defining dimensions in...
# compose
t
What is "good practice" for defining dimensions in Compose? i.e. as plain "magic numbers" like `40.dp`/`12.sp` etc inside Composables, or extracting as constants in separate Kt file, or keeping them in XML?
d
Depends entirely of what you are building and the needs you have. Reusable widget? Good defaults, customizable for the part that makes sense, computed or fixed for the other parts. Do you have some measures in your app that is semantically similar and you want to be able to change it in a single place in the future? Constant or theme dimensions. Etc.
👍 1
t
Makes sense, sounds like how you'd deal with any instance of a "magic number"; i.e. where should it live based on requirements/usage.
m
Honestly, XML files are basically the same as magic constants, with the only difference being that they automatically can select different values for different qualifiers. For most dimensions though, i just generally create an object and put the constants in there:
Copy code
object Dimensions {
    val DefaultPaddingVertical = 16.dp
}
also yes 1
for ones that change based on qualifiers, you can easily adapt:
Copy code
val DefaultPaddingVertical: Dp
   @Composable get() = 
     when { ... }
t
For most dimensions though, i just generally create an object and put the constants in there:
yeah leaning towards doing more of this for every “macro” component
for ones that change based on qualifiers, you can easily adapt:
ahh, this is interesting; hadn’t thought about doing it this way