4 ms·
"What you need instead is a single universal function that does in fact accept a boatload of parameters, but that is never meant to be called directly. You then
by arethuza 9y ago
"What you need instead is a single universal function that does in fact accept a boatload of parameters, but that is never meant to be called directly. You then supplement it with a host of simpler one-liners that fill in most of the parameters with defaults and forward just the select few."
Or if you have a language that can have supply defaults for parameters set them to the default case?
- huhtenberg 9y agoI am not aware of any language that supports overriding _some_ of the defaults from the function declaration, e.g. foo(1,,,'abc',,) Meaning that if you need to override a default for, say, 2nd argument, you will also need to explicitly provide all arguments that follow.
- lccarrasco 9y agoPython lets you override only those that you want using Named Parameters as explained in this answer: https://stackoverflow.com/questions/25374814/multiple-default-values-for-function-arguments-python https://stackoverflow.com/questions/25374814/multiple-defaul...
- conradfr 9y agoAs an alternative to named parameters I've always wanted a "default" keyword for function calling. function bar($word1, $word2='loves', $word3='Elixir') { return sprintf('%s %s %s', $word1, $word2, $word3); } $foo = bar('Hacker News', default, 'PHP'); But I guess functions with too many arguments are frowned upon and it would encourage it.
- wolco 9y agoIf you want to exclude the parameters after 'abc' you can. Better yet in php 7 using the splat operator ... foo(...$args) Allows you to send in any number of args.
- arethuza 9y agoI was thinking of languages (e.g. python and c#) where you can specify default values for named parameters.
- fiddlerwoaroof 9y agoIn some situations (destructuring, at least) JavaScript will use the default value when it encounters undefined.
- stiGGG 9y agoSwift can do this.