'JetPack Compose - Best practices for method length [closed]
I am trying to integrate detekt with JetPack Compose project and as a rule
LongMethod:
active: true
threshold: 60
and as compose recommend passing each parameter in separate line the method length exceeds the 100 lines of code, So I am wondering what is the best threshold to use for composable functions or if there is a way to exclude composables from this rule?
there are some detekt customization for compose here but nothing about this rule
Solution 1:[1]
I would suggest to encapsulate your params in a data class. This is something similar what's is done is some Material Compose components like: Scaffold (which uses ScaffoldState, or LazyColumn/LazyListState, etc.
This recommendation is described here.
Instead of using:
@Composable
fun VerticalScroller(
scrollPosition: Int,
scrollRange: Int,
onScrollPositionChange: (Int) -> Unit,
onScrollRangeChange: (Int) -> Unit
) {
you should use:
@Stable
interface VerticalScrollerState {
var scrollPosition: Int
var scrollRange: Int
}
@Composable
fun VerticalScroller(
verticalScrollerState: VerticalScrollerState
) {
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nglauber |
