Why should local variable x be declared in inner scope.

If I have code such as:
int a;
while (...){
a = somethingThatCalcsValues();
doSomethingWith(a);
}

R# suggests moving int a; into the inner scope.

Doesn't this re-declaration each time through the loop have an adverse
affect on performance? Even if it is negligible, I am curious as to why R#
thinks it is better to do it this way.

Thanks


3 comments
Comment actions Permalink

"Richard Haber" <richard.haber@gmail.com> wrote in message
news:ethphk$r06$1@is.intellij.net...

If I have code such as:
int a;
while (...){
a = somethingThatCalcsValues();
doSomethingWith(a);
}

>

R

  1. suggests moving int a; into the inner scope.

>

Doesn't this re-declaration each time through the loop have an adverse
affect on performance? Even if it is negligible, I am curious as to why
R# thinks it is better to do it this way.


Declaring a variable has no performance impact at all.

John


0
Comment actions Permalink

As John noticed, there is no any difference in performance with regard to
where you
declare the variable (if you care about performance, you have to think about
moving some executable code
outside the loop, and not a variable declaration). However, the code looks
(arguably) more clear when variables are declared as closer
to the places where they are used, so ReSharper suggests moving it inside
the loop.


Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


"Richard Haber" <richard.haber@gmail.com> wrote in message
news:ethphk$r06$1@is.intellij.net...

>> If I have code such as:
>> int a;
>> while (...){
>> a = somethingThatCalcsValues();
>> doSomethingWith(a);
>> }
>> R# suggests moving int a; into the inner scope.
>>
>> Doesn't this re-declaration each time through the loop have an
>> adverse affect on performance? Even if it is negligible, I am
>> curious as to why R# thinks it is better to do it this way.
>>

Declaring a variable has no performance impact at all.

John



0
Comment actions Permalink

No argument about it. Thanks for the clarifications

"Dmitry Shaporenkov" <dsha@jetbrains.com> wrote in message
news:c8a8945d11f28c93748cc8f6094@news.intellij.net...

As John noticed, there is no any difference in performance with regard to
where you
declare the variable (if you care about performance, you have to think
about moving some executable code
outside the loop, and not a variable declaration). However, the code looks
(arguably) more clear when variables are declared as closer to the places
where they are used, so ReSharper suggests moving it inside the loop.

>
>

Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>
>
>> "Richard Haber" <richard.haber@gmail.com> wrote in message
>> news:ethphk$r06$1@is.intellij.net...
>>
>>> If I have code such as:
>>> int a;
>>> while (...){
>>> a = somethingThatCalcsValues();
>>> doSomethingWith(a);
>>> }
>>> R# suggests moving int a; into the inner scope.
>>>
>>> Doesn't this re-declaration each time through the loop have an
>>> adverse affect on performance? Even if it is negligible, I am
>>> curious as to why R# thinks it is better to do it this way.
>>>
>> Declaring a variable has no performance impact at all.
>>
>> John
>>
>



0

Please sign in to leave a comment.