4 ms·
Some languages let you spell out the arguments upon calling a function. clamp(x:3, min:1, max:5) Oh look! No mental strain required to think about what the fu
by dyarosla 9y ago
Some languages let you spell out the arguments upon calling a function.
clamp(x:3, min:1, max:5)
Oh look! No mental strain required to think about what the function does nor what the order should be to minimize mistakes; it’s clear as day!
I wish more languages adopted the option to do this.
- dyarosla 9y agoMoreover, you could also allow for something like passing a structure of parameters. clamp({min:2, max:5, x:3}) ^parameters can be rearranged as you see fit There! Now the programmer can decide what’s the way they want to call the function that makes the most sense to them.
- dvlsg 9y agoIf you use named arguments, can't you typically rearrange the order? Don't get me wrong, I use destructured arguments in js and it works fine, but I thought named arguments in other languages solve that particular issue as well.
- joshvm 9y agoIn languages like Python where you can have keyword args (typically with defaults), these options must come after the first argument, e.g. clamp(x, min=0, max=inf). Then you can do what you like with the order. And often it's good to specify the keyword for your own sanity later, when you have no idea what the third argument to some obscure Numpy call is. Or if you're using something like matplotlib and there are so many kwargs that it's better to specify. Although in this case min/max probably shouldn't have defaults, I think clamp(min, x, max) would be a bit weird in Python?