Encapsulating code helps to maintain a healthy code base. Kotlin extension functions and properties can aid you in doing so. Learn how to create them for a Kotlin Data Class in this video.
- We're here inside our solar system sample app. And when you click on one of the planets, it takes you to this detailed view, where you get a description and other information about the planet, which includes its composition. Let's see how this information is being provided right now, and how we can take advantage of an extension function in order to simplify the code. So, we'll come over to Android Studio, and we're here inside the PlanetDetailFragment. This is the class that's responsible for displaying that screen.
If we scroll down to the onResume method, we see here on line 62 some very simple logic for determining a planet's composition. If the planet is an inner planet, then it's terrestrial. Otherwise, it's a gas giant. Notice how this depends completely on the inner property of the planet class. So, it makes sense to move this logic there, and we can do that by means of an extension function. So, let's highlight this information here on line 62, and copy it, and then we'll move over to the planet class.
Now, right above where we have our planet definition, we can add our extension function here on line five. So, just like any other function, we're going to start with the fun keyword. It's going to be an extension on the planet class, and we're going to call it composition. We want it to return that string value. So, let's set this equal to the code that we copied from our fragment. Now, we have a couple of errors that we need to fix first. Starting here, since inner is a property of the planet class and the planet is the receiver in this extension function, we don't need this reference to it.
We can just use inner directly. And then, the next two issues are related to the fact that terrestrial and gas giant are private in the previous file. So, let's use our alt + enter quick fix to make those public. And now, our extension function is good to go. So, we can move back over to our fragment and update this code. Instead of having all of these if else checks here, we can just change it to it.composition, our new extension function.
Now, how is it that our fragment class knows about this new function that we added to the planet? If you scroll all the way up to the top and take a look at our imports here on line 13, we see that our extension function has been imported, just as if it was another class. So, this is how Kotlin allows classes to share extension functions. Now, if we like, we could've also made this an extension property. Let's do that now on the planet class.
So, the only thing we need to do is change here, at the beginning of line seven, instead of using the fun keyword, we're going to use val 'cause we want this to be a property, and then we're going to remove the use of the parentheses, and then we're going to implement the getter. So, right behind where we have our string, let's do an enter, and then we'll add our call to get. So, if we just align this properly, everything is good to go. The main thing to keep in mind with extension properties is that they can't hold any state, so you need to always at least implement the getter.
So, if we go back to our fragment, we're just going to do one small update. We're no longer using a function this time. We're going to use a property. Everything is good to go. So, we've seen how we can take advantage of extension functions and properties in order to help with code encapsulation.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections