Re: For-loop expression?
From: Michael S (a_at_b.c)
Date: 02/24/04
- Next message: Jonas Knaus: "Software-Studies in India"
- Previous message: .: "Re: history of .NET Framework and its languages (ILAsm, C#)"
- In reply to: Jon Skeet [C# MVP]: "Re: For-loop expression?"
- Next in thread: Jon Skeet [C# MVP]: "Re: For-loop expression?"
- Reply: Jon Skeet [C# MVP]: "Re: For-loop expression?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 24 Feb 2004 10:56:38 +0100
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1aa529652f063dae98a1fa@msnews.microsoft.com...
> Michael S <a@b.c> wrote:
> > I would like to continue on this subject by taking it from another
angle.
> >
> > The overhead of having calculations in the for statement or calculating
an
> > invariant result only once, may be tiny. But there is also a matter of
> > understandability and maintainability.
>
> Absolutely, but...
>
> > If you calculate the invarant result before the loop people maintaining
your
> > code wiill immediately understand that the value is invariant. If you do
the
> > calculation inside the for-statement you are signaling that the value
may
> > very well vary. The coder must check whether the result is invariant or
not.
>
> I don't think that's actually very likely - it's certainly not
> something I've come across often. The length of an actual array never
> changes, so you'd have to change which array you were using in order
> for the length to change. A collection's size may change, but in that
> case you almost always have to use a different construct anyway to
> avoid problems.
>
> If the upper bound of the loop *could* change, I'd probably highlight
> that with a comment instead.
>
> > This is not really an issue on something as typical like taking Length -
1
> > on a list. It's quite obvious. But I always try to write code that is
> > instantly obvious at first glance. Hence it is my recommendation to
never
> > evaluate invariant expressions inside for- or while-loops.
>
> However, I find it's more instantly obvious what
>
> for (int i=0; i < array.Length; i++)
> {
> ...
> }
>
> means than
>
> int n = array.Length;
> for (int i=0; i < n; i++)
> {
> ...
> }
>
> So while we agree that readability is very important, we certainly
> *disagree* on which is the more readable form :)
>
> > This is also why I try to use foreach whenever a can parse a vector
without
> > using an index.
>
> Agreed on that front.
>
For simple statements I also agree. Especially for calculating typical
things like i < arr.Lengthor i < myColumn.Count. But for more complex
calculations I move them outside the loop.
In the end, performance should be the least factor as the overhead is so
slight. It's more important to choose one coding style and stick to it for
all code in on project, one development-team or company.
It's when you interchange coding-styles you confuse programmers.
- Michael S
- Next message: Jonas Knaus: "Software-Studies in India"
- Previous message: .: "Re: history of .NET Framework and its languages (ILAsm, C#)"
- In reply to: Jon Skeet [C# MVP]: "Re: For-loop expression?"
- Next in thread: Jon Skeet [C# MVP]: "Re: For-loop expression?"
- Reply: Jon Skeet [C# MVP]: "Re: For-loop expression?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|