Ideas regarding flow control and loops

Bruce Adams tortoise_74 at yeah.who.co.uk
Mon Nov 5 03:58:20 PST 2007


BCS Wrote:

> Reply to Bruce,
> 
> > Marco Aurélio Wrote:
> > 
> >> Charles D Hixson Wrote:
> >> 
> >>> No.  Finally should be the label on a block of code that will be
> >>> executed *WHATEVER* happens in the preceding loop, including the
> >>> raising of an exception.
> >>> 
> >> Hmm Yeah, now that I think about it, having it on that way would make
> >> it inconsistent with the try-catch-finally behavior... Maybe adding
> >> another keyword? or something like:
> >> 
> >> for(int i = 0; i < 30; i++)
> >> {
> >> if (something)
> >> break;
> >> } catch (break) {
> >> Foo();
> >> }
> > That's redundant. Its the same as
> > 
> > try {
> > for(int i = 0; i < 30; i++)
> > {
> > if (something)
> > throw breakException;
> > }
> > } catch (breakException) {
> > Foo();
> > }
> 
> Tell me that is a joke. If you don't see the problems with that then....
> 
> Do you have any idea how mush potential for overhead there is in that? The 
> other solution has one jump, that has a memory allocation (and a free at 
> some point) a bit of stack un winding, Maybe a RTTI work and who only known 
> what else. Plus it will (incorrectly) trigger any intervening scope(failure) 
> and sooner or later you will need to start fabricating types to keep track 
> of what loop the break is for.
> 
Fair point but the expense of exceptions depends on how they are implemented and what else is going on. You effectively have a form of stack unwinding when you leave a scope. An exception doesn't have to be allocated on the stack but yes its less efficient than a break. I try to avoid using breaks myself because they pollute the control flow too much. Likewise exceptions but they are not supposed to be used for control flow. I should know better. How about:
bool breakNow = false;
for(int i = 0; 
    i < 30 && breakNow == false; 
    i++)
{
   if (something)
       breakNow = true;
   ...
}
if (breakNow) 
{
   Foo();
}

It still doesn't justify a language enhancement as far as I can see.

> > Are you sure there's really a problem here? How about posting
> > something 'evil'. If no-one in the group can think of a good
> > refactoring
> > 
> > then you may have a case.  I suspect structured programming has been
> > around too long to benefit much from anything new. That said, foreach
> > was an awful long time coming so you may have a case.
> > 
> > Regards,
> > 
> > Bruce.
> > 
> 
 





More information about the Digitalmars-d mailing list