- Developer's Commute
- Posts
- Do you know how to make composable Stateless? Why is it important? - Developer's Commute newsletter
Do you know how to make composable Stateless? Why is it important? - Developer's Commute newsletter
Today we will explore how to make composable functions stateless. Also, we have some interesting tweets, articles, memes
Good Morning, It’s your captain speaking. Welcome aboard the Developer’s Commute. Today we will explore how to make composable functions stateless. Also, we have some interesting tweets, articles, memes and in the end, Coding Motivation to keep you moving We hope that you enjoy this ride with us and you have a pleasant journey ahead.
Code Philosophy 🌜
How to make composable Stateless? Why is it important?
One important aspect of Android development is making the screens reusable, maintainable and testable.
While we talk a lot about app architecture we do not talk much about cleaner UI. So how do we make the UI cleaner?
The answer is simple, by making them stateless. Let's understand the basics of Stateful and Stateless.
Stateful means that the UI holds the state whereas stateless does not hold any state.
What do you mean by State and how do we hold it?
Let’s make a simple outlined text field in Jetpack compose
In this code, we can observe that the value of the field won’t change while changing the name field until an important keyword is introduced in the code.
“remember” is the keyword which helps us to save the state in the UI. The values survive the recomposition with the help of the remember keyword. It also updates the value as the recomposition occurs. But, it introduces a problem of UI holding the state values.
Why is it a problem for UI to hold state values?
If UI holds a state value then they become less reusable and testable. Think of every composable function you make like a Jetpack Library function. The changes should not occur inside of composable instead should be introduced by the developer.
Let’s take an example:
In this example, we can see the State of the name inside this function. It makes it less reusable and testable. Suppose we are taking first name and last name. We can use Hello Content in both of these cases but we would have to write the function two times.
But if we were to make it stateless like this. Then we only need to write the name variable twice and use the same function.
This is was one benefit of making composables stateless.
There are many benefits to using Stateless Composables:
→ Single Source of Truth
→ Encapsulated
→ Shareable
→ Interceptable
→ Decoupled
We will further explore the remember keyword and the benefits of Stateless composables in an upcoming newsletter. Thank you for reading.
This is for today I hope enjoyed this newsletter. If you enjoyed it please let me know and if I have written something wrong please forgive me.
Thank you for reading
Regards,
Developer Commute’s Captain
Free Ebook 🌠
Developer Meme 🎄
Tweets That You Might Love 🐿️
Tweets That You Will Find Interesting
Here is an interesting tweet for you →
How do companies ship code to production?
The diagram below illustrates the typical workflow.
Step 1: The process starts with a product owner creating user stories based on requirements.
Step 2: The dev team picks up the user stories from the backlog and puts them into a… twitter.com/i/web/status/1…
— Alex Xu (@alexxubyte)
4:38 PM • Dec 17, 2023
Then you might find this tweet interesting→
Android Developer aye?
Do you want to know how to do API authentication with Ktor?
Here is everything:
(1/12)
— Hitesh Kohli | App Developer (@Hitesh__kohli)
3:09 PM • Dec 20, 2023
Here is something interesting for you→
Do you want to build great apps?
Yes
Have an app idea but don't know how to make it?
Yes
Thinking of app ideas, marketing, developing and realising it sounds daunting.
Here are simple mental models for building great apps:
(1/16)
— Hitesh Kohli | App Developer (@Hitesh__kohli)
4:13 PM • Dec 19, 2023
ArticlesThat You Might Love 🐻
Articles That You Will Find Interesting
Here is an interesting article that you might want to check out →
Here is an interesting article for you →