Once officially released on February 2016 Kotlin fastly became a hot programming language. Kotlin is a statically-typed programming language developed by JetBrains and running on the Java Virtual Machine. Also, it can be compiled to JavaScript source code or JVM bytecode.
Custom mobile app development on Kotlin programming language offers many advantages. First, the main aim of JetBrains was to make it as quick as Java and easy to learn. And they managed to do that.) Second, Kotlin provides lean syntax, extensions features, null-pointer safety, and infix notation.
One of the strong and important things there is the fact that Kotlin language came from the industry, not academia. It was developed to solve programmers’ difficulties and operational challenges.
We also supported this technology trend and made 2 experiments in using Kotlin:
- Moving Java written project to Kotlin language
- Project development on Kotlin from scratch
We wanted to pay special attention to Kotlin features that Java doesn’t have. Here is a list of the most interesting and useful Kotlin introductions:
- Lambda expressions + Inline functions = performance custom control structures
- Extension functions
- Null-safety
- Smart casts
- Properties
- Companion objects
- Primary constructors
Moving Java project to Kotlin programming language
JetBrains took care of the ability of automatic conversion of Java classes to Kotlin ones. Making the conversion of different project classes we received the following results: we got Kotlin code, on the average 20-25% less than the same on Java.
So, we made sure that Kotlin language allows to write more elegant and concise code. This is the most visible in model classes as Kotlin Properties allow to dispense with getters and setters.
Also we were pleased by Smart Class: even a person who understands nothing in programming will be able to put value of simplicity and sophistication of code written in Kotlin programming language comparing it with Java:
// Java code:
if (getActivity() instanceof LoginActivity)
((LoginActivity) getActivity ()). register ();
// Kotlin code:
if (activity is LoginActivity)
activity. register ()
Project development on Kotlin language from scratch
Product development on Kotlin gave us even more pleasant impressions. We believe that Kotlin Android Extensions should be described first of all.
Every Android app developer knows well a findViewById() function. Without a doubt, it’s a source of potential bugs and nasty code which is difficult to read and support.
While there are several available libraries that provide solutions to this problem, there are libraries dependent on runtime and requiring annotating fields for each View.
Also, a Kotlin Android Extensions plugin enabled us to obtain similar experience we had with some of these libraries, without having to add any extra code or shipping any additional runtime.
In Kotlin class developers can directly use a view’s id without additional initialization or leading to the required type as this language will take care about it itself. So, as an example, there is no necessity to use ButterKnife library for dependency injection.
Someone may object that listeners’ initialization takes a lot of place and looks unattractively while the very ButterKnife allows to eliminate this problem. Nevertheless, here Kotlin programming language successfully copes without third-party libraries: Lambda expressions and inline functions provide everything you need to write a concise, intuitive, and perfectly working code.
In our custom mobile app development we used a lot of utility classes for not to load Activities and Fragments with extra logic. For example, to completely implement the display of Toast system message throughout the application improperly.
That’s why we moved this in utility class. However, to display this message app developers have to always transfer a Context system object. Thanks to Kotlin Extension Functions we can add a method to a Context native class.
Compare method calls for Toast message display:
// Java code:
ToastUtils.showMessage (context, “message”);
// Kotlin code:
context.toast(“message”)
Looks great, doesn’t it?
Thus, we received positive impressions from writing in Kotlin thanks to the advantages and smart introductions it provides engineers with. We believe in the potential of this programming language and plan to continue gaining experience in our custom mobile app development.
Lear more about Kotlin project experiments and Kotlin benefits https://smartym.pro/blog/sharing-experience-in-coding-in-kotlin/.
Top comments (4)
Great post! I don't know a thing about mobile development, Java, or Kotlin, but I felt pretty comfortable reading the whole post.
Kotlin seems to be a going on an upward trend this year; do you think it'll eventually be the main language that most Android devs use?
Glad you've liked the post. For now, Kotlin sees rapid growth and attracts a lot of attention. As you probably know, recently Kotlin founders released a new version (Kotlin 1.1.2) with many improvements. What's more, Android announced Kotlin official support. Comparing to Java, Kotlin is more simple and no less quick. So, I believe, it has the potential to become the main language for Android dev.
Kotlin's concise syntax and powerful features like null-safety, extension functions, and smart casts significantly enhance mobile app development. Transitioning from Java to Kotlin not only reduces code complexity but also improves performance. Kotlin’s built-in Android Extensions make development smoother and free from third-party libraries, offering a cleaner experience.
I really enjoyed this blog on the firsthand experience about developing mobile applications using Kotlin. It is also very practical in informing readers who may want to transition from Java for a couple of good tips on handling the syntax of Kotlin and its interoperability with Java. I also like how the blog talks about some of the hurts of app development and how Kotlin's features, such as null safety and extension functions, can remedy a few of those ouchies. Here's where Kotlin beats all other languages when it comes to Android development, which this post does an excellent job at. Overall, an informative and empowering read for someone looking to know a bit more about Kotlin.