Re: For-loop expression?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Michael S (a_at_b.c)
Date: 02/24/04


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



Relevant Pages

  • Re: Function Vs Expressions
    ... But, also I listen to increase the speed, store calculations results. ... Normally, i don't store the calculations results to save memory, but I ... There is overhead associated with the use of a domain aggregate ... When used singularly in code or a form/report expression this overhead ...
    (microsoft.public.access.formscoding)
  • Re: GetRows
    ... Even with the overhead ... I was planning to do some benchmarks with the array idea ... array could possibly result in faster calculations than you'd get on ... Is the temp table slow from an end user's point of view? ...
    (comp.databases.ms-access)
  • Re: hardware crypto and SSL?
    ... overhead of session establishment, because that's where pubkey ... calculations occur. ...
    (FreeBSD-Security)
  • Re: For-loop expression?
    ... > The overhead of having calculations in the for statement or calculating an ... > code wiill immediately understand that the value is invariant. ... The length of an actual array never ... *disagree* on which is the more readable form:) ...
    (microsoft.public.dotnet.languages.csharp)