Hello all. I’m fairly new to the OpenMRS community, (I joined around the onset of GCI 2017, but I have been developing for Android outside of OpenMRS for about 6 years). I have a somewhat major suggestion for the Android client that I would like to bring up to the community.
Simply put, I think the Android Client should be upgraded to use the Kotlin language alongside with or as a replacement for the existing Java code.
This may seem like a really unnecessary major change, but hear me out first.
First of all, what is Kotlin?
Kotlin is a language developed by JetBrains that compiles to the JVM and contains some powerful advanced language features. If you are interested in a more syntaxical introduction to Kotlin, check out Kotlin Koans.
Here are some major advantages of using Kotlin over or with Java:
Kotlin is 100% compatible with Java
It was developed by Jetbrains and is officially supported in Android Studio as well (and now supported as an official language of Android by Google). This interoperability means that we could make this transition a file at a time and have no issues with our existing codebase. The new Kotlin files could call methods and even inherit Java classes with no issues as well.
Kotlin provides advanced language features
Kotlin provides advanced language features that Java is only now receiving or may never receive. A good example is lambdas. I’ve noticed you are using me.tatarka.retrolambda in the Android project. Kotlin would provide native support for lambdas. Simply put, Kotlin can just do things that Kotlin can’t.
Code Maintainability and Readability Would Increase
Another major issue that could be improved by making the switch to Kotlin is code maintainability and readability. The codebase would shrink as well, and as every developer knows, less code means less bugs. Here is a quick Java vs. Kotlin code snippet that highlights the power of Kotlin’s concise syntax:
A common occurence in Android Java:
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
///Handle button click here
}
}
Exact same snippet in Kotlin:
button.setOnClickListener { view ->
//Handle button click here. (Note that `button` is the id of the view. Kotlin has android extensions that allow you to completely remove View binding!)
}
Or, as most Android Kotlin developers do, you can create extension functions that make the above example even more concise!
button.onClick {
//Handle button click here.
}
This is just one example, but hundreds of lines of code could be removed, making the code more readable and maintainable.
Other notable features:
Great support for Rx and http third party libraries and
Due to Kotlin’s ability to use lambdas, callbacks and specifically reactive programming become a breeze. Also, due to sealed classes and the when statement in Kotlin, http requests can become super simple but powerful.
Supported Major IDEs
Kotlin is officially supported in IntelliJ and Android Studio, and is supported via a plugin in Eclipse.
No more NullPointerException
s!
A smaller advantage, but one that may be well received, is that Kotlin removes Java’s silly NullPointerExceptions. That means no more issues like this one : https://issues.openmrs.org/browse/AC-71 (But don’t worry, they still exist, they just don’t come unless invited).
Disadvantages
- Kotlin may take slightly longer to perform a clean build. (It is a pretty trivial time difference in my opinion)
- About 800 kilobytes may be added to the app. This is probably trivial compared to the current app’s size.
- Simply that Kotlin is a different language, so you may have to do a little bit of learning while you develop, but I promise Kotlin will be a helpful language to learn.
One last note
Kotlin lends itself really well to an MVVM architecture, so we may also want to consider making that switch at a future date, but it will have absolutely no problems with the existing MVP architecture and probably make it even easier to implement.
Resources
If you are unfamiliar with the Kotlin language, first check out https://kotlinlang.org for tutorials and documentation
For specifically Android Kotlin, check out http://kotlinlang.org/docs/reference/android-overview.html for some more advantages of Kotlin and other resources.
Conclusion
Kotlin has some clear advantages when it comes down to it. I haven’t even begun to cover them all. I personally think we should make the switch (even if it is just with just one or two files at first). What do you guys think?