Re: Multiple return statements in a function.
| From: | George Larson | Date: | Thu, 23 Apr 2009 12:38:56 +0000 |
| Subject: | Re: Multiple return statements in a function. | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-291858@lists.php.net to get a copy of this message | ||
On Thu, Apr 23, 2009 at 8:25 AM, Per Jessen <per@computer.org> wrote:
> Peter van der Does wrote:
>
>> I tend to put my return value in a variable and at the end of the
>> function I have 1 return statement.
>> I have seen others doing returns in the middle of the function.
>>
>> Example how I do it:
>> function check($a) {
>> $return='';
>> if ( is_array( $a ) ) {
>> $return='Array';
>> } else {
>> $return='Not Array';
>> }
>> return $return;
>> }
>>
>> Example of the other method:
>> function check($a) {
>>
>> if ( is_array( $a ) ) {
>> return ('Array');
>> } else {
>> return ('Not Array');
>> }
>> }
>>
>> What is your take? And is there any benefit to either method?
>
> It's only about style and coding logic. In essence, all the return does
> is pop the previous IP off the stack and adjust the stack pointer. It
> doesn't matter where you do that.
>
>
> /Per
>
>
> --
> Per Jessen, Zürich (16.2°C)
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
That's an interesting subject that I've never considered.
I usually return immediately. For me, it makes the code easier to
read. I work with a number of other coders here and, if the result
isn't returned then I have to keep reading through the code to make
sure nothing else is done with it. However, when I see the 'return'
then I know we're done there.
That said, I often see questionable coding practices in use at work.
I follow this list (and try to read books about the technologies I
use) because I intend to develop good practices for myself. That in
mind, if anybody feels strongly about doing it the other way, I'd be
interested in understanding its benefits.