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

Pages: 1-

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;
}

Name: Anonymous 2012-07-04 7:10

>>1
Neither seems particularly more or less readable than the other.  The latter, however, would probably operate best under the assumption the frequency of error is high and success is the rarity.

Name: tokiko 2012-07-04 7:10

>>2
please email me cudder

please

Name: Anonymous 2012-07-04 11:47

not using this:


return ( x == a ) ? i :
       ( x == b ) ? j :
       ( x == c ) ? k :
       l;

Name: Anonymous 2012-07-04 11:57

int foo()
U MENA public static int foo() ??

Name: Anonymous 2012-07-04 15:38

>>5
You should use EXPERT REVERSE TERNARIES instead.

return !(x == a)? !(x == b)? !(x == c)?
        def : xeqc : xeqb : xeqa;

Name: bampu pantsu 2012-07-06 4:55

bampu pantsu

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