Mobile app development: sharing experience in Kotlin coding
Tags: kotlin, android
Published: 2017-07-27 14:52:05

---
layout: post
title: "Mobile app development: sharing experience in Kotlin coding"
published: true
tags: kotlin, android
cover_image: "https://thepracticaldev.s3.amazonaws.com/i/91owafcblawuuu6151b2.jpg"
---

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:


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:

<ul>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Lambda expressions + Inline functions = performance custom control structures</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Extension functions</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Null-safety</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Smart casts</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Properties</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Companion objects</span></li>
 	<li style="font-weight: 400;"><span style="font-weight: 400;">Primary constructors</span></li>
</ul>

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/.

---