Custom mobile app development: sharing our experience in Kotlin coding
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:
1. Moving Java written project to Kotlin language
2. 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
- Smart casts
- 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:
The amount of code in %
The results were expected: 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 method to a Context native class.
Compare method calls for Toast message display:
// Java code:
ToastUtils.showMessage (context, “message”);
// Kotlin code:
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.
Briefly about Kotlin language advantages as a conclusion:
1. Lean syntax. One line functions and simple structs generally take one line, functions can exist outside of classes., which helps to get a readable and intuitive syntax. Kotlin variable declarations and parameter lists have the data type come after the variable name like it is in Scala, Go, Pascal, PL/SQL, etc.
2. Null safety. This allows the compiler to systematically flag potential null pointer dereferences. Also Kotlin programming language doesn’t include an option type.
3. Functional programming support. Provides zero-overhead lambdas as well as the possibility to make mapping and folding over standard Java collections.
4. Extension functions. They allow mobile app developers to add methods to classes without modifying their source code. Also extension functions enable us to easily discover new methods via auto-completion, implement existing Java APIs with other Kotlin features, etc.
5. Companion objects. Unlike Java and C#, Kotlin language classes don’t involve static methods. Instead they offer package-level functions. For example, if you need to write a function with the ability to call it without having a class instance, you can make it as a member of an object declaration inside this class. If you declare a companion object inside the class, you can call its members with the same syntax as with static methods in Java or C#.
6. Low risk in adopting Kotlin. This is a concise and easy-to-learn programming language which can be easily adopted in your company. One of strong points is that Kotlin classes export Java API which generally looks the same to that of Java code.