Re: Programming style...
- From: Mark Roddy <markr@xxxxxxxxxxxxxx>
- Date: Tue, 31 May 2005 07:50:26 -0400
David J. Craig wrote:
Here is a freebie for those trying to do error checking and avoiding goto's.
K&R say the only legal 'DO ONCE' is done using the for command.
for( ; ; )
{
break; // If OE or ??? warps this, it should be four lines with this line indented.
}
You can then do tests and do a 'continue' or a 'break' as needed to exit or restart the loop. You will forget the terminating break especially when beginning, but it is legal and works. You can add a __finally to handle terminating cleanup, but I just usually initialize all locals and return variables before the 'for' block. Then a test and the appropriate cleanup can be done to free memory, close handles, etc. after exiting the block or before returning.
If you can't code it clean and easy to follow, you need to go back to school or find something other than a programming job. No one will live forever, much less stay in the same job, so be nice to those who follow and write maintainable code. You can add comments to the 'for' loop to tell anyone looking that this is a 'do once' block and not to get confused.
"Robert Marquardt" <marquardt@xxxxxxxxxxxxx> wrote in message news:%23TSRLUaZFHA.3732@xxxxxxxxxxxxxxxxxxxxxxx
Wan-Hi wrote:
2) error checking:
SetupDiGetClassDevs and many other functions return a specific value if they fail. so should i check for failure although such case is impossible? e.g. SetupDiGetClassDevs with GUID_DEVINTERFACE_VOLUME should never fail. i ask because useless failure checks make the code unreadable.
The error checks only make the code unreadable because MS prefers a *really* bad style. Excessive return and goto is about as bad as it can get. A clean if else style and a proper indentation solves that.
or:
do { error = stuff();
if (error) {
...
break;
} error = morestuff;
if (error) {
...
break;
}
} while(0);or the try/finally/leave stuff, although it has overhead.
The for and while one pass models break down (pun intended) over the semantics of 'break'.
I vote for single/entrance single/exit coding style every time.
--
===================== Mark Roddy DDK MVP Windows 2003/XP/2000 Consulting Hollis Technology Solutions 603-321-1032 www.hollistech.com .
- Follow-Ups:
- Re: Programming style...
- From: David J. Craig
- Re: Programming style...
- From: David J. Craig
- Re: Programming style...
- References:
- Programming style...
- From: Wan-Hi
- Re: Programming style...
- From: Robert Marquardt
- Re: Programming style...
- From: David J. Craig
- Programming style...
- Prev by Date: Re: Programming style...
- Next by Date: Re: Correct group for WDM development??
- Previous by thread: Re: Programming style...
- Next by thread: Re: Programming style...
- Index(es):
Relevant Pages
|