foreach thoughts

Marco Leise Marco.Leise at gmx.de
Wed Jan 15 11:45:35 PST 2014


Am Tue, 14 Jan 2014 12:57:19 +0100
schrieb Artur Skawina <art.08.09 at gmail.com>:

> On 01/14/14 09:22, Manu wrote:
> > 2. A filter
> > 
> > The other thing is the ability to skip uninteresting elements. This is typically performed with the first line of the loop testing a condition, and then continue:
> > foreach(i, t; things)
> > {
> >   if(!t.isInteresting)
> >     continue;
> >   ...
> > }
> > 
> > I'm finding in practise that at least one of these seems to pop up on the vast majority of loops I'm writing. It produces a lot of visual noise, and I'm finding it's quite a distraction from the otherwise relative tidiness of my code.
> 
>   foreach(i, t; things) if (t.isInteresting)
>   {
>      ...
>   }
> 
> artur

That's what I use, too and is similar to:

with (Enum) switch (enum)
{
   ...
}

Occasionally I also use:

if (x) do {
   ...
} while (y);

It saves a level of indentation and a pair of {}.

-- 
Marco



More information about the Digitalmars-d mailing list