# Stories by Jossi Wolf on Medium

Stories by Jossi Wolf on Medium

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Leveraging Kotlin for Tests

DevFeed: [Leveraging Kotlin for Tests](<https://devfeed.tech/articles/leveraging-kotlin-for-tests-25968.md>)

Original publisher: [Read original article](<https://jossiwolf.medium.com/leveraging-kotlin-for-tests-311bfd8335ee?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2021-12-01T15:00:03Z

Content type: tutorial

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Behavior-driven development](<https://devfeed.tech/topics/bdd.md>), [Cucumber](<https://devfeed.tech/topics/cucumber.md>), [Development](<https://devfeed.tech/topics/development.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [bdd](<https://devfeed.tech/tags/bdd.md>), [given-when-then](<https://devfeed.tech/tags/given-when-then.md>), [junit](<https://devfeed.tech/tags/junit.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>)

### AI overview

This article explains how Kotlin language features and naming patterns can improve test readability. It covers the Given/When/Then pattern, meaningful test names, and using Kotlin backtick function names to make tests clearer.

### Source excerpt

Writing tests can be an important part of developing applications. Whether unit tests, integration tests or others, Kotlin's language features and clever naming patterns can help us increase the readability of tests. The patterns suggested here aren't new or innovative; rather they are more modern adaptions of well-established practices. I learned about these adaptions from my lovely colleague Volodymyr Galandzij who did fantastic work in setting up the testing framework in a codebase we worked on together. Given/When/Then You may have heard of the Given/When/Then pattern -- also known as the Cucumber/Gherkin syntax -- if you have written tests before, or you might even recognize it from Jira tickets. Originally introduced as a part of BDD (Behavior Driven Development), similar patterns like the Four-Phase Test (you'll see some similarities to JUnit here) have existed for a good amount of time. Martin Fowler gives a good small summary of all of this in his article about the Given/When/Then pattern: The essential idea is to break down writing a scenario (or test) into three sections:The given part describes the state of the world before you begin the behavior you're specifying in this scenario. You can think of it as the pre-conditions to the test.The when section is that behavior that you're specifying.Finally the then section describes the changes you expect due to the specified behavior. Regardless of the testing philosophy you follow, there's a good chance that the Given/When/Then pattern can be applied to your tests. But how does this look in practice? I heard that adding a featured image helps increase clicks. Does it work? Who knows! If it does, thank you, Sigmund on Unsplash!Test Names I argue that test names are one of the most important parts of writing good tests. The name of the test should help you understand what the test does. One quality of a good test is that it fails when the actual outcome is different from the expected outcome. When a test fails, it

## Introducing Navigation-Material 

DevFeed: [Introducing Navigation-Material ](<https://devfeed.tech/articles/introducing-navigation-material-25967.md>)

Original publisher: [Read original article](<https://jossiwolf.medium.com/introducing-navigation-material-%EF%B8%8F-a19ed5cc33fd?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2021-08-04T19:01:30Z

Content type: release

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Compose](<https://devfeed.tech/topics/compose.md>), [Library](<https://devfeed.tech/topics/library.md>), [releases](<https://devfeed.tech/topics/releases.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [bottomsheet](<https://devfeed.tech/tags/bottomsheet.md>), [compose](<https://devfeed.tech/tags/compose.md>), [experimental](<https://devfeed.tech/tags/experimental.md>), [extension](<https://devfeed.tech/tags/extension.md>), [fragments](<https://devfeed.tech/tags/fragments.md>), [getting-started](<https://devfeed.tech/tags/getting-started.md>), [jetpack](<https://devfeed.tech/tags/jetpack.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [library](<https://devfeed.tech/tags/library.md>), [material-design](<https://devfeed.tech/tags/material-design.md>), [navigation-component](<https://devfeed.tech/tags/navigation-component.md>), [new-feature](<https://devfeed.tech/tags/new-feature.md>), [releases](<https://devfeed.tech/tags/releases.md>)

### AI overview

This article introduces Navigation Material, an Accompanist library that adds modal bottom sheet destinations to Navigation Compose. It explains the library's experimental status and provides setup steps using BottomSheetNavigator, NavController, ModalBottomSheetLayout, and the bottomSheet function.

### Source excerpt

Illustration by Ana Teresa Silva from the lovely We Are Systematic. Thank youuuuuu❤✨ Navigation-Compose (well, kinda) has an exciting new feature: Support for bottom sheet destinations! Over the past (almost) 6 months, I worked on a library in collaboration with the Navigation team that offers support for Modal Bottom Sheets in Navigation Compose. Navigation with Fragments has long supported DialogFragments, and with today's release of the Navigation Material library, we are providing the same feature for composable bottom sheets with Navigation Compose. Since modal bottom sheets are still an experimental Compose-Material API, the library will be released as part of Accompanist, a collection of extension libraries for Jetpack Compose. All AndroidX libraries, Navigation and Compose included, follow strict semantic versioning as explained on the AndroidX releases page. This means that any API that isn't experimental is set in stone once a library goes to its Release Candidate (RC) phase. It would take a major version bump (i.e., a '2.0') to make breaking API changes to these stable APIs. - Ian Lake about the versioning system of AndroidX libraries Because of the versioning requirements for AndroidX libraries, Navigation Material will be offered as part of Accompanist, just like navigation-animation. Think of it as an incubator until the bottom sheet APIs are fully grown up! Enough of the talking -- let's look at how you can use it! Getting started! Navigation Material is fresh out of the oven, built off today's release of Navigation 2.4.0-alpha06! There are a few easy steps to get started: Create a BottomSheetNavigator using the rememberBottomSheetNavigator() function Add the BottomSheetNavigator to your NavController Wrap your existing NavHost in the ModalBottomSheetLayout composable provided by Navigation Material. If you don't have a NavHost yet, we'll create one! Now you're ready to define bottom sheet destinations using the new bottomSheet function! Let's go thro

## Navigating in Jetpack Compose

DevFeed: [Navigating in Jetpack Compose](<https://devfeed.tech/articles/navigating-in-jetpack-compose-25970.md>)

Original publisher: [Read original article](<https://medium.com/google-developer-experts/navigating-in-jetpack-compose-78c78d365c6a?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2021-06-07T07:52:33Z

Content type: tutorial

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Mobile](<https://devfeed.tech/topics/mobile.md>), [Compose](<https://devfeed.tech/topics/compose.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [compose](<https://devfeed.tech/tags/compose.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [mobile](<https://devfeed.tech/tags/mobile.md>), [navigation](<https://devfeed.tech/tags/navigation.md>)

### AI overview

A tutorial on using the Navigation component with Jetpack Compose for mobile applications. It explains creating a NavController and NavHost, declaring destinations and nested graphs with the Kotlin DSL, and handling navigation routes without XML navigation graphs.

### Source excerpt

If you are working on a mobile app, chances are you need some form of navigation. Getting navigation right isn't easy with many challenges at hand: back stack handling, lifecycles, state saving and restoration and deep linking are just some of them. In this post, we'll explore the Navigation component's support for Jetpack Compose and take a look under the hood! Could I have chosen a more cliche photo? Probably not! Photo by Mick Haupt on Unsplash. Thanks, Mick!Getting started! Before getting started, we'll add a dependency on navigation-compose, the Navigation component's artifact for Compose support. https://medium.com/media/cfb5c77e0f949f64f3c820eb04e279f1/href Let's jump into the code! https://medium.com/media/abe2eb5ce3e3e78753e1c316c9684b27/href First, we create and memoize a NavController using the rememberNavController method. rememberNavController returns a NavHostController which is a subclass of NavController that offers some additional APIs that a NavHost can use. When referring to the NavController and using it later on, we will use it as NavController as we don't need to know about these additional APIs ourselves; it's just important for the NavHost. We pass this into our NavHost composable. The NavHost composable is responsible for hosting the content of the NavDestination associated with the NavBackStackEntry (we will look at the details in a bit!). The lambda we pass to the NavHost is the builder for our navigation graph. In here, we have access to a NavGraphBuilder and can construct and declare our navigation graph. This is where we declare our destinations and nested graphs. If you are coming from the "old" navigation library, this might feel a bit weird at first! There is no XML anymore, not even for the navigation graphs. Even though the Kotlin DSL has been available for a long time, it had been overshadowed by XML so far. This also means that we won't have a visual representation of the graph for the foreseeable future. The rendered view of the

## Running a Hack Day

DevFeed: [Running a Hack Day](<https://devfeed.tech/articles/running-a-hack-day-25972.md>)

Original publisher: [Read original article](<https://medium.com/snapp-mobile/running-a-hack-day-9b682cb970d2?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2020-12-01T20:46:31Z

Content type: opinion

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Development](<https://devfeed.tech/topics/development.md>), [Learning](<https://devfeed.tech/topics/learning.md>), [Compose](<https://devfeed.tech/topics/compose.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [hack-days](<https://devfeed.tech/tags/hack-days.md>), [hackday](<https://devfeed.tech/tags/hackday.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [learning](<https://devfeed.tech/tags/learning.md>), [programming](<https://devfeed.tech/tags/programming.md>), [stories](<https://devfeed.tech/tags/stories.md>), [team-building](<https://devfeed.tech/tags/team-building.md>), [teamwork](<https://devfeed.tech/tags/teamwork.md>)

### AI overview

The article explains what a hack day is and discusses how it can help developers break from routine work, learn, collaborate across teams, and build connections. It also compares hack days with 20% time and notes that the two approaches can complement each other.

### Source excerpt

Recently, some clients asked us about our opinion on a newly released framework. To get a better idea of it, we ran an internal hack day -- here's how! Stay tuned for the second part about our experiences with running Jetpack Compose hack day! What's a Hack Day? Basically, a hack day is a one-day event where you can work on whatever. This is awesome for exploring a new technology or just doing something different once in a while! There are no rules except for that what you're doing should probably be kinda related to your work, but it doesn't have to be! Why you should run a hack day There are lots of reasons to love hack days -- I'll focus on a few. The benefits will always be a different depending on your company, team and the tech you are working with. It helps people break out of day-to-day work life A hack day can enable working with different people and build connections If you do it right, people will be able to relax a bit A fun breakout First off, hack days are a great thing to break out of day-to-day dev life. Project fatigue gets everyone after a while. Maybe the work is not mundane or boring, but working on exactly the same thing every day can get tedious. The longer you work on a project, the harder the challenges become and the longer the cycles of success/no success become. On a hack day, you will be working on something entirely different, without any expectations. This makes it a lot easier to learn, takes some pressure off and can be very satisfying. Teamwork!Teamwork Since a hack day is a defined space and time for folks to work on something, this makes working together a lot easier -- you're there anyway! Working on something alone can be nice, and maybe that's exactly what somebody is looking for. For others, learning together is a lot more fun and more memorable. Interacting with folks from different teams In our case, we had people working together who normally work on different projects. This helps create connections and makes the team grow toge

## Refactoring from LiveData to Coroutines & Flow

DevFeed: [Refactoring from LiveData to Coroutines & Flow](<https://devfeed.tech/articles/refactoring-from-livedata-to-coroutines-flow-25969.md>)

Original publisher: [Read original article](<https://jossiwolf.medium.com/refactoring-from-livedata-to-coroutines-flow-e73b6c59f5ad?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2020-08-24T16:29:47Z

Content type: tutorial

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Refactoring](<https://devfeed.tech/topics/refactoring.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Android](<https://devfeed.tech/topics/android.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [coroutines-flow](<https://devfeed.tech/tags/coroutines-flow.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [livedata](<https://devfeed.tech/tags/livedata.md>), [main-thread](<https://devfeed.tech/tags/main-thread.md>), [migration](<https://devfeed.tech/tags/migration.md>), [network](<https://devfeed.tech/tags/network.md>), [refactoring](<https://devfeed.tech/tags/refactoring.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>), [threading](<https://devfeed.tech/tags/threading.md>), [ui](<https://devfeed.tech/tags/ui.md>)

### AI overview

This tutorial describes migrating Android repositories from LiveData to Kotlin Coroutines and Flow. It distinguishes one-shot operations from data streams and introduces migration helpers to preserve compatibility during a gradual refactor.

### Source excerpt

If you haven't, I recommend reading my other post about LiveData in Repositories. We recently joined a new project with heavy LiveData usage, everywhere. The search for main thread blockages led us down a rabbit hole of removing a significant portion of our LiveData usages. Here's how we migrated our Repositories from LiveData. We were faced with a tough challenge: We had to migrate as quickly and smoothly as possible -- there was no time for huge interruptions. Having all our repositories and lots of helper classes use LiveData, this left us with two options: Make sure we get threading right in all places that use LiveData Replace LiveData in our Repositories Making sure we're on the right thread everywhere and continuing to use LiveData would be the solution requiring fewer changes, but we would still be using LiveData for a use case it isn't really designed for. It can not be said often enough: LiveData has nothing to do in layers not related to UI! Keep it out of Repositories, DataSources! There use Flow or RxJava and only put LiveData in your ViewModels!It was very unfortunate that this was shown by Google as best practice. 😪 https://t.co/zOnbmyJYDP Agreed, 💯! We're actually working on revamping this guide which hasn't been updated with the latest recommendations for a looong time 🙈 Our code was also quite convoluted and hard to fix, so we decided to look for another option to handle asynchronicity. In an ideal world, refactoring it would have been easier, and we probably would have stuck with LiveData had our code been less convoluted. Our main requirement was for our replacement to fit with our existing mental model -- ViewModels being able to observe data from repositories where needed. Looking at our code, we were able to categorise our LiveData use cases into two categories: One-shot LiveData, for example, network calls Streams of data (e.g. for retrieving cached data before emitting fresh data) Our Replacement We looked around for a bit and decided to go wi

## Sample Data in Compose Previews

DevFeed: [Sample Data in Compose Previews](<https://devfeed.tech/articles/sample-data-in-compose-previews-25973.md>)

Original publisher: [Read original article](<https://medium.com/snapp-mobile/sample-data-in-compose-previews-bec32b62370f?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2020-07-05T17:45:51Z

Content type: tutorial

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Compose](<https://devfeed.tech/topics/compose.md>), [Android Studio](<https://devfeed.tech/topics/android-studio.md>), [Android](<https://devfeed.tech/topics/android.md>), [Design system](<https://devfeed.tech/topics/design-system.md>), [ui](<https://devfeed.tech/topics/ui.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-studio](<https://devfeed.tech/tags/android-studio.md>), [components](<https://devfeed.tech/tags/components.md>), [compose](<https://devfeed.tech/tags/compose.md>), [data](<https://devfeed.tech/tags/data.md>), [design-systems](<https://devfeed.tech/tags/design-systems.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [parameter](<https://devfeed.tech/tags/parameter.md>), [stories](<https://devfeed.tech/tags/stories.md>), [ui](<https://devfeed.tech/tags/ui.md>), [ui-components](<https://devfeed.tech/tags/ui-components.md>)

### AI overview

This tutorial explains how to provide sample data for Jetpack Compose previews using the @PreviewParameter annotation and PreviewParameterProvider. It addresses previews for parameterized Composables and lists, while noting that the annotation can be used on only one function parameter.

### Source excerpt

This post was written for Compose 0.1.0. Jetpack Compose is Google's new Android UI Toolkit, currently in development. If you already want to have a look, you can play around with it and learn some exciting new things! One area where Compose shines is previews. By annotating Composables with @Preview , you can preview the Composables inside Android Studio. This is very useful when working with e.g. Design Systems, or you just want to explore different variants of your UI components. But often, your Composables will take parameters, like our Project Composable. https://medium.com/media/9384b6a63c4a8ba376843a4ee3b90717/href In these cases, you can't use the Preview annotation on your Composable -- after all, Compose doesn't know where to obtain those parameters. One solution to this is to create "wrapper" Composables without any parameters. https://medium.com/media/d1f3459d8cd4af796160f696d97eb84d/href This has one big downside: You will end up creating lots of preview Composables, cluttering your code. In other cases, you might have a list and want to provide some sample data. With XML, supplying data for the preview was easily doable with the tools namespace. https://medium.com/media/16d547ae6a634bce91bb44023d4919e3/hrefPreviewParameter to the Rescue Luckily, Jetpack Compose has the @PreviewParameter annotation. It can be applied to any parameter of a @Preview. It needs PreviewParameterProvider , a simple interface which we can implement. https://medium.com/media/e1c92b209a2fdff94bb2a44662eee97f/href Finally, we can use the @Preview annotation with our @PreviewParameter -annotated arguments. https://medium.com/media/25d1c0eb02a8bfd7591af59dabce8885/href One caveat is that @PreviewParameter is only allowed on one parameter of a function, which is why we assign a default value to our second parameter. With this, previews become a lot more powerful in Compose. Let me know if this is useful in your day-to-day work and comment with any thoughts on this! Sample Data in Com

## Replacing Custom Views with View Binding

DevFeed: [Replacing Custom Views with View Binding](<https://devfeed.tech/articles/replacing-custom-views-with-view-binding-25976.md>)

Original publisher: [Read original article](<https://proandroiddev.com/replacing-custom-views-with-view-binding-7836c34185fb?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2020-04-19T12:31:58Z

Content type: tutorial

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Code](<https://devfeed.tech/topics/code.md>), [XML](<https://devfeed.tech/topics/xml.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [code](<https://devfeed.tech/tags/code.md>), [custom-view-android](<https://devfeed.tech/tags/custom-view-android.md>), [dockerignore-usage](<https://devfeed.tech/tags/dockerignore-usage.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [view-binding](<https://devfeed.tech/tags/view-binding.md>), [xml](<https://devfeed.tech/tags/xml.md>)

### AI overview

This tutorial explains how a codebase replaced reusable custom views with Android View Binding when custom rendering was unnecessary. It covers generated binding objects, lifecycle-scoped instantiation, reusable binding extension functions, and passing data models to reusable components.

### Source excerpt

How we used View Binding to replace Custom Views In our codebase, we use a lot of custom views for reusability -- but most of the time, a custom view was overkill. Here's how we used View Binding to replace some of our custom views. View Binding 101 If you are not familiar with View Binding yet, here is a great post by Sean McQuillan about it. In short: View Binding is a better alternative over Kotlin Synthetics, findViewById and good old Butterknife. It allows you to access the views in a layout in a type-safe and null-safe way. For each XML layout, View Binding generates a binding object. For a layout fragment_login.xml, you'd get a binding object called FragmentLoginBinding. Instantiating the binding objects requires a bit of boilerplate to make sure it is scoped to the components' lifecycle. We use a delegate class as suggested in this post to make the code cleaner and easier to read. About Our Custom Views We used many custom views for our reusable components. Like this one: https://medium.com/media/0d681c4bb6b7d1f682a84aa2e58801ba/href Custom views are awesome if they are used right, but they bring quite a bit of boilerplate and are harder to maintain then XML layouts. For cases where we don't do any custom rendering, View Binding made more sense to use as we could cut down on the boilerplate. Using View Binding for Reusable Components As View Binding generates a binding object for each layout, the first step for migrating was to create a layout: https://medium.com/media/b9c9bf94e38e1b30923d741f6e85d7c4/hrefPreview of the layout Now for our card_user_data.xml, View Binding generates a CardUserDataBinding.java which we can use. Binding the data First, we need to include the card layout in our fragment's layout. https://medium.com/media/1af56e360407fe6f1eaef366f18e6401/href View Binding will automatically cast the type of the <include> to CardUserDataBinding, so we can use it in a type-safe way. That also means we can reuse those bindings across layouts. Beware t

## Under the hood of Kotlin Class Delegation

DevFeed: [Under the hood of Kotlin Class Delegation](<https://devfeed.tech/articles/under-the-hood-of-kotlin-class-delegation-25974.md>)

Original publisher: [Read original article](<https://medium.com/snapp-mobile/under-the-hood-of-kotlin-class-delegation-24d53f9aa924?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2019-04-09T13:20:12Z

Content type: tutorial

Language: en

Sources: [Stories by Jossi Wolf on Medium](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Code](<https://devfeed.tech/topics/code.md>), [Inheritance](<https://devfeed.tech/topics/inheritance.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [software-development](<https://devfeed.tech/topics/software-development.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [api](<https://devfeed.tech/tags/api.md>), [bytecode](<https://devfeed.tech/tags/bytecode.md>), [delegation](<https://devfeed.tech/tags/delegation.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [interface](<https://devfeed.tech/tags/interface.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [proxy](<https://devfeed.tech/tags/proxy.md>), [stories](<https://devfeed.tech/tags/stories.md>)

### AI overview

This Kotlin tutorial explains how class delegation can expose an interface through a class without cluttering it with proxy methods. It also describes the generated delegation functions and their effect on compiled code size.

### Source excerpt

I was told to add an image to the post, so here's one Photo by Jf Brou from Unsplash. Bonus points since Samoyeds are some of the cutest dogs out there! A piece of rather popular advice heard in software development is "favour composition over inheritance". I won't go into the details of why as there is an excellent chapter in "Effective Java" about it, as well as this nice article by Eric Lin. This article explores how class delegation can be useful and how it works under the hood. In our case, we had a custom view which should expose an interface looking like this: https://medium.com/media/2efb0d0125ffec7f388ee2df190bcf6a/href Now it was time to create the CanvasCapabilities implementation. https://medium.com/media/3d8b72ac99b28b99f39c95c882a69dce/href Of course, we don't want to start putting logic into our view just for the sake of exposing the interface through it, which meant that the view now had to act as a "proxy" (implementing the interface and calling the ArtboardCanvas ): https://medium.com/media/f3d51d082e7f23d725ef7efafb3fe131/href Not a nice solution, but at least we extracted the logic. The view was still cluttered though :( Class Delegation to the rescue Using the by keyword in Kotlin, properties and even classes can be delegated. You might know it from using lazy : https://medium.com/media/cbea2e6d1363f31e28552d9a8c87823e/href You can also delegate inheritance, which is what we are after. The usage looks like this: https://medium.com/media/5482d423d1b2a0fcff239f25c4ab31e9/href This delegates the methods from CanvasCapabilities to ArtboardCanvas . No more cluttering with "proxy" calls! Under The Hood Under the hood, Kotlin actually doesn't do any "black magic": It just generates a function which invokes the delegate. The bytecode, decompiled to Java looks like this: https://medium.com/media/34cf5d85a4bcf122bc98d74425e9a5dd/href If you have a lot of methods, that code will still be generated and adding to your compiled code's size, but at least it do