Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Why do people do this?

Name: Anonymous 2012-07-04 4:24

Why write this

int foo() {
    int rc;

    rc = bar();
    if( rc < 0 ) {
        report_error();
        return -1;
    }

    return 0;
}



if this is much clearer to read?

int foo() {
    int rc;

    rc = bar();
    if( rc >= 0 )
        return 0;

    report_error();
    return -1;
}

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-07-04 4:35

return bar() < 0 ? report_error() && -1 : 0;

But in the case of multiple possible error returns, it's usually better to keep the main flow the "correct" one and error flows out of the way, like this:

int foo() {
 if(...)
  return -1;
 ...
 if(...)
  return -2;
 ...
 if(...)
  return -3;
 ...
 ...
 return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List