Why You Should Totally Switch To Kotlin.

When we talk about Android app development, we immediately think of Java. But should we? So in this Post I am going to explain few reasons why we should all think about switching over to Kotlin instead.

Tech is full of stories where a new technological marvel comes along to challenge the long-established order. When we talk about Android app development, Java is the dominant programming language. We all immediately assume this is so. But is it true?

For me, no. There are so many other languages that can be used for writing Android apps that are also compliant with the JVM.

Which brings me to my main point for today: JAVA is obsolete. It has been overthrown by Kotlin.

Kotlin is going to be the future platform for designing and writing Android apps. In fact, many developers have already titled it as the premium class language for writing the Android Apps.

Peter Ducker once said, “For new technology to replace old, it has to have at least ten times the benefit.” Following that maxim, I’m going to go over ten significant reasons why Kotlin should be adopted as the new official programming language for Android apps. These reasons should explain why Kotlin has garnered all the kudos it has despite the crowded field.

In the last post, I had already told you about the Kotlin and promise you to tell you about the reasons to switch to Kotlin in the next post.
 
So here’s a couple of reasons why you should totally switch to Kotlin (in no particular order):

0# Java Interoperability

Kotlin is 100% interoperable with Java. You can literally continue work on your old Java projects using Kotlin. All your favorite Java frameworks are still available, and whatever framework you’ll write in Kotlin is easily adopted by your stubborn Java loving friend.

1# Familiar Syntax
 
Kotlin isn’t some weird language born in academia. Its syntax is familiar to any programmer coming from the OOP domain, and can be more or less understood from the get go. There are of course some differences from Java such as the reworked constructors or the val var variable declarations. The snippet below presents most of the basics:


2# String Interpolation

It’s as if a smarter and more readable version of Java’s String.format() was built into the language:


 
3# Type Inference

Kotlin will infer your types wherever you feel it will improve readability:


 
4# Smart Casts

The Kotlin compiler tracks your logic and auto-casts types if possible, which means no more instanceof checks followed by explicit casts:



5# Intuitive Equals

You can stop calling equals() explicitly, because the == operator now checks for structural equality:


6# Default Arguments
 
No need to define several similar methods with varying arguments:


7# Named Arguments

Combined with default arguments, named arguments eliminates the need for builders:


8# The When Expression

The switch case is replaced with the much more readable and flexible when expression:


 
It works both as an expression or a statement, and with or without an argument:



9# Properties

Custom set & get behavior can be added to public fields, which means we can stop bloating our code with mindless getters & setters.


 
10# The Data Class

It’s a POJO complete with toString(), equals(), hashCode(), and copy(),  and unlike in Java it won’t take up 100 lines of code:




11# Operator Overloading

A predefined set of operators can be overloaded to improve readability:



12# Destructuring Declarations

Some objects can be destructured, which is for example useful for iterating maps:



13# Ranges

For readability’s sake:


 
14# Extension Functions
 
Remember the first time you had to sort a List in Java? You couldn’t find a sort() function so you had to ask either your tutor or google to learn of Collections.sort() . And later when you had to capitalize a String , you ended up writing your own helper function because you didn’t know of  StringUtils.capitalize().

If only there was a way to add new functions to old classes; that way your IDE could help you find the right function in code-completion. In Kotlin you can do exactly that:


 
The standard library extends the functionality of Java’s original types, which was especially needed for String  :



15# Null Safety
 
Java is what we should call an almost statically typed language. In it, a variable of type Stringis not guaranteed to refer to a String— it might refer to null. Even though we are used to this, it negates the safety of static type checking, and as a result Java developers have to live in constant fear of NPEs.
Kotlin resolves this by distinguishing between non-null types and nullable types. Types are non-null by default, and can be made nullable by adding a ? like so:


 
Kotlin forces you to guard against NPEs whenever you access a nullable type:


 
And while this might seem cumbersome, it’s really a breeze thanks to a few of its features. We still have smart casts, which casts nullable types to non-null wherever possible:


 
We could also use a safe call ?. , which evaluates to null instead of throwing a NPE:


 
Safe calls can be chained together to avoid those nested if-not-null checks we sometimes write in other languages, and if we want a default value other than null we can use the elvis operator ?: :


 
If none of that works for you, and you absolutely need a NPE, you will have to ask for it explicitly:



16# Better Lambdas
 
Oh boy, is this a good lambda system — perfectly balanced between readability and terseness, thanks to some clever design choices. The syntax is first of all straight forward:



And here come the clever bits:
  1. Method parentheses can be moved or omitted if the lambda is the last or the only argument of a method.
  1. If we choose not to declare the argument of a single-argument-lambda it’ll be implicitly declared under the name it .
These facts combined makes the following three lines equivalent:


 
And this allows us to write concise functional code — just look at this beauty:



Kotlin’s lambda system combined with extension functions makes it ideal for DSL creation. Check out Anko for an example of a DSL that aims to enhance Android development:


 
17# IDE Support 

You have a number of options if you intend to get started with Kotlin, but I highly recommend using IntelliJ which comes bundled with Kotlin—its features demonstrate the advantage of having the same people design both language and IDE.
Just to give you a minor but clever example, this thing popped up when I first tried to copy-paste some Java code from Stack Overflow:



Comments

  1. I'm glad you liked my article, but taking credit for other peoples work is not cool. Would you mind removing this?

    ReplyDelete

Post a Comment

Popular posts from this blog

The Principle About Bluetooth 5.0 - Explained

India, The Apparent Land Of kamasutra - Becomes The hird-highest visitors of Porn Hub

Google Tez, Digital Payment App 'Made for India' - Everything You Should Know Before Using It.

Technical News - A Leap Ahead.