Kotlin Tip #4: Utilize Named and Default Parameters — 100 Kotlin Tips in 100 Days
Twitter | LinkedIn | YouTube | Instagram
Tip #3: Utilize Type Inference
In Kotlin, functions can have multiple parameters, and remembering the order of parameters or understanding their roles solely based on position can be challenging, especially with functions that accept several arguments or have parameters of the same type.
Named parameters address this issue by allowing the developer to specify the name of each parameter being passed to the function.
fun displayUserInfo(id: Int, name: String, email: String) {
println("ID: $id, Name: $name, Email: $email")
}
// Not using named parameters
displayUserInfo(1, "Raphael De Lio", "raphael@example.com")
// Using named parameters
displayUserInfo(id = 1, name = "Raphael De Lio", email = "raphael@example.com")
// In any order
displayUserInfo(name = "Raphael De Lio", email = "raphael@example.com", id = 1)
Default parameters, on the other hand, allow developers to specify default values for function parameters. These defaults are used when a corresponding argument is omitted in the function call.
fun greet(name: String, greeting: String = "Hello") {
println("$greeting, $name!")
}
// Calling the function without specifying the greeting
greet(name = "Raphael")
// Output: Hello, Raphael!
// Or with specifying the greeting
greet(name = "Raphael", greeting = "Olá")
// Output: Olá, Raphael!
This feature simplifies the function’s usage by allowing callers to skip specifying every argument, especially when most of the time, they would pass common values.
When combined, they also decrease the need for the Builder pattern, which is traditionally used in languages like Java to simplify the construction of objects that require numerous parameters, some of which may be optional.
Let's take a look at the traditional builder pattern in Java:
public class User {
private final String name;
private final int age;
private final String email;
private User(Builder builder) {
this.name = builder.name;
this.age = builder.age;
this.email = builder.email;
}
public static class Builder {
private String name;
private int age;
private String email;
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setAge(int age) {
this.age = age;
return this;
}
public Builder setEmail(String email) {
this.email = email;
return this;
}
public User build() {
return new User(this);
}
}
}
That can then be used as in:
User user = new User.Builder()
.setName("Raphael De Lio")
.setAge(30)
.setEmail("raphael.delio@example.com")
.build();
In Kotlin, the same goal could be reached with a much simpler implementation by leveraging default and named parameters:
data class User(
val name: String,
val age: Int = 0, // Assuming default age to 0 if not specified
val email: String = "" // Assuming default email to an empty string if not specified
)
That can then be used as in:
val user = User(
name = "Raphael De Lio",
age = 30,
email = "raphael.delio@example.com"
)
Named and default parameters in Kotlin improve the readability and flexibility of function calls. While each feature independently offers distinct advantages, combining them allows developers to write more expressive and intuitive code.
I hope you have enjoyed the fourth tip of our series! Don’t forget to subscribe and stay tuned for more Kotlin tips!
Stay curious!
Contribute
Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio
Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw
Follow Me on Social Media
Stay connected and dive deeper into the world of Kotlin with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.