foreach ... else statement

Daniel Keep daniel.keep.lists at gmail.com
Tue Jan 6 03:59:57 PST 2009



Brian wrote:
> [snip]
> 
> neither of those seem necessary in that example, whats wrong with this:
> void somefunction() {
> 	do_stuff();
> 	if (error)
> 		handle_error();
> 	else
> 		do_more_stuff();
> }

A few things I can think of:

1. You're mixing error handling code with the other code.  This breaks 
the flow and makes the logic harder to follow.

2. If you've got more than one point in the function where you need to 
do error handling, then you're going to be duplicating code; error 
handlers are very rarely just a single function call.

3. The point was that execution "fell out" after the handler.  If you 
want to use conditionals, you can end up with really deeply indented 
code which is a bugger to read.

It's rather ironic, but one thing that struck me going from Visual Basic 
to Python was that VB had much nicer error handling; instead of having 
error handling all over the place, it was all localised to the end of 
the function.  This is why I absolutely adore scope statements.

Of course, you're technically correct; in that specific example, you 
could do it either way.  :)

   -- Daniel



More information about the Digitalmars-d mailing list