0
0
Kotlinprogramming~3 mins

Why It keyword for single parameter in Kotlin? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a tiny keyword can make your Kotlin code cleaner and faster to write!

The Scenario

Imagine you want to write a small function that takes one input and does something with it, like doubling a number. Without a shortcut, you have to name the input every time, even if you only use it once.

The Problem

Manually naming the single parameter every time can feel repetitive and clutter your code. It slows you down and makes your code longer than needed, especially for simple operations.

The Solution

Kotlin's it keyword automatically represents the single parameter in a lambda, so you don't have to name it. This keeps your code short, clean, and easy to read.

Before vs After
Before
list.map { number -> number * 2 }
After
list.map { it * 2 }
What It Enables

You can write concise and readable code for simple single-parameter functions without extra naming.

Real Life Example

When processing a list of prices to apply a discount, you can quickly write prices.map { it * 0.9 } instead of naming each price.

Key Takeaways

Manually naming single parameters is repetitive and verbose.

it keyword simplifies single-parameter lambdas.

Code becomes shorter, cleaner, and easier to understand.