Get Started with Kotlin — It is here to stay

Functional programming is the present & Its time Android developers experience the happiness of coding.

It’s been a while since Google extended its support to Kotlin and app developers have already started moving to Kotlin. But while we adopt modern programming, its also important that we have a look at why this change is so important and is widely accepted.

Having worked in Android for a year+ now, I would like to take you through this change right from the start to how it is seen from the Android perspective. Here we go!

Functional programming is the key for the change

Now, there has been a lot of complex definitions hanging around in the web defining it, but to put it in a clear and concise way:

A programming method where functions based on mathematics are the building blocks and the focus is more on what you want to achieve than how you achieve it.

That said, functional programming deals with immutable data i.e. it doesn’t change the value of the variable. All it does is, Receive->Compute->Return without changing values it receives or causing side effects like changing values elsewhere.

Avoiding side effects is an important aspect of functional programming as it makes debugging a trillion times easier, so that you don’t end up searching the whole program for a bug caused by a variable value.

Here is an example of adding 2 t0 an array of numbers:

Imperative type:

int[] nums=new int[]{1,2,3,4,5};

for(int i=0;i<nums.length;i++){

nums[i]=nums[i]+2;

}

return nums;

Functional type:

def addTwo(x):x+2

result=map([1,2,3,4,5],addTwo)

The functional example is also what we would call a Higher-Order Function, as map function takes another function(addTwo) as an argument.

Below is an example of Higher-Order Function that takes a Lambda function as an argument.

Higher-Order Function:

max(strings, { a, b -> a.length < b.length })

Lambda Function:

b -> a.length < b.length

A Lambda Function is a function that is not declared, but passed immediately as an expression.

Now that we know that functional programming is the key to avoid side effects and mutable data (common in imperative programming) and as it helps in writing concise code thereby improving code readability, it is why a step forward and is most adopted in modern programming languages.

Functional programming is sane, straightforward, efficient and beautiful.

Despite having around such a paradigm, questions pop up that why it hasn’t made the cut to Android yet and to know about that we have to dig deeper into the relationship of Java and Android.

Why is Android stuck at Java 6 despite Java 8 being functional?

Ever since Java 6 came into existence, it took two years for Android to support it and be born, but then it never got better and despite Java 7 in 2011 & Java 8 in 2014 coming into play, Android’s adoption is still fairly fragmented, and Java 7 is only supported for API 19 and up.

Nearly 50% of the users use Android API version 18 and below. Source: https://academy.realm.io

The reason why Android could never support Java fully is because,

For every new release of Java, the device needs to be updated to support the new features of Java, and it introduces new bytecodes, so a device that can’t be updated to the latest version of android can’t make use of the new bytecodes.

This, is exactly why Android is still stuck at Java 6, but then lets assume if Android could extend 100% Java 8 support to all its devices in the next 2 years from now, Why shouldn’t we wait for Java 8 as it supports functional programming?

The straight answer would be:

Java as a language itself has a lot of problems.

Following are few of the problems:

Null references , coined by its inventor as a billion dollar mistake: You can code very defensively, but null pointer exceptions sneak into everyone’s code just because Java’s type system is not safe .

, coined by its inventor as a billion dollar mistake: You can code very defensively, but null pointer exceptions sneak into everyone’s code just because . Raw types:We’ve been stuck with raw types in order to stay backwards compatible.

List numbers=getNumberList();

int sum=0;

for(Object num : numbers){

sum+=(Integer)num; // Unchecked cast

}

Covariant arrays: You can create a string array, and an object array and then assign that string array to that object array, but it’ll throw an exception if you try to assign a number to that array of strings.

String [] strings={"hello"};

Object[] objects=strings;

objects[0]=1; // java.lang.ArrayStoreException

Java 8 has higher-order functions but they’re implemented with SAM types . SAM is single abstract method, every function type needs an interface correlated to it.

. SAM is single abstract method, every function type to it. Wildcards in generics, anything but non-trivial generics can get out of control and become hard to read, write and understand.

And with the confessions of a Java developer, took birth is a new language, Kotlin.

Statically typed programming language for modern multiplatform applications

But, lets see why would some one prefer Kotlin, when there are languages like Scala and Groovy already in the race.

Why Kotlin & Why Kotlin + Android?

Android loves Kotlin

Moving forward, its everything about Kotlin in Java vs Kotlin perspective, so be ready to be amazed.

#Kotlin puts properties at front

A very good example of boilerplate code would be a Java Class, where everything except for the variable decalrations, seems like unwanted and excessive lines of code, this is where Kotlin comes into play.

Kotlin eliminates the need to add & maintain lines of codes that take care of getting, setting and converting values of variable.

//Java //Declaring Class public class Person{ //Variables String name;

int age; //Constructors public Person(String name, int age){

this.name=name;

this.age=age;

} //Getters and Setters public String getName(){

return name;

}

public int getAge(){

return age;

}

public setName(String name){

this.name=name;

}

public setAge(int age){

this.age=age;

}

} //Kotlin Equivalent data class person(val name:String val age:Int)// That's it?

Kotlin doesn’t have Getters and Setters , it has only properties.

#Kotlin is functional:

Having said so much about functional programming, Kotlin indeed is functional and that is why it is elegant, concise, expressive , readable and understandable.

Kotlin is not 100% functional programming because immutability is optional, but thats a pro.

#Kotlin’s Syntax feels like family

Kotlin’s syntax stays very close to its logical context .

. It cuts down lot of ceremonial boilerplate codes seen in Object Oriented Programming languages.

lot of seen in Object Oriented Programming languages. Easily understandable by novice developers and is also simple and short.

//Java int num=0;

String str="Kotlin"; // Both are mutable and has side effects of being changed anywhere in the code. //Kotlin val str="Kotlin" // Type is optional & can be explicitly defined.

val str: String = "Kotlin" //val is immutable & remains unchanged.

var num: Int = 0 // var is mutable & can be changed anytime.

Property of Immutability is controlled by two variables, var & val, its type is inferred by itself.

#Kotlin is in terms with Java