Where do you DIM your vars!?

Where do you put your DIM code!?
At the top of the page or inside the code, when the variable is used for the first time?

Sometimes I think it’s easier to DIM where the var is used for the first time, along with some documentation of the purpose…
But I also see the charm of doing all the DIMming on the top!

What’s your vote!?

Top of the file to have a basic list of variables and avoiding to use non existent ones(if it’s not in the top list i forgot to declare it).

There are no code/performance issues in disseminating them wildly in your code so you are free to declare it as you like so YMMV :slight_smile:

On top. Properly named so I know what they mean. Not s1,s2,s3 as String or something like that.
If on top they are easily found :slight_smile:

At the top, the project I did with XOJO has a lot and it was easier to give them logical names seeing them in a list and give them some sense of order at the same time, it would be a lot harder if they were mixed in with the code, like Albin I never use letters and numbers, usually I use camel case names

Interesting. Thank you!
I guess, if you can see all the code at once, the order is not so important.
It’s more important if you need to scroll much to see all code?

But at the other hand, if you need to scroll that much, maybe then it’s easier to just make a Method out of it!? If possible, of-course!!

Always when the variable is used. Also I usually try to make methods so short that I don’t need to scroll.

For me it makes more sense to declare a variable when it needs to be used, therefore as late as possible.
Also, declaring on top makes you allocate memory for something may not be actually used.

Interesting thoughts, actually!
As far as the conversation show, to this point, there seem to be no right and no wrong!

It more seems to be a personal kind of view, how you write the code.
Maybe a mix will do?
Sometimes the top is better, with better overview. But also, sometimes “when needed” may be better in other cases.

Interesting! Very!

I declare a variable immediately before its first use. It would be nice to have the possibility to “end” the scope of a variable before the end of a method. Something like:

... Dim xyz As Integer ... Endim xyz ...
to have something similar like what in C# is:

... { int xyz; ... } ...

When it is used. It avoids cases where you are scrolling through code and have no idea what a variable is at the bottom. Then you have to wade through a sea of declarations to find it.

It’s also much easier to leave unused variables around when they are declared all at once. You DIM’d it at the top, wrote some code, later deleted that code, but forgot to delete the variable too. If the variables is dimensioned as part of the code, that can’t happen.

@Eli Ott, this works for just that purpose:

if true then // Scope
  dim xyz as integer
  …
end if

xyz = 4 // ERROR - out of scope

I tend to declare them when I use them. Like Kem, I don’t like putting them at the top of the method because then I forget to remove them sometimes. I also like the scope control of declaring them where you use them.

Do what makes sense to you. That’s really all that matters. There’s no real right or wrong.

What I find myself doing more and more is making the shortest possible method to avoid scrolling in the code editor. Too much code makes it hard to read regardless of where the variables are declared. I also avoid having more then a couple of indents in code when possible. I’m a lazy programmer. I want the shortest, easiest to read code I can possibly write.

my only deviance from this, is that I do use single letters , but only for specific types of uses. and these are NEVER public

S and T for temp strings
I,J,K, L for for/next indexes

otherwise all variables are given full names that reflect their use

For the most part, I do identically to what Dave S outlined (minor exceptions like I use idx. jdx, kdx for indexing variables, etc.) including attempting to name variables something that I’ll recognize it for what it really is 6 months from now.

Of course, this goes to hell in a hand-basket when you have control sets. I have an emulated data grid in my current project where each data cell contains a text field. Some of these grids have 150 - 200 such cells … which precludes the usage of individualized, descriptively-meaningful names for each text field in place of a control set. In my case, let’s say I named the control set “txf” … so, for instance, I really have no way of knowing whether txf(99) is a volts, amps, watts, or whatever field without having a commented cross reference list of each cell’s contents somewhere close at hand in the code. In the end run, having the ability to use control sets in scenarios like that still trumps (by a long shot) whatever pain may result from the resultant inability to descriptively label each variable individually.

my use of I,J,K, S & T is rooted in my first introduction to programming… [ie. Fortran66 in 1974]

I always Dim at top of scope for the variable.

I dim when the oncoming car flashes their high beams…

So, your car has “vars” (since you’re dimming them) and not lights? Please add your location to your profile … wanna make real sure I’m not on the road at night anywhere near where you live ^^

I dim key fields for the method at the top of the method and then dim the lesser loop type variables as and when I need them so that they go out of scope the moment I have finished with them.

Example:
for lp as integer = 0 to lbLines.Listcount-1

next

As local in scope as I need them and usually right when I need them

I keep flowers in mine …