https://kotlinlang.org logo
Title
p

Philip Dukhov

05/12/2023, 10:47 AM
I'm used to using Modifier as the last parameter in a composable call. I know that most people don't use it this way - including the Compose sources.
Row(
    modifier = modifier
        .background(backgroundColor, shape = shape)
        .padding(
            vertical = 4.dp,
            horizontal = 8.dp,
        ),
    verticalAlignment = Alignment.CenterVertically,
)
vs
Row(
    verticalAlignment = Alignment.CenterVertically,
    modifier = modifier
        .background(backgroundColor, shape = shape)
        .padding(
            vertical = 4.dp,
            horizontal = 8.dp,
        )
)
The main reason for me is that as the last parameter I don't need to use a comma. This way I can reorder modifier lines, copy/paste them without thinking about the comma, the diff in git look cleaner, and so does the code itself - when the last parameter line is indented compared to the beginning of other parameters, it catches my eye - but it's just a habit, for sure So those are my reasons for using it as the last parameter, if you use it as the first - what are your reasons? Are there any advantages?
o

Oleksandr Balan

05/12/2023, 11:36 AM
I also prefer Modifier as the last parameter on the call site, because as for me all other parameters are component specific and thus more important than the modifier itself.
s

Stylianos Gakis

05/12/2023, 5:40 PM
It’s so annoying that after enabling trailing commas everywhere, it’s a better experience except for this one case, and every time I have to reorder modifiers I clench my fist against the clouds being annoyed 😅 With that said, I also always use Modifier as the last parameter on the call site, exactly for the reasons that Oleksandr explains too. It’s a pretty nice habit to get into.