Defining Programming Standards   
for Professional Programmers 
  

         

Home

Contents

1: Standards

2: Psychological Factors

3: General Principles

4: Commenting

5: Naming

6: Code Layout

7: File Layout

8: Language Usage

9: Data Usage

10: Programming Usage

11: Implementing Standards

A: Example Standard

B: References

C: Glossary

Syque

About

Share this page:

Google
C Style
syque.com
Web

 

 

Books and
more at:

USA:

In association with amazon.com

UK:

In Association with Amazon.co.uk

Canada:

In Association with amazon.ca

 

 

CHAPTER 8 : Language Usage

PART 4 : USAGE

CHAPTER 8 : Language Usage
8.1 General principles of language usage
8.2 Using expressions
8.3 Using 'if'
8.4 Using 'while'
8.5 Using 'for'
8.6 Using 'do'
8.7 Using 'switch'
8.8 Using 'goto'
8.9 Using 'continue' and 'break'
8.10 Using 'return'
8.11 Using functions
8.12 Using '#define'
8.13 Conditional compilation
8.14 Other preprocessor commands
8.15 Summary

<--Prev page | Next page -->

 

8.10  Using 'return'

Where do you return from a function? A simple philosophy is to return as soon as you are ready, for whatever reason:

 

WriteStatus = PutDetailBlock( pEmpRec->EmpDetail );
if ( WriteStatus == WRITE_OK )
    return ( WRITE_OK );

if ( WriteStatus == DISC_FULL )
{
    printf( "Disc full, can't write details\n" );
    return ( WRITE_ERR ):
}
...

 

This reduces levels of nesting, but may be difficult to follow and trace, and can result in many return's throughout the code. A simple way of counteracting this is to ban all return's, except at end of the function:

 

WriteStatus = PutBlock( pEmpRec->EmpDetail );
if ( WriteStatus == WRITE_OK )
    RetVal = WRITE_OK;
else
{
    if ( WriteStatus == DISC_FULL )
    {
        printf( "Disc full, can't write details\n" );
         RetVal = WRITE_ERR;
    }
    else...
...
}
...
return ( RetVal );

-------------------------------------------------------

This can help tracing (e.g. put breakpoint just before the end) and ensures use of any final 'cleanup code' (eg. closing files), but it can also make code more unreadable, as it forces more use of nesting and awkward paths through the code.

A compromise between the above two examples is to allow error returns, but to insist that there is only one non-error return point which must be at the end of the function:

 

WriteStatus = PutBlock( pEmpRec->EmpDetail );
if ( WriteStatus != WRITE_OK )
{
    if ( WriteStatus == DISC_FULL )
    {
        printf( "Disc full, can't write details\n" );
        return ( WRITE_ERR ):
    }
...
}
...
return ( WRITE_OK );

 

Return type

If the function is declared to return a given type, then a value of that type should always be returned. The only time a plain return; may be used is when the function is declared to return a type void.

Returning from 'main'

In several environments, values returned from programs are available and may be tested in a shell script. Typically a non-zero return indicates that the program did not successfully complete its purpose. It is worth doing this, even if no other error codes are returned.

 

<--Prev page | Next page -->

 

 

  © Syque 1995-2010

Massive Content -- Maximum Speed