What if you could skip all those annoying null checks and write neat, safe code in just one line?
Why Let function with safe calls in Kotlin? - Purpose & Use Cases
Imagine you have a phone book with some missing phone numbers. You want to call a friend only if their number is listed. Without a smart way, you have to check if the number exists every time before calling.
Manually checking if a value is null before using it makes your code long and messy. It's easy to forget a check and cause errors, or write repeated code that's hard to read and maintain.
The let function combined with safe calls (?.) lets you run code only when the value is not null. This keeps your code clean, safe, and easy to understand.
if (name != null) {
println(name.length)
}name?.let {
println(it.length)
}This lets you safely work with optional values in a smooth, readable way without cluttering your code with checks.
When loading user data from a database, you can safely display the user's email only if it exists, avoiding crashes or awkward error messages.
Manual null checks make code bulky and error-prone.
let with safe calls runs code only when values exist.
This leads to cleaner, safer, and easier-to-read Kotlin code.
Practice
nullableVar?.let { println(it) } do?Solution
Step 1: Understand safe call operator usage
The?.operator checks ifnullableVaris not null before proceeding.Step 2: Analyze the
Ifletfunction behaviornullableVaris not null,letexecutes the lambda, printing the value.Final Answer:
Prints the value ofnullableVaronly if it is not null. -> Option AQuick Check:
?.letruns code only if not null [OK]
- Thinking it prints null when variable is null
- Assuming it throws an exception on null
- Confusing let with always running code
name using let?Solution
Step 1: Identify safe call usage
To avoid null errors,?.letmust be used, not just.let.Step 2: Check lambda syntax correctness
The lambda must be enclosed in braces{ }and useitto refer to the value.Final Answer:
name?.let { println(it) } -> Option AQuick Check:
Safe call + let with braces = correct syntax [OK]
- Omitting the safe call operator (?.)
- Missing braces around lambda
- Using variable name inside lambda instead of 'it'
val number: Int? = 5
number?.let { println(it * 2) }Solution
Step 1: Check if number is null
The variablenumberholds 5, which is not null, so?.letruns.Step 2: Calculate inside let block
The lambda multipliesit(5) by 2 and prints 10.Final Answer:
10 -> Option BQuick Check:
Non-null value runs let, prints doubled value [OK]
- Expecting no output if value is not null
- Confusing printed value with original variable
- Thinking it prints null when variable is non-null
val text: String? = null
text.let { println(it.length) }Solution
Step 1: Check safe call usage
The code usestext.letwithout?., so it runs even iftextis null.Step 2: Understand null dereference
Inside the lambda,it.lengthis called on null, causing a NullPointerException.Final Answer:
It throws NullPointerException because safe call is missing. -> Option CQuick Check:
Missing ?. causes null error [OK]
- Assuming let skips null without safe call
- Thinking null acts like empty string
- Ignoring possibility of NullPointerException
val names: List<String>? = listOf("Anna", "Bob", null, "Cara"), which Kotlin code safely prints only non-null names using let with safe calls?Solution
Step 1: Handle nullable list safely
Usenames?.forEachto avoid errors ifnamesis null.Step 2: Filter out null elements inside the list
Inside the loop, useit?.letto print only non-null names.Final Answer:
names?.forEach { it?.let { println(it) } } -> Option DQuick Check:
Safe calls on list and elements print only non-null [OK]
- Not using safe call on list causing errors
- Printing null elements without filtering
- Using let without safe call on nullable elements
Ready to prove it?
Pass the test and this pattern moves unknown
