logo       

Re: AW: Function parameter passing (was: Re: limit the list): msg#00028

Subject: Re: AW: Function parameter passing (was: Re: limit the list)
On Wed, 20 Nov 2002, Pense, Joachim wrote:

> Bart Lateur [mailto:bart.lateur@xxxxxxxxxx] wrote:
> (Mittwoch, 20. November 2002 11:43)
> 
> >On Wed, 20 Nov 2002 04:10:02 -0600, Steven Lembark wrote:
> >
> >>sub commify
> >>{
> >>    my ( $max, $sep, $end ) = ( shift, shift, shift );
> >     ...
> >>}
> >
> >Wow! Hold it! Am I the only one who finds this absurd? More than one
> >shift on the same array in one single expressing, sounds like bad style
> >to me. Comments?
> 
> In one of my programs, this would be
> 
> sub commify {
>     my $max = shift;
>     my $sep = shift;
>     my $end = shift;
> 
>     ...
> }
> 
> better or even worse in your view?

Better, but only if it makes sense to conditionally take values off the 
calling stack, like this contrived example:

sub foo {
        my $type = shift;
        my $arg1 = shift;

        my $arg2 = 0;

        if ($type == 'APIv2') {
                $arg2 = shift;
        }

        return $arg1 + $arg2;
}

A variation of this would be if, depending on the type, you might want 
different names for a given element on the calling stack:

sub foo {
        my $type = shift;
        my $arg1 = shift;

        if ($type == 'APIv2') {
                my $adder = shift;
                return $arg1 + $adder
        } elsif ($type == 'APIv3') {
                my $subtractor = shift;
                return $arg1 - $subtractor;
        }

        return $arg1;
}


You might also do something like this to illustrate that depending on the
value of the first argument, we may not even care about any of the rest.

sub foo {
        my $type = shift;

        if ($type == 'APIv3') {
                carp "APIv3 unsupported\n";
                return;
        }

        # perhaps do computationally intensive or transactional stuff here
        # ..

        my $arg1 = shift;
        my $arg2 = 0;

        if ($type == 'APIv2') {
                $arg2 = shift;
        }

        return $arg1 + $arg2;
}

Granted, there's no performance benefit, but since certain variables
aren't introduced until after the conditional return case, it definitively
shows that those values aren't needed unless we pass the condition.

I think that using 'shift' as a little faucet that gives you stuff as you
need it is rather elegant.

--Jeremy

-- 

Jeremy Impson
Sr. Associate Network Engineer
Investigative Research & Marketing
Lockheed Martin Systems Integration
email: jeremy.impson@xxxxxxxx
phone: 607-751-5618
fax:   607-751-6025





<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
audio.irate.dev...    yellowdog.gener...    ietf.ips/2002-0...    xfree86.fonts/2...    busybox/2003-07...    emacs.jdee/2004...    linux.mandrake....    hardware.microc...    user-groups.lin...    science.analysi...    version-control...    db.filemaker.de...    cluster.openmos...    mail.eyebrowse....    text.xml.xerces...    kde.devel.kwrit...    finance.moneyda...    gcc.regression/...    network.routing...    os.freebsd.deve...    recreation.radi...    qnx.openqnx.dev...    python.xml/2002...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe