Should I declare variables as close as possible to

2019-06-20 08:20发布

ReSharper usually suggests me that, and I'm still looking for a good reason of why to do that.

The only thing that came to my mind is that declaring it closer to the scope it will be used, can avoid initializing it in some cases where it isn't necessary (because a condition, etc.)

Something related with that is the following:

int temp;
foreach (var x in collection) { 
    temp = x.GetValue();
    //Do something with temp
}

Is that really different than

foreach (var x in collection) {
    int temp = x.GetValue();
    //...
}

I mean, isn't the second code more expensive because it is allocating memory everytime? Or are both the same? Of course, after finished the loop, in the second code the garbage collector will take care about temp variable, but not in the first one...

7条回答
Anthone
2楼-- · 2019-06-20 08:24

I was always taught to declare your variables at the top of a function, class, etc. This makes it easier to read.

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-20 08:28

The difference is a matter of coding style and one of such dispute that different coding standards have completely opposite rules. The conflict is still strongest in the C++ world where the C language forced variables to be declared at the beginning of a scope and so old-timers (like myself) were well accustomed to "looking at the beginning of the function" to find variables.

The C# style that you most often see is that variables come into existence right at the point where they are needed. This style limits the existence of the variable and minimizes the chance that you could mean some other variable accidentally. I find it very easy to read.

In the modern C# era, putting the declaration of variables at their first point of use is most clearly beneficial when combined with the both loved and hated var feature. Using var just isn't that useful unless you use it with an assignment that allows the compiler and readers to infer the type of the variable. The var feature encourages declaration with first use.

Me, I love var, and so you can guess which coding style I prefer!

查看更多
欢心
4楼-- · 2019-06-20 08:40

The cost of the second example is negligible. The only difference is that in the first example, temp will be available outside the scope of the for loop, and thus it will exist longer than if you declared it inside the for loop.

If you don't need temp outside the for loop, it shouldn't be declared outside that loop. Like others have said, readability and style are more at play here than performance and memory.

查看更多
孤傲高冷的网名
5楼-- · 2019-06-20 08:40

There is no performance benefits, I believe, but more of a coding style. Its more C programming style to declare it all at the beginning of the scope. There is more details here: Scope of variables in C#

查看更多
一夜七次
6楼-- · 2019-06-20 08:45

I agree that if you init a variable inside the scope that it's being used then you're helping the gc out, but I think the real reason is more to do with code maintenance best practices. It's sort of a way of reducing cognitive load on you or another developer coming back to the code after months (or years) of not looking at a particular block. Sure, IDE's help you discover things, but you still have to do the "go to definition" dance.

查看更多
混吃等死
7楼-- · 2019-06-20 08:48

Declaring as close as possible to use is a readability decision. Your example doesn't display it, but in longer methods it's hard to sift through the code to find the temp variable.

It's also a refactoring advantage. Declaring closer to source leads to easier refactoring later.

查看更多
登录 后发表回答