Ok so I probably shouldn't care enough, it's such a small, tiny thing, it almost doesn't even deserve a blog post but dammit! I keep se

Why you shouldn't default to positional parameters

submited by
Style Pass
2024-10-16 10:30:06

Ok so I probably shouldn't care enough, it's such a small, tiny thing, it almost doesn't even deserve a blog post but dammit! I keep seeing positional parameters used everywhere and I feel I have to make a full position on why there is no good reason to use positional parameters beyond maybe 2.

Positional parameters are inherently less readable. They can be acceptable for one or two values, but anything beyond that starts becoming a cognitive burden.

The second version is not only more readable but also self-documenting. Anyone reading the code can immediately understand what each parameter represents without needing to look up the function definition.

When using positional parameters, adding new ones is always a challenge. You could add it in whatever index you think is the most semantically appropriate place. This will likely break your API, which means you now need to refactor all usages. So clearly not an option in most cases.

So you add it at the end instead. And so the position ends up meaning nothing other than the order in which params where added.

Leave a Comment