First thoughts on Jetpack Compose

Image for post
Image for post

Design Patterns

In the initial chapters, the authors take you along the development process of a graphical editor based on a view system. While following the process I became fascinated by how relevant this old book (1995) still seemed and how much I could actually apply to my coding style. There was one pattern that I found especially fitting for creating a view system, the Composite pattern. And it got me thinking that the Android view system seemed to ignore this pattern, besides this book being around for so long already. The Android code design is heavily based on inheritance with very large base classes, which is for me today one of the main reasons the code became hard to maintain in the long run. Besides the fact of course that its really really old.

Flutter!

Model View Intent

The advantage of this pattern is that there is one single source of truth and there is no syncing effort between the layers. In my opinion, this is a very important aspect to MVI, but unfortunately the Android view system is not really made for this, as the views in Android have state baked into them.

Jetpack Compose

But when I initially saw that there was a state inside the composables I was wondering, why the state is not just passed down the hierarchy and controlled by the ViewModel. Luckily there some good official explanations available.

You can think of internal state as a private variable in a class. The source of truth belongs to whoever creates and controls that state

coming from the OOP world this was quiet plausible for me. A variable should be in the class, which is using it the most (in this case exclusively), also described in Martin Fowler's book as move field refactoring. A good example of a private state of a composable would be the scroll state of a scrollable element. On the other hand, if the state is actually needed by multiple composables you should lift the state up in a higher layer, this is what they refer in the documentation to as state hoisting. There are quite some other aspects to how state is handled in composables like ambient, but that's something still left to explore and to write down in another blog post fully dedicated to state handling.
Thanks for reading and to share some more thoughts on Jetpack Compose lets connect on Twitter.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store