Search code examples
androidandroid-studioandroid-jetpack-compose

Why is Jetpack Compose's modifier recommended to be the first optional parameter?


In Android Studio, if any composable has an optional parameter preceding the Modifier parameter, the IDE generates a warning:

Modifier parameter should be the first optional parameter

Does anyone know why this is considered to be good practice? I can't see how it makes any difference, even stylistically. What value is there in having it precede optional parameters?


Solution

  • In the API Guidelines for @Composable components in Jetpack Compose the section about Parameters order explains that the rationale in general is that more important parameters should come first.

    The most important parameters are the required parameters. The following parameters are all optional (except for a potential composable content lambda which always comes last). The most important optional parameter is the modifier, because it is expected to be available on all composables and is generally the main parameter for customizations.

    This obviously is just a convention. There is no performance or other syntactical benefit, it is more about the expectation that there somehwere is a modifier parameter, and having it at a defined (relative) position helps with the expectation.