The Psychology of Quality and More |
CHAPTER 8 : Language Usage
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 );
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 ); ------------------------------------------------------- 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 );
Return typeIf 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.
|
Site Menu |
Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive | |
And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact | |
Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate | |
You can buy books here |
And the big |