Discover how a tiny keyword can make your Kotlin code cleaner and faster to write!
Why It keyword for single parameter in Kotlin? - Purpose & Use Cases
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.
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.
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.
list.map { number -> number * 2 }list.map { it * 2 }You can write concise and readable code for simple single-parameter functions without extra naming.
When processing a list of prices to apply a discount, you can quickly write prices.map { it * 0.9 } instead of naming each price.
Manually naming single parameters is repetitive and verbose.
it keyword simplifies single-parameter lambdas.
Code becomes shorter, cleaner, and easier to understand.