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

Re: [perl #56150] return return

Thread Previous | Thread Next
Mark Mielke
June 23, 2008 10:41
Re: [perl #56150] return return
Message ID:
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 

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


Mark Mielke <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About