"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
It is always a good practise to re-use as many variables as possible in a program
"I'm not back." - Bill Harding, Twister
Originally posted by Ashish Chopra:
So in the second case, you are definitely wasting heap memory, and if "sum" were to be an object, you;d end up creating a large number of redundant objects.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Jim Yingst:
by declaring a variable as clase as possible to where it's actually used, you make it easier to understand that code.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Jim Yingst:
[Rusty]: The recursive version executes far slower, so it might affect that in a negative way.
What recursive version? I don't see any recursion in either code sample.
If you think one of those bits of code executes slower than the other, I suggest testing it. I believe you will find they both perform the same. Even "if utmost runtime performance is the only concern" - there is no performance penalty for declaring a local variable inside a loop. It performs the same as if you had declared it outside.
There is a tiny chance that someone may one day make a compiler which behaves differently, but every time this subject has come up in the past and someone studied the byte codes, we confirmed that the compiler generates the same byte codes (or effectively the same byte codes) in each case. I would say it's very unlikely that will change anytime soon. But feel free to test this yourself to confirm.
[b][Rusty]: The second is just plain bad programming, regardless of efficiency. Why re-declare the same variable that is used over and over? It is pointless and illogical, don't do it.
I strongly disagree with both of these statements. These are local variables, therefore on the stack rather than the heap. If you study the bytecode generated you will find that there's no reallocation occurring - the compiler just keeps reusing the same memory space each time through the loop. So it costs you nothing to redeclare the variable. More important are two other effects: (1) by eclaring a variable as clase as possible to where it's actually used, you make it easier to understand that code. And (2) by restricting the scope of the variable to the minimum required (declare the variable as late as possible, inside blocks if possible), you typically would decrease the chance of errors which might occur from accidentally re-using a variable name somewhere else. Many of us have loop variables named i, or Iterators named it or iter - if you have two loops one after another, and in the second loop you type i when you should have said i2 (or "iter" when you should have typed "iter2", you get a bug. Such bugs are easily avoided (or promptly detected) if each loop variable is only defined within the loop it's needed for.
I suggest reading Effective Java, Item 29: Minimize the scope of local variables. Also the rest of the book is strongly recommended, for other reasons.
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
You said that before, but you didn't say why. So how about saying why this time?Originally posted by Rusty Shackleford:
The second code example is bad programming practice, period.
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
"I'm not back." - Bill Harding, Twister
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|