Is there anybody who can review my code? it's abou...
# codereview
c
Is there anybody who can review my code? it's about modifier and I want to simplify it when it's complicated. https://codereview.stackexchange.com/questions/284140/how-can-i-simplify-this-modifier
m
I don’t have context to give you a meaningful answer, but if and only if the only thing you want is an alternative way to write that bit of code, you likely can do a conditional modifier extension:
Copy code
fun Modifier.thenIf(
  predicate: Boolean,
  block: () -> Modifier,
) = then(if (predicate) block() else this)
And now you can rewrite that code as such:
Copy code
Modifier
    .thenIf(label != null) {
        Modifier
            .padding(8.dp)
            .semantics(mergeDescendants = true) {
                // no-op
            }
    }
    .background(colors.backgroundColor(enabled).value, shape)
    .defaultMinSize(
        minWidth = 120.dp,
        minHeight = 24.dp
    )
That isn’t much better than the
apply
+
if
you did - if you are willing to give more details, maybe we can find an alternative solution.
c
@Marcello Galhardo I updated my question.
j
Be aware that conditions like that make it really hard for compose to optimize modifier recompositions. Tbf most of the time this shouldn't matter.
c
Well,.. I don't get it.. do you mean, should I take the condition out and make them remember?
j