Structures as status variables.

Cary Bates bates%falcon.dnet at fermat.mayo.edu
Wed May 18 07:28:50 AEST 1988



To take advantage of what little type checking is done by our C
compiler, some of our programmers have started writing their functions
so that they return structures rather than integer constants.  This
makes it impossible to check for a status that is not returned by the
function without getting a compile time error.  For example, the
completion status for the function 'foo' can have three possible
values (SUCCESS,WRONG_VALUE,& BAD_PARAMETER). These are the only three
values that are valid to check for. Using a structure for the
completion status, the function is as follows: 

| #include stdio
| 
| typedef  struct
| {
| 	unsigned success       : 1;
| 	unsigned wrong_value   : 1;
| 	unsigned bad_parameter : 1;
| }  foo_status;
| #define INIT_FOO_STATUS {FALSE,FALSE,FALSE}
| 
| /*****************************************************************************\
| *                                                                             *
| * This is a very simple function that accepts one parameter 'i' by reference. *
| *  The function completes SUCCESSfully if a value of 1 is passed in for 'i'.  *
| *  The function returns WRONG_VALUE if a other than 1 is passed in for 'i'.   *
| *  The function returns BAD_PARAMETER if no address was passed in for 'i'.    *
| *                                                                             *
| \*****************************************************************************/
| foo_status foo( int *i )
| {
|      foo_status return_status = INIT_FOO_STATUS;
|      if ( i == 0 ) 
|      {
|           return_status.bad_parameter = TRUE;
|           return( return_status );
|      }
| 
|      if ( *i != 1 )
|      {
|           return_status.wrong_value = TRUE;
|           return( return_status );
|      }
| 
|      return_status.success = TRUE;
|      return( return_status );
| }
| 
| 
| main()
| {
|    foo_status status;
|    int i;
| 
|    i = 2;
|    status = foo( &i );
| 
|    if ( !status.success )
|    {
| 	if ( status.wrong_value )
|                 printf("Wrong value\n");
|         if ( status.bad_parameter )
|                 printf("Bad parameter\n");
|    }
| 
|    status = foo( 0 );
| 
|    if ( !status.success )
|    {
| 	if ( status.wrong_value )
|                 printf("Wrong value\n");
|         if ( status.bad_parameter )
|                 printf("Bad parameter\n");
|    }
| }


Such a method may cause more work to delcare and check status
variables.  Assuming that coding is only 20% of the development cycle
(40% design and another 40% testing/debug), will such a concept reduce
debugging time enough to make up for the extra coding time?  Is this
easy enough to understand so that maintenance is not made more
difficult?  Does this violate any C rules or hurt efficiency? 


Cary Bates

bates%falcon.dnet%fermat at bru.mayo.edu



More information about the Comp.lang.c mailing list