Named parameters in method calls

Discussion in 'Perl Misc' started by fishfry, Feb 6, 2004.

  1. fishfry

    fishfry Guest

    Is the named parameter style of calling methods the officially right way
    to do it these days? Or is there some debate? I'm talking about

    my $foo = new Foo(FISH => 'tuna', DRESSING => 'mayo');

    as opposed to Foo('tuna', 'mayo');

    In other words if you do it the old (positional) way, is that considered
    either hopelessly old-fashioned and/or bad coding practice?
    fishfry, Feb 6, 2004
    #1
    1. Advertising

  2. Also sprach fishfry:

    > Is the named parameter style of calling methods the officially right way
    > to do it these days? Or is there some debate? I'm talking about
    >
    > my $foo = new Foo(FISH => 'tuna', DRESSING => 'mayo');
    >
    > as opposed to Foo('tuna', 'mayo');
    >
    > In other words if you do it the old (positional) way, is that considered
    > either hopelessly old-fashioned and/or bad coding practice?


    No, neither of that. Named parameters come in handy to smoothen the
    interface a little when you have many arguments. Often I use them for
    additional configuration options only:

    my $obj = Class->new( $required_arg, -opt1 => 1, -opt2 => 2 );

    The implementation of Class::new() would do something like that:

    sub Class::new {
    my $self = shift;
    my $arg = shift;

    croak "Required argument missing" if not $arg;

    my %opts = @_;
    ...
    }

    This allows the user to just write

    Class->new( $arg );

    instead of the longer

    Class->new( arg => $arg );

    when he doesn't want to set any additional options.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Feb 6, 2004
    #2
    1. Advertising

  3. fishfry

    Guest

    fishfry <> wrote:
    > Is the named parameter style of calling methods the officially right way
    > to do it these days? Or is there some debate? I'm talking about
    >
    > my $foo = new Foo(FISH => 'tuna', DRESSING => 'mayo');
    >
    > as opposed to Foo('tuna', 'mayo');
    >
    > In other words if you do it the old (positional) way, is that considered
    > either hopelessly old-fashioned and/or bad coding practice?


    As far as I can tell, the named parameter method is common only when there
    are lots of optional parameters. If all or most parameters are mandatory,
    positional seems to be the common method.

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    , Feb 9, 2004
    #3
  4. fishfry

    gnari Guest

    <> wrote in message
    news:20040209122834.865$...
    > fishfry <> wrote:
    > > Is the named parameter style of calling methods the officially right way
    > > to do it these days? Or is there some debate? I'm talking about
    > >
    > > my $foo = new Foo(FISH => 'tuna', DRESSING => 'mayo');
    > >

    > As far as I can tell, the named parameter method is common only when there
    > are lots of optional parameters. If all or most parameters are mandatory,
    > positional seems to be the common method.


    it is also useful if you foresee that the methods might be extended later.
    this avoids breaking code that does not know about new parameters.
    that is why this is popular in all sorts of modules.

    gnari
    gnari, Feb 9, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    863
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    752
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Magnus Lyck?
    Replies:
    5
    Views:
    385
    Magnus Lyck?
    Dec 2, 2003
  4. aidy
    Replies:
    1
    Views:
    113
    Farrel Lifson
    Sep 13, 2006
  5. Intransition
    Replies:
    3
    Views:
    123
    Intransition
    Jun 18, 2010
Loading...

Share This Page