Why do I not see stricter scoping more often?

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2010-06-18T05:41:37Z Indexed on 2010/06/18 5:43 UTC
Read the original article Hit count: 278

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about java