develooper Front page | perl.perl5.porters | Postings from June 2008

Re: [perl #56150] return return

Thread Previous | Thread Next
From:
Mark Mielke
Date:
June 23, 2008 10:41
Subject:
Re: [perl #56150] return return
Message ID:
485FE029.3080906@mark.mielke.cc
Ben Morrow wrote:
> I disagree. IMHO
>
>     sub foo {
>         my ($this) = @_;
>
>         exists $this->{foo} and return $this->{foo};
>
>         ...expensive calculations...
>
>         return $this->{foo} = ...;
>     }
>
> is clearer in at least two important ways.
>   

I find it amusing that you like to see "return" to clearly express 
intent to a maintainer, but don't mind using:

    exists $this->{foo} and return $this->{foo};

The "and" in this context to replace:

    if (exists $this->{foo}) { return $this->{foo}; }

Is very much a Perl specific thing. It's all about style and what you 
are comfortable with. You are comfortable with something different. In 
my Perl style guide that I provide to my people, I suggest that "or" is 
allowed but "and" as you have used it is not.

As for your code being clearer in two important ways - it is clearer to 
*you* in two important ways. For myself, I find "return ... if" or "... 
and return" to be un-clear. After all, unless one looks closely, one 
might miss the return altogether. I prefer the one subroutine, one 
return as a good policy, with many smaller subroutines over a few big 
subroutines.

With that, I'll leave this thread. I think my intent has been 
recognized. This is a style issue. There is no right answer.

Cheers,
mark

-- 
Mark Mielke <mark@mielke.cc>


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About