views:

132

answers:

4

I've found myself limiting scope fairly often. I find it makes code much clearer, and allows me to reuse variables much more easily. This is especially handy in C where variables must be declared at the start of a new scope.

Here is an example of what I mean.

{
        int h = 0;
        foreach (var item in photos)
        {
            buffer = t.NewRow();
            h = item.IndexOf("\\x\\");
            buffer["name"] = item.Substring(h, item.Length - h);
            t.Rows.Add(buffer);
        }
}

With this example, I've limited the scope of h, without initializing it in every iteration.

But I don't see many other developers doing this very often. Why is that? Is there a downside to doing this?

+5  A: 

Well, in that case you're assigning a new value in every iteration anyway, without using the "old" value - so I'd use:

foreach (var item in photos)
{
    buffer = t.NewRow();
    int h = item.IndexOf("\\x\\");
    buffer["name"] = item.Substring(h, item.Length - h);
    t.Rows.Add(buffer);
}

I find that if I do that as aggressively as I can (within reason, of course) I don't have much in the way of a scoping problem - and if I do, it probably means the method is too long and I should refactor it anyway.

I dare say it can be helpful in old-style C - but when I'm writing C#, I see no reason to do things which are useful in C but don't improve my C# :)

Jon Skeet
+3  A: 

Most develpoers are pretty careless about scoping. I've worked with developers who scope everything as high as possible to avoid having to allocate "extra" variables. This is usually due to people thinking that thye are saving memory because thye don't understand stack variables. Nevertheless, you are doing the right thing, although the savings that you are getting by initializing the variable outside of the foreach loop is minimal and I probably wouldn't bother.

Jeff Hornby
+1 for recognizing that there is a minimal saving for not having to redeclare h each iteration
Tim Bender
+3  A: 

Even if you're using a version of C that still requires variables to be declared before statements in a block, the following will have no ill performance effects in a non-debug build:

    foreach (var item in photos)
    {
        int h = 0;
        buffer = t.NewRow();
        h = item.IndexOf("\\x\\");
        buffer["name"] = item.Substring(h, item.Length - h);
        t.Rows.Add(buffer);
    }

The compiler will recognize that the 'initialization' of h isn't actually used, so it won't bother doing it (even on the first iteration through the loop). The compiler also won't bother to even reallocate h (chance are it would simply be a register).

However if h were an object type in C++ that had a constructor/destructor that performed work that the compiler could not optimize away, it might pay to hoist the declaration of h out of the loop.

Michael Burr
+2  A: 

The example is bad, as others have pointed.

Otherwise, scoping is not used because everywhere it makes sense - writing a function makes even more sense.

ima