paint-brush
Kotlin’s development pot holes found in 3 monthsby@elye.project
768 reads
768 reads

Kotlin’s development pot holes found in 3 months

by ElyeAugust 25th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

As mentioned <a href="https://medium.com/@elye.project/3-months-of-kotlin-code-review-summary-8217e3f18e9">earlier</a>, it has been 3 months developing Kotlin again after Google endorsed it in Google IO 17. Among the great things enjoying, there’s also various limitations found. Just to share a few&nbsp;here.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Kotlin’s development pot holes found in 3 months
Elye HackerNoon profile picture

As mentioned earlier, it has been 3 months developing Kotlin again after Google endorsed it in Google IO 17. Among the great things enjoying, there’s also various limitations found. Just to share a few here.

Smart-casting limitation, not checking beyond the function

I have a function as below

private fun String?.isGood(): Boolean {
   return this != null && !this.isNullOrEmpty() && this.check()
})

You’ll notice there’s a redundant check this != null since we already have !this.isNullOrEmpty(). It is required, the compiler will assume it could still be null at this.check(). Smart-casting to can’t check internal to !this.isNullOrEmpty().

Companion Object is not inheritable in Kotlin, but accessible in Java

open class KotlinBase {
    companion object { const val TAG = “testing” }
}

class KotlinChild : KotlinBase()

When I try access the TAG as below

class KotlinOther {
    val test1 = KotlinBase.TAG // This is okay
    val test2 = KotlinChild.TAG // Compile/Syntax error
}

However if you access through java, they are okay.

public class JavaOther {
    String test1 = KotlinBase.TAG; // This is okay
    String test2 = KotlinChild.TAG; // This is okay
}

Custom View constructor overload

It is cool we have @JvmOverloads to help shrinking the 4 Custom View constructor to one constructor.

class MyView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, 
    defStyle: Int = 0, defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyle, defStyleRes)

However, this will crash if you support Android SDK 19 and earlier. You’ll need to define it as below instead, separating out defStyleRes which is only available in SDK 21 and later.

class MyView : LinearLayout {
    @JvmOverloads
    constructor(context: Context, attrs: AttributeSet? = null,
        defStyleAttr: Int = 0)
    : super(context, attrs, defStyleAttr)

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet?, 
        defStyleAttr: Int, defStyleRes: Int)
    : super(context, attrs, defStyleAttr, defStyleRes)
}

Warning on EventBus unused parameter

In using EventBus, the parameter are used as a flag to know where it would land. If there’s nothing we need to pass along, the parameter is not used as below.

fun onEvent(event: EventTypes.MyEvent) {   
    trigger(EVENT_TAG, MY_EVENT)
}

Kotlin compipler will complain as below.

MyKotlinClass.kt: (11, 33): Parameter ‘event’ is never used

There’s a good intention of checking this thought I have to admit. So I just use @Suppress(“UNUSED_PARAMETER”) to suppress it :P

Gson deserialized result could make non-null variable null.

Null Safety is cool. But the example below would break it. A null variable could be printed, and still works without error, until you try to access it.

fun forceNull() {
    val nullBody = “””{ “nonNull” : null}”””
    val nullNonNull = Gson().fromJson(nullBody, MyData::class.java)
    System.out.println(nullNonNull)
}

data class MyData (val nonNullString :String)

A post Gson deserialization check is needed to protect yourselves.

Coverage Support broken in Android Gradle Plugin 3.0.0-beta2

I think among all, this would be the biggest hole. Coverage doesn’t work on latest Android Studio :(

Android Studio 3.0 gradle 3.0.0-beta2, breaks Kotlin Unit Test Coverage?

Well, these all are Pot Holes. They are still not Sinkhole. So we could still walk the journey to ship the product. Nonetheless, hopefully it would be patched soon.

If you like my post and want to get future update, follow me at medium~Twitter:elye; Facebook:elye