6 ms·
Parameter Hell
- NigelTufnel 13y agoAfter six years of programming in Python I've almost forgot that there are languages that don't have named function parameters. I remember the horror of working with WINAPI. There are WINAPI functions with 10 or so arguments and I was using Delphi which as far as I remember had no support for dictionary literals, let alone named parameters.
- jon_kuperman 13y agohaha a friend just showed me named parameters in python! /jealous
- krapp 13y agoI've used to 'options as array' pattern in PHP plenty of times. While it's probably the closest thing to named parameters you can get in that language, it can get kind of ugly and clumsy when you have to validate it. function something(array $options, $defaults=array('foo'=>1,'bar'=>2)){ ...(array_intersect_key or something)... } Actual named parameters would work so much better. And Wordpress 'solves' this in the plugins API by suggesting using extract(), which converts an array into variables. Which is even worse.
- jon_kuperman 13y agoAbsolutely, they would be the ideal answer but a lot of times this gets the job done better than just having 10 parameters to your function.
- mlntn 13y agoThere is currently an RFC for named parameters in PHP 5.6. The spec looks good and I'm hoping it gets approved. https://wiki.php.net/rfc/named_params https://wiki.php.net/rfc/named_params
- jon_kuperman 13y agoHadn't seen that, thanks for the share.
- RossM 13y agoThere's been some recent discussion about this[0]. The RFC owner reckons it won't be ready in time for 5.6 (more like 5.7). [0]: http://markmail.org/message/wgdsntbsnojloxqd http://markmail.org/message/wgdsntbsnojloxqd
- chaghalibaghali 13y agoThe problem with this approach is that you basically lose all IDE support and static code checking - it's a good solution in extreme cases, but I prefer not to use it as the default.
- deleted 13y ago[deleted]
- mdxn 13y agoBack in highschool, I wrote an Uno clone for a final project in Java. I ended up encountering a similar issue for when I was defining constructors for Card objects. At some point, I had to cast null as an Object to rid of some ambiguity. Still pretty embarrassed about it.
- omervk 13y agohttp://en.wikipedia.org/wiki/Builder_pattern http://en.wikipedia.org/wiki/Builder_pattern