Why do I not see stricter scoping more often?
- by Ben
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?