3 months of Kotlin, code review summary

Elye
Elye
Aug 24, 2017 · 3 min read

After Google endorsed the use of Kotlin at Google IO 17, many have started using it. On my end, it’s about 3 months since it has been used for development. As part of learning, I’m sharing a summary (non exhaustive) of items detected during code reviews .

Improvement Detected

val datamap = HashMap<String, Any>()
datamap.put(“A”, ID)

Improvement

val datamap = mapOf(“A”, ID)
private fun startEvent(id: String) {
// Some logic
}
private fun startEvent(id: String, value: String) {
// Some logic
}

Improvement

private fun startEvent(name: String, value: String? = null) {
// Some logic
}
@BindView(R.id.sort_type) @JvmField
var sortTypeView: TextView? = null
fun doSomething() {
sortTypeView.text = "abc"
}

Improvement

fun doSomething() {
sort_type.text = "abc"
}

Refer to https://antonioleiva.com/kotlin-android-extensions/ for more interesting discussion on Android Extension.

fun <T> whenever(methodCall: T): OngoingStubbing<T> 
= Mockito. `when`(methodCall)

Improvement

import org.mockito.Mockito.`when` as whenever
val settings = webview.settings
settings.setAppCacheEnabled(true)
settings.setAppCachePath(absolutePath)
settings.domStorageEnabled = true
settings.displayZoomControls = false

Improvement

with(webview.settings) {
setAppCacheEnabled(true)
setAppCachePath(absolutePath)
domStorageEnabled = true
displayZoomControls = false }
if (progress > MAX) {
progress = MAX
} else if (progress < MIN) {
progress = 0
}
progressBar.progress = progress

Improvement

progressBar.progress = progressValue.clamp(MIN, MAX)private fun Int.clamp(min: Int, max: Int): Int 
= Math.max(min, Math.min(this, max))

Updates. Thanks to Dmitry Jemerov who twitted respond to me that Kotlin Stdlib already have that extension, named coerceIn.

interface NeedInterface {
fun setNeeded(needed: Boolean)
fun isNeeded() : Boolean
}

Improvement

interface NeedInterface {
var isNeeded : Boolean
}
if (progress_bar != null) {
hideProgressBar(progress_bar)
}

Improvement

progress_bar?.let { hideProgressBar(it) }

Neat Coding Detected

Other than constant value, one could set a function call as default parameter

fun myMessage(message: String = context.getString(R.string.default))

There were some report the overhead of binding View Holder view in https://proandroiddev.com/the-costs-of-kotlin-android-extensions-6809e2b32b13. The below is a neat workaround. (might not be needed now with Kotlin 1.1.4)

fun <T : View> bind(view: View, @IdRes res : Int) : Lazy<T> { 
@Suppress(“UNCHECKED_CAST”)
return lazy(LazyThreadSafetyMode.NONE)
{ view.findViewById(res) as T }
}
private val myTextView: TextView by bind(view, R.id.my_text_view)

Mockito Any is nullable. But with Kotlin, some function doesn’t acceptable nullable value. Below would be handy workaround.

fun <T> any(): T {
Mockito.any<T>()
return null as T
}

Imagine if you have two different type of possible values. You could group with using sealed class

sealed class BaseValue

data class GoodValue(val result: String) : BaseValue()
data class BadValue(val error: String) : BaseValue()

Then, upon receiving the value, you want to ensure you code all the possible conditions, add the return statement in your function as below (though it is not needed by the function).

private fun receivedValue(value: BaseValue) {
// return to ensure all cases covered
return when (value) {
is GoodValue -> { //Do Good }
is BadValue -> { // Do bad }
}
}

This allow the compiler to error when a new BaseValue is added in the definition, but not added in the receivedValue function.

Hope this provides you some useful insight in your journey in Kotlin. I’ve also blog on the issue found during this time in

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

Mobile App Development Publication

Sharing Mobile App Development and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store