Is it Good to Abuse Kotlin Extension Functions

How we are Abusing Kotlin

Well, sometimes we humans have a tendency to abuse some things in the context of full filling our bad habits. A simpler example of that would be, we cover our bad habit of eating sugar with the justification of doing exercise. Which is not at all right to eat more sugar because you have done some exercise. So, getting back on the topic, we human took the aid of one good thing to cover our one bad habit.

Today, we are talking about the Kotlin, a very well structured and beautifully designed programming language. This language has eased down the process of writing codes for the developers very much. Kotlin has numerous cool features and out of them, the extensions function is most abused by the developers.

That’s why today we are going to study the Kotlin extension function and how are developers abusing it.

Why Extensions Are Good

Before understanding that how developers are abusing the extension function, let’s study that why the extension is good. If you have used java earlier, then you must be familiar with the “Utils” classes such as FileUtils, StringUtils and so on.

The famous java.utils are part of this collection and the biggest problem with these utils classes is that name always comes in the way of classes. Such as:

Collections.swap(list,

               Collections.binarySearch(list, Collections.max(otherList)),

               Collections.max(list))

But, with the code complication and easy readability Kotlin has reduced this code to;

list.swap(list.binarySearch(otherList.max()), list.max())

However, we can implement list methods inside the list class with this code. So, in lieu to implement list methods, people we need the help of the Kotlin extension function.

What Is An Abuse With Example

As the name suggests, the extension means extending the abstraction of code. There is no problem with the extension of code, but the problem occurs when the bad or absent abstraction is extended. To better understand the abuse let’s taken example of Google I/O code that could be refactored to use extension functions, but probably shouldn’t be. By using the extension method in the context of the code may look like:

// PrefExtensions.kt

fun Context.setAttendeeAtVenue(newValue: Boolean) {

   val defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)

   defaultSharedPreferences.edit().putBoolean(BuildConfig.PREF_ATTENDEE_AT_VENUE, newValue).apply()

}

 

fun Context.markAnsweredLocalOrRemote(newValue: Boolean) {

   val sp = PreferenceManager.getDefaultSharedPreferences(this)

   sp.edit().putBoolean(PREF_ANSWERED_LOCAL_OR_REMOTE, newValue).apply()

}

 

// AttendingFragment.kt

override fun onClick(v: View) {

   // Ensure we don’t run this fragment again

   LOGD(TAG, “Marking attending flag.”)

   mActivity.setAttendeeAtVenue(true)

   mActivity.markAnsweredLocalOrRemote(true)

}

You see people, utilization of the extension function in the above-mentioned example is a mere abuse of the function. As without the utilization of extension function this equation can be easily created and that with the lesser code. By far we can pinpoint only Attendee abstraction is missing in the equation. Which can be easily inserted and with this code will look like this;

interface Attendee {

   enum class Attending {

       IN_PERSON,

       REMOTE,

       UNKNOWN

   }

 

   fun setAtVenue(newValue: Boolean)

   fun attending(): Attending

This code can be written in numerous other ways also without using the extension function.

Final Words

So, you see people, sometimes over using the one excuse can become dangerous for you.  As the code mentioned in the above example can be easily written without the help of the extension function. On the contrary, the extension has complicated the whole process. That’s why don’t abuse the extension function of the Kotlin as it will eventually bite you back in the ass. So, use Kotlin features, but don’t abuse them.

Send a Message