Using : SonarQube™ - Enterprise Edition - Version 9.9 (build 65466) Language : Kotlin Rule: S107 ( Functions should not have too many parameters).
Expectation:
Methods annotated with @Composable (androidx.compose.runtime.Composable) should be ignored for this rule.
Composable tend to have a lot of parameters and it is by design. It is pretty trivial for composables to have more than 7 parameters, as all values (states, event callbacks, and the content of the child composables) are passed down from parent to child.
Example:
@Composable
fun HazeScaffold(
modifier: Modifier = Modifier,
topBar: @Composable () -> Unit = {},
bottomBar: @Composable () -> Unit = {},
snackbarHost: @Composable () -> Unit = {},
floatingActionButton: @Composable () -> Unit = {},
floatingActionButtonPosition: FabPosition = FabPosition.End,
containerColor: Color = MaterialTheme.colorScheme.background,
contentColor: Color = contentColorFor(containerColor),
contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
hazeState: HazeState = remember { HazeState() },
blurTopBar: Boolean = false,
blurBottomBar: Boolean = false,
content: @Composable (PaddingValues) -> Unit,
) {
// Method body...
}
This is an example sourced from Chris Banes’s tivi project.
I had the same thought at first, but after an investigation, I found it is considered bad practice.
More resources are available here: Android Developers - Thinking in Compose.
Just to give some more information
I don’t think it’s a really bad practice, when we watch the video of android developer they say that we must simplify the compose function to be understand (api)
But we have some cas in android like the sample of Jaya where we must given all the parmeters necesseray to custom the view ( style, color, etc…).
In consequence we can have many parameters in this function some times
For sample, I want to wrap a TextEdit for some behavior(UI) but keep the custom ui purpose by google, for an library component.
When you watch the composable provide by android you can watch many parameters available in.
Hi Irina, android compose components implemented as below, but sonar rules force us to write less parameters and less function content, can you please consider evaluate to arrange for composable ?
We acknowledge that the rule does not make much sense for methods with @Composable annotation. We want to change the rule behavior by either adding an exception for such methods or by having a separate threshold.
We will update this thread once the change is released.
We’re also affected by this issue and are very interested in seeing it resolved. Thanks for looking into it and acknowledging the limitation with the current rule.
Do you have any updates or an estimated timeline for when this change might be released?