Kotlin Scope Functions: let, apply, run, also — Don’t hesitate, let’s use it

Nam Bui Van
4 min readNov 30, 2020

--

In the process of learning Kotlin, or reading some projects using Kotlin in Android. Surely we will feel unfamiliar with some “new symbols” compared to Java that we already know.
Such as: let, apply, run, with, also …
I myself have been confused by them and wondered how they help in my programming process. ❓

Let’s start looking for the answers! 🔥

Intro

First, let’s go through some examples of use or not them!

“let, apply, run, also..” which we mentioned above is called Scope Functions.

Scope Functions is in the Kotlin standard library whose sole purpose is to execute a block of code within the context of an object. When you call such a function on an object with a lambda expression provided, it forms a temporary scope. In this scope, you can access the object without its name.

For the sake of simplicity, these functions will help us perform some operations on an object and return the desired result in a particular block.

Why Use?

Pause to discuss what Scope Functions is, and how to use it. Let’s tackle the effects of Scope Functions or why we should use it (while if written code normally, everything will be OK).

Scope Functions: Can’t do, that we think to do:

  • Don’t introduce any new technical capabilities.
  • Don’t make our code run any faster.
  • Can only be used under certain circumstances.

Scope Functions: Can do:

  • Use it whenever you want, in a function, in a class
  • Make our code more concise and readable, maintain.
  • Pack the code, avoid “leak out”.
  • Somehow remove the declaration of val or var during coding.

Distinguish and Use Case

→ The question is which scope functions should we choose, and how to distinguish them? Because they actually look quite similar in nature.

Don’t worry too much, because you can actually choose from many Scope Functions to solve your problem and still have the same results.
The choice mainly depends on your intent and the consistency of use in your project, even your preferences.

It is like working with the “HTTP APIs” we can use Retrofit, Volley, or Basic HTTP Connection. All of them can produce the desired results.

To better understand the usage of Scope Functions, as well as to distinguish them. We will be interested in two things.

  • Context object: this or it
  • The return value: this or lambda result

Analyzing the above example we can see:

  • Context object of T.apply{} is this (this: Intent) as a lambda receive. We can access the members of the receiver object, making the code shorter. Here is to set some parameters for the intent
  • T.apply{} return value is this (it’s context object)

→ If want call its functions or assign properties for an object, we can use scope function with context object is this

  • Context object of T.let{} is it (it: Intent) as a lambda argument. If the argument name is not specified, the object is accessed by the implicit default name it (in this case is Intent). We can not call functions or assign properties of object directly in lambda that must go through it.
  • T.let{} return the lambda result. We can assign any value.

→ So, having the context object as it is better when the object is mostly used as an argument in function calls. it is also better if you use multiple variables in the code block.

Below is a summary of some Scope Functions be used of the most commonly in Android.

In terms of use cases, it’s all relative. The things Scope Function A does, we can completely do the same with Scope Function B.
Of course, you must choose the Scope Function to suit the context you have

We can also use multiple Scope Functions together, for the best results.

Note

Using Scope Functions can make our code more concise and readable, maintain, but if we overuse them, use irregularities in the team, it can have “side effects”, making the code even more confusing and ambiguous.
Don’t hesitate but be careful!

This article introduces to Scope Functions, why to use it + some notes. If you want to learn more about each function, please refer to here

Hopefully, this article will help you to understand more about how to use Scope functions in real situations.

Let me know your thoughts on this article.

Thanks!!! Happy Coding! 😇

Reference

--

--

Nam Bui Van
Nam Bui Van

Written by Nam Bui Van

I ‘m a Mobile Developer. #AndroidDeveloper #Kotlin #Flutter

No responses yet