views:

404

answers:

11

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?)

+5  A: 

Not really. As I make my methods really small the var section is not far away. As my method size has reduced a lot since university, I'd say I break it less often.

Stephan Eggermont
+1  A: 

I think delphi makes the exception with the overuse of temp variables. Most of the time when i'm creating a function/procedure where i know i will need loops or temp strings, first thing i do is to create a var i,j:integer; tmp:string; and add more as needed :)

TheBrain
I like that, I'd imagine C++ guys would scoff at the idea, especially since you're implicitly implying you use a lot of nested loops, but I bet you save a lot of time doing that.
Peter Turner
"Implicitly implying?" I should report you to the Department of Redundancy Department for that. ;-)
Mason Wheeler
+2  A: 

Declaring variables is very simple - some times they would get automatically created ('for' loop template), other times you can just use 'Declare Variable' refactoring (or 'Add Local Var' if you are using MMX - as you should).

gabr
+1  A: 

I probably would have found this to be a bigger problem if hadn't had CTRL-SHIFT-V as a shortcut to the VAR section. I'm not writing GIGANTIC methods here, but sometimes they get a little out of hand (and I can justify this of course) and it helps a lot. I'm not sure if that shortcut comes from cnTools or GExperts, but they're both pretty useful and I'd recommend them both.

Peter Turner
I have both add-ins installed - I'll give this a try later on. I didn't know about Ctrl-Shift-V. :-)
robsoft
+3  A: 

I don't tend to reuse local vars as a general safety rule. I do love the new "var" live template stuff in d2007+. Just type var[tab] and the helper pops up. Also check out Ctrl-Shift-D (others mentioned Ctrl-Shift-V for local vars) to declare a field.

MarkF
Aye, Ctrl Shift D is awesome, do you know which version that started in? It works in Delphi 2009, but in Delphi 7 it brings up my cnTools procedure list wizard, but that may be overriding it.
Peter Turner
I think it was added in D2007.
MarkF
+5  A: 

This is just a matter of discipline. Yes, Delphi would probably be better served by inline variable declaration, but that's not really a big deal. Just be sure to name your variables in a descriptive way, and then it will just feel awkward to use them incorrectly. And, as Stephan Eggermont said, if your methods are really getting that long, then that's a whole different code smell.

JosephStyons
Good advice, and Prism has that. The Anders who still works on Delphi (forgot his last name) mentioned that some of the Prism features may be coming to Native Delphi in the future (and nothing in Prism will be stripped to make it more like Pascal)
Peter Turner
@Peter: perhaps you mean Andreas Hausladen?
Argalatyr
NO, I think he's talking on Anders Ohlson...
Fabricio Araujo
Yeah Anders Ohlson, he was at Delphi Developer Days in Chicago back in June.
Peter Turner
+5  A: 

I definitely do tend to re-use local variables like 'Findex' (or just plain 'i') if the routine has several distinct iterative sections to it. Not really the best practice I guess, but I'd like to think it's only really obvious where I do it, and obviously the usage doesn't overlap.

It's not usually a big deal to go back to the top of the routine and key-in the new variables, though I didn't know about Ctrl-Shift-V (will be trying that later!).

It'll be interesting to see what everyone else says. :-)

robsoft
+1  A: 

As a long time Delphi user (since 1.0) this is the major thing I hate about Pascal. all other modern languages support definition at the point of use, yet Delphi persists with the var section, and Delphi programmers persist in ridiculous hand-waving antics to justify it.

anon
How is it "ridiculous hand-waving antics" to say that code where you can find all of you variables in one well-defined place is cleaner and easier to read than code where they could be declared just about anywhere?
Mason Wheeler
Have you ever used a language that allows definition at point of use? If so, which one, and did you REALLY find it difficult to use? And if not, I don't think you are qualified to comment on the subject.
anon
That depends on the viewpoint. Especially for temporary variables used in a very small scope (like the counter variable of a for loop) I would really like Delphi to allow variables with block scope. Of course such a feature could be abused too.
dummzeuch
@Neil: Coming from a Pascal background it always feels a bit messy, yes. The overview of local variables tell me a lot about the method at hand, just as the list of class fields at the top of a class. I wouldn't say it "REALLY makes it difficult", because we are still talking about variable declaration. Not the most rocket-sciency part of the code, usually.
Paul-Jan
@Neil: I've used plenty of languages, and as you pointed out Pascal is the only modern language that does the "var section" thing. I find it makes my code a lot better-organized and easier to read, not to mention easier to debug. It drives me nuts when I'm trying to step through a C routine and my list of locals keeps changing!
Mason Wheeler
@Mason Well, unless you are using C99 which not many people seem to do, C is one of the few remaining languages (along with Pascal) that insists on declaring variables before they are used. So I don't understand your problem.
anon
@Neil: It seems obvious that he means C++.
Ulrich Gerhardt
@Ulrich: No, I mean C, although C++ has the same problem. @Neil: I wouldn't have it any other way. If you can use variables without declaring them, typos change from an easily-found compile error to a very subtle runtime error that takes tons of time and frustration to track down. There's a reason **Option Explicit** was considered a best practice for VB6...
Mason Wheeler
Neither C nor C++ allows you to use variables without declaring them.
anon
Since you seem to be confused by what I said about debugging C code, look at the Delphi debugger. On the bottom-left (by default, at least) is a list of all the local variables for your procedure and their values. The values might change as you step through the procedure, but the number and names of the variables don't. This is not the case in C, where new locals can be created and go out of scope again within the same procedure, which makes keeping track of them a real pain.
Mason Wheeler
"all other modern languages support definition at the point of use" <-- Smalltalk doesn't count as modern?While Common Lisp's LET _could_ be regarded as declaring something at point-of-use, I'd much rather read a function where all the local variables are declared in one LET at the top!
Frank Shearar
No, Smalltalk doesn't, IMHO - it dates from the late 70s.
anon
+12  A: 

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.

Jim McKeeth
OK, maybe you're in control of more code than I am, but given a situation where you have 50 string fields in a table and you need to assign them to some labels' caption property and the function you're given to access data from the database requires a VAR parameter, so you can't pass in label1.caption. Then I'll bet you'd use something like getfield('thisstring', tempstr); label1.caption := tempstr; getfield('thisotherstring', tempstr); label2.caption := tempstr; etc... etc... etc.... Yea or Nay?
Peter Turner
@Peter: I would create a procedure that does that for me, so I just pass ('thisstring', label1) and it does the GetFeild() and .caption := for me. Maybe even I would setup a way to pass a list of fields and labels, or even create a way to associate the two with each other. If you are working somewhere you can't create a helper procedure then you need to look for a new job. BTW, my company is hiring, and we let you write good code.
Jim McKeeth
+1  A: 

Well Curly did have a good point. I'm a sinner in that respect occasionally. Usually just a temp string variable for convenience more than anything.

To be honest I've never really thought about it... until now. I have no issue with the VAR section being where it is as that's been a habit formed since Delphi 1.0.

To answer the question, I only re-use a temp variable, usually a string, and usually only to gain some slight performance improvements. Don't have an issue with that.

Gerard
+2  A: 

You can develop your own style of coding that uses variables as required. I generally use unique vars (90%) with a few temp vars (10%) when required.

It depends on the nature of the var. If it is a var to help support other code (counter for loops, building SQL strings, etc.) then a temp var you can re-use is helpful. In this case your temp vars are useful as "disposable" vars in sections of code. Just add a comment to your var declarations indicating the temp vars.

i.e. //temp vars are re-used as required in this procedure --> clear/re-initialise them after/before use.

Other than that I avoid temp vars & never use them to hold critical data. A unique var should be used then to avoid confusion & make readability/maintenance of code clearer.

ForerMedia