With Delphi are you more likely to re-use temporary variables than with other languages?

delphi, variables

Solution

I hardly ever reuse variables. I hate to say never, but it is close to never.

Here is why:

- Small methods (It's good practice to keep methods and property-getters/setters as concise as possible).

- When only one thing is done, no need to reuse variables

- The var section is always on the screen.

- The compiler reuses the storage as necessary, so reuse is only a lazy coder crutch with no performance improvements.

- Newer versions of Delphi have CTRL+SHIFT+V to declare a variable if I am feeling lazy.

- Reusing variables makes debugging more difficult - more time and effort is spent on maintenance then development (for any serious application) so always do things to make maintenance easier, even if it makes development a little harder.

- Prefer user defined types, so a Account Balance is a specific type, not just a Currency. This means variables are less reusable anyway.

- For loop variables (a common reused variable) are used less now that we can use for in and skip the iterator all together.

- My variables have descriptive names, so it would not make sense to use them out of context.

Generally speaking, I like having all the variables at the top for the same reason I like having an interface section on my units. It is kind of like having an abstract on a paper - give me a general idea of what is going on without having to read the whole paper. Delphi could benefit from having the ability to declare variables at "inner scope" like within a for loop or other begin / end blocks, but I don't know how much that would distract from the cleanliness and readability of Delphi code.

Problem

Since Delphi makes you go all the way up to the var section of a method to declare a local variable, do you find yourself breaking "Curly's Law" (re-using variables) more often than you did in college?(unless of course, you programmed Pascal in college). If so, what do you do to break yourself of that habit, especially in functions where you need to get and/or set large numbers of properties. Is there a threshold where it is acceptable to declare `TempInt : Integer` and `TempStr : String`. (Do you use an 'e' in `Temp` sometimes and not other times?)

Original source