Too many parameters in the Jetpack Compose function?

Yair Carreno
1 min readJul 8, 2022

--

You may have noticed that during designing views through Compose functions, it’s effortless to fall into the scenario of having a function with many parameters. Something similar to:

Two techniques can help us reduce excess parameters in the function clearly and cleanly. These techniques are as follows:

1. Using an object that groups as a pair the state and the event

This helper object can help pair a UI component’s state/event definition. In this case, I have named it StateVsEvent:

Using this helper object, we halve the number of parameters:

Why use Data class instead of Pair?
It is a documented recommendation in “Prefer data classes instead of tuples”.

2. Let’s use Components UI’s States, which group the state of related UI components

Another option is to use components that group UI elements through a consolidated state. I described this technique in a previous article, “Do you know the ways to represent states in Jetpack Compose?”.

I know, it seems very simple but effective definitions to organize our code a bit. Happy coding 🤓📱!

In my book Building Modern Apps for Android, I talk a little more about that topic.

--

--

Yair Carreno
Yair Carreno

Written by Yair Carreno

Software engineer with a technical blog about #iOS, #Android, #Angular. Author of “The Clean Way to Use Rx”: https://leanpub.com/the-clean-way-to-use-rx

No responses yet