C style

Mike Shannon mikes at 3comvax.UUCP
Tue Oct 29 11:44:26 AEST 1985


Sorry Scott, I don't buy it.  Your example isn't good enough:
Scott's example:
>     checkreturn = OK;
>     for (i=0; i<100 && checkreturn==OK; i++) {
>         checkreturn = function1(x);
> 	if (checkreturn == OK) {
> 	    checkreturn = function2(x);
> 	    if (checkreturn == OK) {
> 	        checkreturn = function3(x);
> 		if (checkreturn == OK) {
> 		    checkreturn = function4(x);
> 		    if (checkreturn == OK) {
> 		        checkreturn = function5(x);
> 			if (checkreturn == OK) {
> 			    result[i] = function6(x);
> 			}
> 		        else {
> 		            result[i] = 0;
> 		    	}
> 		    }
> 		}
> 	    }
> 	}
>     }
> 
-------
Scott's example done the RIGHT (i.e. my :-))  way:
	for(i = 0; i < 100; i++) {
		if(f1(x) == OK &&
		   f2(x) == OK &&
		   f3(x) == OK &&
		   f4(x) == OK &&
		   f5(x) == OK	  ) {
			result[i] = 0;
		}
		else {
			result[i] = function6(x);
		}
	}
--------
	In general, I don't like humongous if statements like the above, but
this one is pretty easy to read.
	I didn't do anything with checkreturn, because Scott's code didn't
use it for anything, either.
	In the more usual case where you need to take different error actions
depending on several successive function calls, I venture to state that
It Should Be Done This Way:
	if(f1() == ERROR) {
		take some sort of error action.
	}
	else if(f2() == ERROR) {
		fix up any side effects of f1,side effects are bad practice too
		take some other sort of error action
	}
	else if(f3() == ERROR) {
		fix up side effects of f1 and f2
		take another error action
	}
	else {
		collect two hundred dollars, you may pass GO
	}
-- 
			Michael Shannon {ihnp4,hplabs}!oliveb!3comvax!mikes



More information about the Comp.lang.c mailing list