Kotlin has many amazing features but one that stands out is extensions. Extensions are used to add functionality to existing classes.
They are extremely useful at helping to reduce boiler plate code. The good thing is that you can add them to any class, including the ones from third-party libraries that you can’t modify.
Most of the time we see Extension functions but Kotlin also supports Extension properties.
Lets start with some extensions that make the code easier to read, the ones that improve the code quality over time by reducing boiler plate code.
The Android API changes over time and something as simple as getting the color for a given ID can become not so simple. The extensions below helps reduce the boiler plate code associated with retrieving resources for a given ID.
We all know how repeating it’s to check whether the user has granted the permissions the app requires. The extension below can help with that. It doesn’t solve the whole problem but helps reduce the code size.
Copying some text or URL to the clipboard should be something very simple. It only works with plain text but that’s what we use most of the time, if you need to support other formats you can change it.
Is there an URL in your app that the user needs to open the browser to view? Are you handling the case where the browser or equivalent app might be missing? The next extension abstracts most of what’s needed. You just need to give it the Uri and do something such as showing an error if there’s no app to resolve the intent.
This extension is very useful if you need to log a date object over and over or send it via an API call. Something to notice here is that the ISO format is universal, it doesn’t change from country to country, I’ll show why that’s important later.
Some other extensions that might be useful
Many things including extensions can be abused. Some of them look good, but are actually disguised code smells.
Let’s start with an useless extension. I was reading an article about extensions the other day and found this one.
- There’s no code reduction by using the extension.
- By using the extension the compiler will consider it a normal function call and refrain from optimizing the null case if possible.
- That’s bad use of OOP, we add methods to objects to “ask” them about something, null by definition is the absence of an something. An object is never null, what’s is null is the reference to an object.
The extension below may look reasonable to most of us. It formats a Double to its decimal format.
The dangerous thing about this code is that money is not formatted equally worldwide. In some countries you’ll see 1,200.00 (comma first, dot after), in others 1.200,00 (dot first, comma after). This is only a problem if the app is/will be used in many countries.
My solution would be to add the formatting pattern to
strings.xml and create an extension on
Context to handle that. That way you can define the monetary format for each language and still use the same function.
Kotlin also allows us to add properties as extensions. I’ve not found an use case for them yet but it’s useful to know that it’s possible to use them.
Thank you for reading. If there’s anything you disagree with, please leave a comment below so we can discuss it and arrive at a better solution.