Avoid Passing In Too Many Parameters To Your Functions
Topic | Source |
---|---|
๐งน Clean Code | Clean Code - Udemy |
Use as few parameters as possible. Consider using objects to reduce the number of parameters.
In many languages, like JavaScript, you pass in the parameters of your functions without explicitly adding the parameter name. This makes it difficult to understand the parameters, the more a function has.
So, this log('message')
is straightforward to grasp, but this transformData(1, 'userName', false
is very difficult because you don't have any clue what the parameters do and in what order you have to pass them.
One way to make this easier, is by using objects as parameters. This way, you can pass one parameter and map the corresponding values, making them much easier to read.
class User {
constructor(userData) {
this.name = userData.name;
this.age = userData.age;
this.email = userData.email;
}
}
const user = new User({ name: 'Max', email: 'max@test.com', age: 31 });