Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^4: How should named paramater keys be named?

by DeadPoet (Scribe)
on Jul 02, 2011 at 22:15 UTC ( #912502=note: print w/ replies, xml ) Need Help??


in reply to Re^3: How should named paramater keys be named?
in thread How should named paramater keys be named?

Once again it depends on the complexity of the parameters that are being supplied. One, two, three parameters exposed to the public maybe not an issue, and agreeably named parameters probably not needed.

However, if the interface in to the whatever you are calling requires a more complicated set up, then I would say that named parameters are in order. Now with that said, when using named parameters they better be consistent throughout the entire program. Moreover, I would go so far as to say they should also be lower case and clearly documented. Personally I see named parameters having value, but the value is only obtained where exposed to the public. Code that is on the back end that users cannot call directly why would they be needed--you already have established programmatic control over the parameters.

Yes, I use named parameters outside of a constructor -- my previous restrictions still apply ( internal NO, user called YES ). An example would be when I want to enable a local debug. Keep in mind that the routine already has other values being passed.

sub foo { my $o = { q{debug} => 0, ## debug 0|1. q{db} => SQL_DB, ## sql db. q{host} => SQL_SERVER, ## sql server. q{port} => SQL_PORT, ## sql server port. q{user} => SQL_USER, ## sql user. q{pass} => SQL_PASS, ## sql user pass. @_ }; ## do your safety checks... my $dsn = q{DBI:mysql:database=} . $o->{'db'} . q{;host=} . $o->{'host'} . q{;port=} . $o->{'port'}; my $objSQL = eval { DBI->connect( $dsn, $o->{'user'}, $o->{'pass'}, { 'PrintError' => 0, 'RaiseError' => 0 } ); }; if ( ( $@ ) || ( ! defined $objSQL ) ) { ## maybe we only want to show the error if in debug?? print STDERR $DBI::errstr if ( $o->{'debug'} ); return undef; } DBI->install_driver( q{mysql} ); return $objSQL; }

Now, I did not run check the code above, and it is ONLY an example of what can be done. Also, I do not expect everyone to agree with the code. So, not only does this allow a user to call the function, method, subroutine, etc... but it provides simple named parameters by which it can be called. More importantly, it allows the end user to call it without having to care about the order and if the parameter is not specified then it is defaulted. I understand that one cannot always default to a meaningful value, but this is just an example. As far as my style goes, I do not like the leading hyphen, nor do I like the mixed case, such as the DBI Connect implements. But that is just my style. Honestly, I prefer the shorter single case format.

Finally, named parameters have their place and can provide value. The trick is to balance how and when they are used. Is there a cost? Yes. Can they provide a more robust public interface? Yes. Can they be overused? You better believe it.


Comment on Re^4: How should named paramater keys be named?
Download Code
Re^5: How should named paramater keys be named?
by BrowserUk (Pope) on Jul 03, 2011 at 11:58 UTC

    The problem with your arguments is that they are based upon emotion rather than logic. Your preferences rather than clear reasoning.

    1. You say "I see named parameters having value, but the value is only obtained where exposed to the public.", but then go on to say "I prefer the shorter single case format.".

      But the public interface is for other people to call, not you, so surely you should cater to their preferences, not yours?

      But of course you couldn't possibly cater to all their preferences, no more than they are all likely to cater for your preferences.

      So the problem remains that even if you could guess what the names of the arguments might be (highly unlikely), for some new module you are using, you'd still have to look up the documentation to discover the casing of those names. And as soon as you have to look the up, any "easier to learn" justifiction for named arguments goes right out the window.

      And once you've looked it up, the ordering of parameters are far easier to remember than the names and casing, so the "easier to remember" justifiction crashes and burns also.

      All that leaves is the nebulous old standby, the "easier to read" justifiction. But that doesn't stand up to logical analysis either.

      Take your 'foo' function, as poor an example as it is. The crux of your defence is that this;

      my $dbh = foo( q{debug} => 0, q{db} => SQL_DB, q{host} => SQL_SERVER, q{port} => SQL_PORT, q{user} => SQL_USER, q{pass} => SQL_PASS, );

      Is somehow easier to read than:

      my $dbh = foo( SQL_SERVER, SQL_PORT, SQL_DB, SQL_USER, SQL_PASS, DEBUG + );

      Note the reordering of the parameters into their 'natural ordering'. Ie. A server responds on a port, contains the DB which can be accessed by a user with an appropriate password, who may want to debug this code.

      And that just ain't true. Regardless of whether you are passing constants (symbolic or inlined) or variables, the names of the variables or symbolic constants, or the values of the inlined constants makes the naming of the parameters just redundant noise. (And ignoring the quoting properties of the fat comma by using 'fat quoting' is just more redundant noise.)

      Far better and simpler would be:

      my $dbh = foo( SQL_DSN, SQL_USER, SQL_PASS, DEBUG );

      There is no point in breaking out the DSN sub-fields as there is nothing you can do to validate them. Can you check the name of the server or database or user? Tell the guy he has supplied the wrong password? I guess you could check that the supplied port number is in the range 0..65535, but then your users might be using a named pipe connection anyway.

      And defaulting them individually makes no sense at all. You've said this is a public API, so what are you going to use as the default for the server name (for all your users!): MySqlServer? The DB name: myDB? The user name:root? Password?

      For a more thorough examination of the futility of breaking apart DBI DSNs see Re^5: Avoiding compound data in software and system design.

      Even port number, unless this is an open DB, security suggests that it makes a lot of sense to not use the default port number. No point in making it easy for the hackers.

    2. Your example is very badly chosen.

      Other than a lip-service comment, you've completely ignored a major part of my argument, that of parameter checking.

      Named parameters hugely complicate the process of parameter checking. So much so, that it almost guarantees that your token comment will remain that way ever more.

    3. Your example is badly implemented.

      In addition to all the overhead created by the parsing and validating of named parameters, (re)constructing your defaults hash every time you call the function is almost criminal.

    4. The fact that your example is contrived for the purposes of supporting your emotive argument, suggests that you didn't have a real example somewhere in your codebase. And that further suggests that this is an idea you subscribe to, not a practice that you er... practice.

      And ideas born of purely theoretical, high ideology, are rarely practical in practice. They become burdens to ingenuity and productivity for no payback.

      I was reading an article the other day about a bunch of people and companies trying to come up with standards for cloud computing. The crux of piece is that as yet, no one really knows what "cloud computing" is, never mind the best way to do it. It is therefore way too premature to be considering draughting a standard, much less to impose it upon anyone.

      The best (I would say:only good) standards are the de-facto standards that get codified, cleaned up and adopted. You have a free-for-all period where everyone tries to do things whatever way seems to work, and slowly, over a period of practical usage, certain practices fall out as being easier to use, or more effective or more efficient. Ie. More practical. That's when standardisation should be considered.

      IMO the biggest problem with CPAN, is that the APIs for many of the modules there where defined on the basis of the author sitting down and inventing them, rather than them falling out of writing an application that needs to do what the module does. Which means that applications end up having to be structured to fit the exposed API, rather than the API fitting the way you would want to write the application. (There are also many good exceptions to that!)

    There *is* utility in using named parameters for sub/methods that (usefully) take large numbers of optional parameters. And that mostly means constructors.

    There are examples of non-constructor functions/methods that could have many optional parameters--fetching a url (get) is one such with all the possibilities of headers, agent ids, cookies, redirects, time-outs et al. But even then there are good ways and bad ways of defining the API. Most of the options used by get() for any given application are unlikely to change for every fetch. So, whilst the run-time of a fetch is likely to always be dominated by the IO, it simply doesn't make sense to re-parse and re-validate those options for every individual fetch, so using the options to construct a user agent, and then have the $ua->get( $url ) as a simple method of the user agent is just good design.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I hear your arguments, and did state that this is just an example of what can be done. I also do not think I have ignored your argument of parameter checking, as I stated early on that there is no excuse for not checking parameters and return codes. I also stated that not always can a value be defaulted--this example just provided a way of showing setting a default value. Poorly chosen or not, this is a simple example to illustrate.

      I always enjoy conversing with you on matters, as I enjoy the different point of view. But let us remember, Examine what is being said, not who is saying.... This was just a quick example, and all I have said is that named parameters can have value. If it is your stance that they can have no value, then I respectfully disagree.

      There is a product called PowerKeeper that implements a programmatic interface much as you have described-- foo( opt1, opt2, opt3, optn ). Let's just look at the AddSystem API for a moment:

      AddSystem System,['][ReservedParameter],[NewSystemName], NetworkAddress,[PrimaryEmail],PlatformName, [ChangeFreq],[ReleaseDuration],[SystemAutoFl], [\"][FunctionalAccount][\"], [FunctionalAcctCredentials],[ChangeTime], [PasswordRule],[PropagateFlag],[PortNumber], [EnablePassword],[AlternateIP],[Description], [DomainFunctionalAccount],[BoksServerOS],[LineDef], [Timeout],[DomainName],[OracleType],[OracleSIDSN], [CheckFl],[ResetFl],[ReleaseChgFl],[NetBIOSName], [MaxReleaseDuration],[PropagateToAliasesFlag], [SecretPassUsedFl],[ParentSystem] [,LDAPServer,ConnectionEncryption, \"SearchBase\",[AccountNameAttributeName], [PasswordAttributeName],PasswordReadability, [LDAPPort],[MultiValuedPasswordFl],[StoreClearTextFl], [StoreCryptFl],[StoreMD5Fl],[StoreSHAFl], [StoreSMD5Fl],[StoreSSHAFl]][']

      There are 47 options, and none named. Moreover, all parameters are position dependent. So by your logic, it is easier to remember that the 'CheckFl' belongs in field 26? I think this is the spirit of the PBP when it comes to when to use named parameters.

      To exclude possibility is to limit oneself, to blindly accept is to subjugate ones mind, to explore is to learn. --Poet

        If it is your stance that they can have no value, ...

        To exclude possibility is to limit oneself, to blindly accept is to subjugate ones mind, to explore is to learn.

        I'll just point out that I said: "There *is* utility in using named parameters for sub/methods that (usefully) take large numbers of optional parameters..

        There is a product called PowerKeeper that implements a ... the AddSystem API ... There are 47 options

        And that "47 options" is as close to the perfect fit for "take large numbers of optional parameters." as I have ever seen.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-21 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (108 votes), past polls