Hi, i saw a code snippet who is written by a googl...
# compose-android
s
Hi, i saw a code snippet who is written by a googler like below:
Copy code
@Composable
fun PolygonComposable(polygon: RoundedPolygon, modifier: Modifier = Modifier) =
    PolygonComposableImpl(polygon, modifier)

@Composable
private fun MorphComposable(
    sizedMorph: Morph,
    progress: Float,
    modifier: Modifier = Modifier,
    isDebug: Boolean = false
) = MorphComposableImpl(sizedMorph, modifier, isDebug, progress)
Why did they seperate the composable to implementation and caller ? What is the benefit of this ?
z
Without knowing more about the code and context, I can’t see any reason to do this.
s
Ahh ok. Here are the implementations:
Copy code
@Composable
private fun MorphComposableImpl(
    sizedMorph: Morph,
    modifier: Modifier = Modifier,
    isDebug: Boolean = false,
    progress: Float
) {
    Box(
        modifier
            .fillMaxSize()
            .drawWithContent {
                drawContent()
                val scale = min(size.width, size.height)
                val path = sizedMorph.toComposePath(progress, scale = scale)
                if (isDebug) {
                    drawPath(path, Color.Green, style = Stroke(2f))
                    sizedMorph.forEachCubic(progress) { cubic ->
                        cubic.transform { x, y -> TransformResult(x * scale, y * scale) }
                        debugDraw(cubic)
                    }
                } else {
                    drawPath(path, Color.White)
                }
            })
}

@Composable
internal fun PolygonComposableImpl(
    polygon: RoundedPolygon,
    modifier: Modifier = Modifier,
    debug: Boolean = false
) {
    val sizedShapes = remember(polygon) { mutableMapOf<Size, List<Cubic>>() }
    Box(
        modifier
            .fillMaxSize()
            .drawWithContent {
                drawContent()
                val scale = min(size.width, size.height)
                val shape = sizedShapes.getOrPut(size) { polygon.cubics.scaled(scale) }
                if (debug) {
                    shape.forEach { cubic -> debugDraw(cubic) }
                } else {
                    drawPath(shape.toPath(), Color.White)
                }
            })
}
and the repo: https://github.com/chethaase/ShapesDemo
z
yea i have no idea why he split up those functions
👍 1
s
Ah ok. Appreciated for your answers. Have a nice day.
z
just to be clear, i would advise against doing this, as i would always advice against adding complexity for no reason 😅 i’m sure he had a reason at the time but maybe this was left in by accident after some changes 🤷🏻
👍 1
s
Yea agree with that but i also saw similar practice in compose source code
So when i see a googler apply this, then i wanted to learn the reason why
c
This is useful for testing mocks of these composables, but i can’t think of any other reason.
z
How would you mock these composables? You can’t change the function that
PolygonComposable
is calling without more bytecode manipulation than most mocking libraries do by default.
also, mocking bad