Return from 1 point in function

Possible duplicate:
If a function has only one return statement?

Hello,

gcc 4.4.4 c89

Good programming practice will return from 1 point in a function.

I wrote a function below. However, I am returning from two possible points.

Is this a good style?

static int init_data(struct timeout_data_t *timeout_data)
{
    if(timeout_data == NULL) {
        fprintf(stderr, " [ %s ] [ %d ]\n",
            __func__, __LINE__);
        return FALSE;
    }

    /* Assign data */
    timeout_data->seconds = 3;
    timeout_data->func_ptr = timeout_cb;

    return TRUE;
}
+3
source share
10 answers

I had managers who lived and died using the 1st return policy for the sake of "readability", although in some cases it was more readable without it.

The bottom line is ... if the person who signs your salary says that you are only going to use 1 refund, use 1 refund. The best way to do this is

type myfunc(params) {
    type result = defaultValue;
    // actual function here, word for word
    // replace "return $1" with "result = $1"

    return result;
}

- . , , ZERO, , , "return" ( ) "result =", . , , , , , ?: -)

+4

, .

.

+11

. , .

, - , , , ( , ). - , ).

+7

C , / (, , ) . , . (, ), .

+2

, , . , , if/else . ( " ", , , . == .)

+2

"C", .

C ,

open file
process data
close file

, , , , , , .

++, , , ++

+2

, .

, , .

, , - , - . , , , .

:

  • , , .
  • , .
  • , , ( ).
  • .
+1

(10-15 ), :), , . .

. , , , .

+1

, .., , , . , , , " " " ", . , , , (, switch).

+1

(, , ) - . .

+1

Source: https://habr.com/ru/post/1752479/


All Articles