Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Thoughts on "one function, flexible arguments"?

by davido (Cardinal)
on Aug 29, 2019 at 07:15 UTC ( [id://11105221]=note: print w/replies, xml ) Need Help??


in reply to Thoughts on "one function, flexible arguments"?

While at the outset it would seem that this adheres to Postel's law ("Be liberal in what you accept, and conservative in what you send.", paraphrased.), it's a bit of a path toward madness. The more alternatives you allow, the more you are agreeing to support all those alternatives forever, for some values of forever. You are also adding complexity to your code, making param unpacking more difficult, particularly in special cases, and are painting yourself into a corner where the calling alternatives may prevent you from doing something with the args that makes perfect sense in a specific case, but differs from the myriad of ways all of the other methods in your library work.

I suggest either requiring a hashref, or permitting either a hashref or key/value pairs. Don't go providing coercing of array-refs into hash-refs, or scalar refs, or code-refs (executed to produce the args)... unless you have a good reason for providing one of those, and in that case don't provide all the other ways. It's really an overzealous application of the perceived spirit of Postel's law, but isn't founded in the actual intent.

Also if you do find yourself doing this often, consider putting the args unpacking into a subroutine or method that can be called for every sub with conforming arg handling.

If you really need to provide significantly different ways to pass args, consider writing one version of the method, and then creating wrapper methods that provide the alternate calling styles:

sub my_func { my $args = ref($_[0]) eq 'HASH' ? shift : {@_}; # do the thing } sub my_func_positional { return my_func({ message => shift(), (@_ ? (newline => shift) : ()), }); }

There is a module, Params::Smart which will handle both positional and key/value args, but the time I used it I kind of wish I had just created two subs to call instead.


Dave

Replies are listed 'Best First'.
Re^2: Thoughts on "one function, flexible arguments"?
by roboticus (Chancellor) on Aug 29, 2019 at 13:05 UTC

    davido:

    I just wanted to chime in on Postel's law. As I see it, much of the difficulty of the WWW comes from allowing the browser to ignore malformed HTML without complaint. I agree that the browser could try to make a good attempt at rendering broken HTML, but it should *never* do it silently. The problems weren't that the browser tried to render garbage, but that (a) it would do so without complaint*, and (b) that it would try far too hard** to make it sensible.

    If web developers had to deal with the alert box every time a page load had an error, they'd either fix the problem because they got sick of seeing it, or they'd be forced to fix it when the people signing their paychecks saw it and said "WTF!?".

    Notes:
    * Back in the day, you'd get an alert box, but it had a checkbox at the bottom that said something to the effect of "I never want to see these warnings again", and people would click that and then never again see the alert box. It lead to a culture of "Meh, it works well enough, I guess I'm done" and crap coding. It contributed to the perception that JavaScript is a toy language in a toy environment, and lead to a bunch of slackadaisical front end developers.
    ** If we had to ensure that we provided correct HTML, there would be far less effort involved in "bug-compatible" HTML rendering tweaks on varied browsers. They could try to do something sensible so that developers could make changes to their pages and see what would happen, knowing that the errors would get fixed before release, so it wouldn't really matter if the table was rendered entirely in italics or whatever.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11105221]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found