Kotlin – Part 1- Exclusive features that you should use

Tram Ho

Unlike Java, where we need to write everything, the Kotlin compiler can understand the code and define the code itself. This increases code productivity and also saves time.

Kotlin makes Android software development more interesting

If you’ve gone through Kotlin, you’ll find countless ways that Kotlin has addressed many of Java’s weaknesses and Kotlin’s great features that make Android development more interesting. As Open Source For You describes

“Kotlin is a multi-platform programming language that is concise, safe, interoperable and tool-friendly. It is a statically-typed programming language that runs on the Java virtual machine and can also be compiled to the JavaScript source code or can use the LLVM compiler infrastructure. ”


Kotlin is a concise, secure, well-interacting, cross-platform programming language and a friendly tool. It is a static programming language that runs on a Java virtual machine and can also be compiled into JavaScript source code or can use the LLVM compiler infrastructure.

Regardless of the features you are viewing here, most of them are applicable to other platforms (navite code, android, JVM, etc.), where Kotlin is used for development. Let’s find out if Kotlin is so cool.

Kotlin Extensions

The first concept I’m talking about here is Kotlin Extensions. As described in Doc :

“Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use design patterns such as Decorator.”


Kotlin provides the ability to extend a class with new functionality without having to inherit or use a desing pattern like Decorator.

We can add methods to existing classes or interfaces without inheriting them. For example, we can add a new function to a class in a third-party library without modifying the sample code, the part we added is still used as a normal library function. Such functions are called extension functions.

Default Kotlin Extensions

Kotlin is a package of many useful functions. Kotlin’s default extensions can be filter , single , first and last in array-list as well as last to check the last character in a string.

filter

Filter is an array-list extension function through which we can filter the results, as in the example below:

In Java, we need to write all the loops, create a separate array, and add all objects to a new array, etc. With Kotlin we say “NO”.

single

The single is quite similar to the filter when it also filters but only filters out one result with the given condition (gossip like JS or Python) 😜

first and last

As the name implies, they return the first and last value of the array

Of course, if that’s all, don’t say anything. We can add nested conditions as well

Speaking through the fog, Kotlin also has many of the above extensions that help us reduce boilerplate code. This is just the tip of the iceberg.

User-Defined Extensions

We can create extensions for any class or interface in Kotlin to have our desired functions. Here are some of the extensions I use often.

ViewExtensions

Let’s first take a look at the example below, writing in the usual way

The code seems to be quite long for a click, assuming there are dozens of textview to click like. With Kotlin, things were easier. First create file viewextensions and then rewrite the extension function for RxClick in View:

We write more functions for View, like to write in it, as long as you like. Just kidding, but writing so appropriately, miscellaneous will die immediately. reactiveclick similar to the general-click function in View: setOnClickListener . And now it’s free to use everywhere

Do a little comparison huh

We will recognize the advantages of extension functions when we start using them. It’s like you’re making your own style. Here are some things that I use in my project to scare people, and to increase productivity for code (hehe):

String extensions

We can write any function to apply on the chain as required at the project level. For example, when we make the chat app to secretly date with Tuesday, if we want the user to type @, the list of user suggestions (that Facebook Messenger) can be done as follows:

Context extension

This is one of my favorite things. There are many extension functions that we can write using context like setting the status bar color, taking the height and width of the screen, displaying the toast and even checking the internet connection. Take a look at some of the things that I use:

Looks delicious yet 😂😂😂

But note that do not include all extension functions in one file. Be careful after a month or two you will look dizzy. Let’s create separate files like ViewExtensions , StringExtensions , FileExtensions , ….. as the way we work with files in Java. These are the regular funtsion extensions I use, of course, we can use more depending on the characteristics of each project.

Higher-Order Functions

A function that can be passed as a parameter or can be returned from a function is called higher-order-functions. In other words, higher-order-functions is a function that takes functions as parameters or returns functions. This function is very handy regardless of the platform you are developing. I feel the higher-order-functions are very handy to replace click listeners and pass functions as parameters. To better understand it, see an example:

The most common thing we all use is to create a RecyclerView in which we create click listeners and pass it into the adapter. Instead, we will pass the function as a parameter directly, as in the following example:

Look at the second argument, which is a higher-order-functions with two parameters that need to be invoked in the Rycylerview item and click Invoke how it looks, see:

And now create the adapter

coutriesItemClick is the function we passed to CountryAdapter as a parameter to invoke


Delicious yet 🤑🤑🤑 . We removed all the boilerplate code to create the click listener, and there was no need to determine which view was called when the item clicked.

Recyclerview adapter with multiclick areas

Pass higher-order-functions in when we have one or two areas in the item to assign the click event. But what if there were more. Of course, do not pass all of them (maximum of 3 parameters – Read the Clean code and you’ll know why)

See how to handle these types of situations effectively.

It was delicious and sweet (hihi). Now you can enjoy as much as you like. Why must we twist?

Scoped Functions

If you’ve been using Kotlin for a while, you already know scope functions like let , also , apply , etc. – take a look at the benefits of using them.

Basically, these functions do the same thing – executing a block of code on an object. What is different is how this object becomes available inside the block and what is the result of the whole expression. Let’s take a look at some of them

let

let is a scope functions in which variables declared inside an expression cannot be used externally. let takes the object it’s called as a parameter and returns the result of the lambda expression. Take a look at its usage:

But when you do not use let, you will have to declare a new variable and repeat its name whenever used:

Scopen functions can make code look shorter and easier to read. Moreover it can check null:

No more scenes like that:

run

  • Similar to let , run also returns the last statement
  • But unlike let , run does not support the keyword it

apply

apply can change the properties of the entity where we apply the keyword

with

with again is a keyword that works much like apply to accessing the properties of the entity to which the keyword is applied

Different scope functions return results:

  • apply , also returns an object
  • let , run , with returns a lambda expression

Choose which one to use, it depends on how you want to handle your code. But the use of them is really convenient If you want to learn more about scope functions, please refer to the article of Phuc Hammer offline.

The article was translated from the Medium page and has a bit more of its ugly. There is nothing wrong to expect everyone to comment. 😍😍😍

Share the news now

Source : Viblo