<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lorenzo Suarez on Medium]]></title>
        <description><![CDATA[Stories by Lorenzo Suarez on Medium]]></description>
        <link>https://medium.com/@lorenzosuarez1994?source=rss-72e1dd0e8309------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BbSje8MYNQMu6_RQ5DUpZA.jpeg</url>
            <title>Stories by Lorenzo Suarez on Medium</title>
            <link>https://medium.com/@lorenzosuarez1994?source=rss-72e1dd0e8309------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 06:49:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lorenzosuarez1994/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Sleek Code, Easy Life: Kotlin’s Delegation for the Modern Developer]]></title>
            <link>https://medium.com/@lorenzosuarez1994/sleek-code-easy-life-kotlins-delegation-for-the-modern-developer-8b16776190bb?source=rss-72e1dd0e8309------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b16776190bb</guid>
            <category><![CDATA[good-practices]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[delegator]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Lorenzo Suarez]]></dc:creator>
            <pubDate>Fri, 08 Dec 2023 04:32:55 GMT</pubDate>
            <atom:updated>2023-12-08T04:48:46.594Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*u3Hexg0YfvnWJL2kuLWnGw.png" /></figure><p><em>Unlock the elegance of Kotlin&#39;s delegation in Android development with practical, code-enhancing examples.</em></p><h3>Introduction:</h3><p>In the ever-evolving world of software development, writing clean and efficient code is not just a practice; it’s an art. Kotlin, a modern programming language that’s taking the Android world by storm, brings a plethora of features designed to simplify a developer’s life. One such feature, often underappreciated but incredibly powerful, is Kotlin’s delegation. Let’s dive into how Kotlin’s delegation can transform your code from good to great.</p><h3>What is Delegation in Kotlin?</h3><p>Delegation is a design pattern where an object hands over its responsibilities to a helper object. Kotlin, with its idiomatic design, brings this concept to the forefront, allowing developers to write more concise, readable, and maintainable code. This approach is particularly useful in Android development, where managing the state and behavior of UI components can get unwieldy.</p><blockquote><strong>Example 1: Encapsulating Shared Resource Access</strong></blockquote><blockquote><strong><em>Without Delegation</em></strong><em>:</em> Accessing shared resources like SharedPreferences in Android can often lead to repetitive and cumbersome code:</blockquote><pre>class UserRepository(val context: Context) {<br>    fun getUser(): User {<br>        val sharedPreferences = context.getSharedPreferences(&quot;UserData&quot;, Context.MODE_PRIVATE)<br>        val userName = sharedPreferences.getString(&quot;name&quot;, &quot;defaultName&quot;) ?: &quot;defaultName&quot;<br>        // Other data retrieval...<br>        return User(userName)<br>    }<br><br>    fun saveUser(user: User) {<br>        val sharedPreferences = context.getSharedPreferences(&quot;UserData&quot;, Context.MODE_PRIVATE)<br>        sharedPreferences.edit().putString(&quot;name&quot;, user.name).apply()<br>        // Other data persistence...<br>    }<br>}</pre><blockquote><strong><em>With Delegation</em></strong><em>:</em> By using Kotlin’s delegation, we can significantly clean up the code:</blockquote><pre>class SharedPreferenceDelegation&lt;T&gt;(val context: Context, val key: String, val defaultValue: T) {<br>    private val sharedPreferences by lazy {<br>        context.getSharedPreferences(&quot;UserData&quot;, Context.MODE_PRIVATE)<br>    }<br><br>    operator fun getValue(thisRef: Any?, property: KProperty&lt;*&gt;): T {<br>        return sharedPreferences.all[key] as? T ?: defaultValue<br>    }<br><br>    operator fun setValue(thisRef: Any?, property: KProperty&lt;*&gt;, value: T) {<br>        with(sharedPreferences.edit()) {<br>            when (value) {<br>                is String -&gt; putString(key, value)<br>                // Handle other types...<br>            }.apply()<br>        }<br>    }<br>}<br><br>class UserRepository(val context: Context) {<br>    var userName by SharedPreferenceDelegation(context, &quot;name&quot;, &quot;defaultName&quot;)<br>    // Other data...<br>}</pre><blockquote><strong><em>Benefits</em></strong><em>:</em> This approach reduces redundancy and centralizes the logic for accessing shared preferences, making it easier to manage and less error-prone.</blockquote><blockquote>Example 2: View State Management with Delegation</blockquote><blockquote><strong><em>Without Delegation</em></strong><em>:</em> Managing view states in ViewModel can quickly become messy:</blockquote><pre>class MyViewModel : ViewModel() {<br>    private val _isLoading = MutableLiveData(false)<br>    val isLoading: LiveData&lt;Boolean&gt; get() = _isLoading<br><br>    private val _user = MutableLiveData&lt;User&gt;()<br>    val user: LiveData&lt;User&gt; get() = _user<br><br>    // Methods updating the states...<br>}</pre><blockquote><strong><em>With Delegation</em></strong><em>:</em> Kotlin’s delegation can streamline this pattern:</blockquote><pre>class LiveDataDelegate&lt;T&gt;(initialValue: T) {<br>    private val liveData = MutableLiveData(initialValue)<br>    operator fun getValue(thisRef: Any?, property: KProperty&lt;*&gt;): LiveData&lt;T&gt; {<br>        return liveData<br>    }<br><br>    operator fun setValue(thisRef: Any?, property: KProperty&lt;*&gt;, value: T) {<br>        liveData.value = value<br>    }<br>}<br><br>class MyViewModel : ViewModel() {<br>    var isLoading by LiveDataDelegate(false)<br>    var user by LiveDataDelegate&lt;User?&gt;(null)<br>    // Methods updating the states...<br>}</pre><blockquote><strong><em>Benefits</em></strong><em>:</em> Delegation here simplifies LiveData and MutableLiveData declarations, enhancing code readability and reducing boilerplate.</blockquote><h3>Conclusion:</h3><p>Kotlin’s delegation offers an elegant solution to common coding challenges in Android development. It not only streamlines the code but also makes it more intuitive and less prone to errors. Whether it’s managing shared preferences or handling complex UI states, Kotlin’s delegation pattern is a tool every modern developer should have in their arsenal.</p><p><em>Keep Coding, Keep Simplifying!</em></p><ul><li><em>Lorenzo Suarez</em></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b16776190bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hoist with Care: Mastering State in Jetpack Compose]]></title>
            <link>https://medium.com/@lorenzosuarez1994/hoist-with-care-mastering-state-in-jetpack-compose-5fede9ca7b47?source=rss-72e1dd0e8309------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fede9ca7b47</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[state-management]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[best-practices]]></category>
            <dc:creator><![CDATA[Lorenzo Suarez]]></dc:creator>
            <pubDate>Mon, 06 Nov 2023 04:03:46 GMT</pubDate>
            <atom:updated>2023-11-06T04:05:33.206Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YMsIWQdEyxisBMATOLcv-w.png" /></figure><p><em>Lift your state management skills to towering heights with a pinch of Compose magic!</em></p><h4>Introduction</h4><p>In the realm of Jetpack Compose, state management is akin to a magic act — it can dazzle and amaze, or it can lead to an unexpected vanishing act of your data. Let’s pull back the curtain and reveal the trick to masterful state manipulation: State Hoisting.</p><h4>What’s State Hoisting?</h4><p>Hoisting state is like hosting a party and deciding not to keep it in your tiny apartment but moving it to a spacious venue instead. In Compose, it means taking the state out of your composable and passing it down from a parent or a higher level of your app’s hierarchy.</p><h4>Why Hoist?</h4><p>Hoisting state can transform a rigid, one-trick pony composable into a versatile show horse that can dance to any tune in the app’s grand performance.</p><p>The Performance Begins: A Non-Hoisted Act</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V8gwzxpCpGRFA-3uGRcZHQ.png" /></figure><p>Here we have a composable that’s a lone wolf, managing its own state. But what if other composables want in on the text action?</p><p>The Encore: A Hoisted Spectacular</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JAfn6moxA8h0jQn9kbIi2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CLwheg7qdKcsbFcBjx4QRQ.png" /></figure><h4>Why This Deserves a Standing Ovation</h4><ol><li>Encore-Ready: MyVersatileTextField is now ready for any part of your app to call it onstage.</li><li>No Solo Acts: The state isn’t locked away in a composable; it’s in the hands of MyApp, ready to be shared.</li><li>Applause for Testability: Testing is now a breeze, as you can orchestrate the state from outside the composable.</li></ol><h4>The Final Bow</h4><p>Remember, in Jetpack Compose, hoisting state isn’t just about showing off a fancy programming move. It’s about setting the stage for your UI to perform flawlessly, with every composable in perfect sync. So hoist wisely, and watch your app’s performance soar to a standing ovation!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fede9ca7b47" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>