Is it better to have one return declaration for a function?

When using functions, you often want to return different values ​​depending on the result of the function. Is there any preferred method for setting a single return or setting multiple returns in a management structure? Which of these two methods is preferred?

Several declaration declarations within management structures:

function doStuff( $input ) {

    if ( true === $input ) {
        return 'You are an A+ fellow.';
    }
    else {
        return 'Not too sure about you...' ; 
    }

}

A single return is declared, but introduced a new variable to save the result:

function doStuff( $input ) {

    if ( true === $input ) {
        $result = 'You are an A+ fellow.';
    }
    else {
        $result = 'Not too sure about you...' ; 
    }

    return $result;

}

Or is it just the case in each case?

EDIT: Wow, a lot of answers. I really understood that this could be reduced by removing another; it was more for example than anything :-).

, , " ". ! : .

+4
7

:

    function doStuff( $input ) {

        if ( true === $input ) {

    /*
If your program enter here is not necessary evaluate the else
statement (and/or other pieces of code, that could be later).  
Your function return at this point the main thread
 (or the point where the function was called).
    */

            return 'You are an A+ fellow.';
        }
        else {
            return 'Not too sure about you...' ; 
        }

    }

:

function doStuff( $input ) {

    if ( true === $input ) {
        $result = 'You are an A+ fellow.';
    }
    else {
        $result = 'Not too sure about you...' ; 
    }

    /*
        Your program must evaluate each piece of code above this point
     to return to the main thread (or the point where the function was called).
In this case is simple, but could be a lot of code to reach this point.
        */
            return $result;

        }

:

, , , , , .

0

, .

, PHP ( ). PHP , . . PHP, , , . , , .. .

. . . , function/class , . , . , .

function imaginary(array $array) {

    if(!$array) { // Evaluates to false on empty arrays
        return false;
    }

    // Do other things.

}

function imaginary(array $array) {

    if(!$array) { // Evaluates to false on empty arrays
        $value = false;
    }else{
        // Do other things to determine return value
        $value = true;
    }

    return $value;
}

. , (, , ).

. . , .

!

+1

-, . :

function doStuff( $input ) {
    if ( true === $input ) {
        return 'You are an A+ fellow.';
    }

    return 'Not too sure about you...';
}

.

0

, , , , return, , , . , ​​,

increment_by_five(){
    global $number;
    return $number+5;
    $number+=5;
}

, , .

increment_by_five(){
    global $number;
    $result= $number+5;
    $number+=5;
    return $result;
}

, , , else

function doStuff( $input ) {
if ( true === $input ) {
    return 'You are an A+ fellow.';
}
return 'Not too sure about you...' ; 
}

, , return.

0

, ... :

if ( true === $input ) {
    return 'You are an A+ fellow.';
}
else {
    return 'Not too sure about you...' ; 
}

:

if ( true === $input ) {
    return 'You are an A+ fellow.';
}
return 'Not too sure about you...' ; 

, , if.

0

, .

Looking at http://php.net/manual/en/function.return.php (php manual), there is no performance hit from the many return statements.

You can reorganize your code for the following:

function doStuff( $input ) {
    return $input === true ? 'You are an A+ fellow.' : 'Not too sure about you...';
}

Or even

function doStuff( $input ) {
    if ($input === false) {
        return 'Not too sure about you...';
    }

    return 'You are an A+ fellow.';
}
0
source

To reduce code complexity, the best thing to do is:

function doStuff( $input ) {

    if ( true === $input ) {
        return 'You are an A+ fellow.';
    }

    return 'Not too sure about you...' ;
}

This way it will produce the best result with a code quality analyzer such as a code coverage tool for phpUnit

-1
source

All Articles