[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: expressions vs. statements

On Thu, Dec 20, Pixel wrote:
> kragen@pobox.com (Kragen Sitaker) writes:
> [...]
> > The consequence is that your function returns some value, even if you
> > didn't really intend for it to do so.  This means that whenever you
> > change the function, you need to either maintain the same return value
> > (possibly leading to wordier code, inefficiency, or in the worst case,
> > the inability to make code correct) or verify that nobody is using the
> > return value.
> I confirm this is a problem in Perl. I have some functions that ends with:
> --------------------
>    if () {
>       ...
>    } else {
>       ...
>    }
> }
> --------------------
> where the if_then_else returns an error/warning value.
> When you want to add some code after it, you must check wether the return
> value was wanted and used by some caller. Adding debugging statement after the
> if_then_else may cause havoc!

You already know that a function or block "returns" the last statement
contained within it. So why would you ask Perl to do what you did not
want it to do?

FWIW, in that scenario, you would put the debugging print statement
as the first statement in each of the conditional blocks, then do
the rest of whatever was already there.

> > I don't think this issue is terribly important in language design, but
> > it's obvious to me that the right answer is that most blocks of
> > sequential statements shouldn't return a meaningful value.
> agreed for while/for. My only problem is with if_then_else which return value
> is useful. Alas in perl you can't write "return if () ..." to disambiguate.


sub foo {
        return unless @_;       # don't do this function if called without args
                                # returns 'undef'
        return if $bar; # returns the special 'undef' value
        return $baz if $quux;
        $boom; # returns $boom by default

Not exercising control of the program or idiom does not mean the
language or idiom is either problematic or insufficient for a purpose.

Michael Fischer                         7.5 million years to run
michael@visv.net                        printf "%d", 0x2a;
                                                -- deep thought