OOZOU
Get in Touch
Back to Blog

Make things easier on Android with “let” and “apply”

“let” and “apply” are very useful functions in Kotlin. It could change the way you write your code from what you did in Java

July 21, 2017
3 min read
Make things easier on Android with “let” and “apply”

“let” and “apply” are very useful functions in Kotlin. It could change the way you write your code from what you did in Java. In this article, I would like to share how to make your code easier with “let” and “apply”. I will assume that you have some basic knowledge about Kotlin.

What can “let” do?

I would like to point out two main usages of “let”

  1. Scoping Function: “let” could be used to make your code self-contain in the “let” block and keep the logic separate. The variable in front of “let” could be referred to as it. Look at the code below as an example.
File("dummy.txt").let {
  print(it.toString())
}
//File "dummy.txt" is not visible here

As you can see that the File(“dummy.txt”) will only be visible in the “let” block. 2. Check against null: In Java, to prevent our lovely NullPointerException we have to repeatedly check for null in our code. Fortunately, we could make things easier with “let”. We can make the “let” block execute only when the variable is not null. Let’s see an example.

Suppose we have an object like this user -(hold)-> info -(hold)-> emailBoth user and info could be null. This is how we would do this in Java.

if(user != null) {
  Info info = user.getInfo();
  if (info != null) {
    String email = info.getEmail();
    if(email != null) {
        sendEmailToUser(email)
    }
  }
}

And in Kotlin :)

user?.info?.email?.let? {
    sendEmailToUser(it)
}

As you can see that it’s much shorter and cleaner in Kotlin.

ow to use “apply”?

“apply” may help you write the code in a slightly different way

“apply” can be used as an extension of all types of variables. It will return the object with the thing in the “apply” block apply to the object (it works according to its name). Let’s see things in action to get a clearer picture.

Here are a couple of examples:

  1. In Java, when we try to set up RecyclerView. We might do something like this.
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new YourRecyclerViewAdapter(this));
recyclerView.setHasFixedSize(true)

With “apply” in Kotlin, we could make it this way.

recyclerView.apply {
  layoutManager = LinearLayoutManager(this@YourActivity)
  setHasFixedSize(true)
  adapter = YourRecyclerViewAdapter(this@YourActivity)
}
  1. Creating an object through setter in Java might require something like
User user = new User();
user.setFirstName = "John";
user.setLastName = "Doe";
user.setGender = "Male";
user.setProfileUrl = "...";
user.setPhoneNumber = "...";

With “apply” we could make things slightly better

User user = User().apply {
  firstName = "John"
  lastName = "Doe"
  gender = "male"
  profileUrl = "..."
  phoneNumber = "..."
}

Inside “apply” block we can refer to “properties” or “methods” of the variable that used “apply” without having to refer to that variable again.

Conclusion

In my opinion “let” does make the code shorter and cleaner, especially the ability to check against null. Since as an Android developer we all might have to write many lines of code to check for null values in Java to prevent NullPointerException and “let” does solve that pain. For “apply”, I think it helps to avoid repeatedly referencing the same variable and makes the code a bit easier to read.

Reference

http://beust.com/weblog/2015/10/30/exploring-the-kotlin-standard-library/

Related Articles

How Can I Design a Software: A Beginners Guide for Business Owners

How Can I Design a Software: A Beginners Guide for Business Owners

Designing software is an exciting yet complex process that involves creativity, logical thinking, and problem-solving skills.

Read More →
The Future of Edge Computing: What Businesses Need to Know

The Future of Edge Computing: What Businesses Need to Know

The digital landscape is evolving rapidly, and businesses must adapt to keep up with rising demands for speed, efficiency, and real-time processing.

Read More →
The Future of Retail: AI, AR, and Digital Transformation Trends

The Future of Retail: AI, AR, and Digital Transformation Trends

Retail is evolving at an unprecedented pace, driven by AI (Artificial Intelligence), AR (Augmented Reality), and digital transformation technologies, revolutionizing the retail industry. Traditional brick-and-mortar stores are no longer just about selling products—they are becoming smart, interactiv

Read More →

Have a Project in Mind?

Let's discuss how we can help bring your ideas to life with our expertise in web and mobile development.

Start a Conversationhello@oozou.com
OOZOU Logo

Bangkok · Singapore · Hong Kong

X
Facebook
Instagram
LinkedIn

Services

  • Web Development
  • AI Agents & Generative AI
  • Mobile App Development
  • Data Analytics & Engineering
  • UI/UX & Product Design
  • Digital Transformation

Company

  • About Us
  • Careers
  • Blog
  • Whitepapers
  • Case Studies
  • Today I Learned
  • Contact

More

  • Industries
  • Get an Estimate
  • Partner Network
© 2026 OOZOU. All rights reserved.
Privacy PolicyCode of ConductABAC Policy