Descriptive Variables, Descriptive Comments, or Both?

You don't need to dim variables at all in vba ... I believe you can use x for everything. Or x and xx if you need two. :rolleyes:
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I generally use a bag of scrabble letters. I just stick my hand in, pull out four or five and use them. Of course, for the more complicated variables I may pull out up to ten letters. :p
 
Descriptive variables as much as possible here with comments if it's not immediately obvious although I also use l, i and n for loop counters.

I'm currently working my way through some inherited code which has non-descriptive variable names (some are mildly descriptive but using Spanish or French words instead of English - for no other reason than to try and appear edgy I believe). There's no Option Explicit and when I add it i find many variables that haven't been declared and often I get half way through a procedure and find another chunk of Dim statements (where the code was copied and pasted without being re-written). It's horrible yet strangely satisfying to get each module re-written, commented, variables declared and code optimised.

Nick
 
Descriptive variables as much as possible here with comments if it's not immediately obvious although I also use l, i and n for loop counters.

I'm currently working my way through some inherited code which has non-descriptive variable names (some are mildly descriptive but using Spanish or French words instead of English - for no other reason than to try and appear edgy I believe). There's no Option Explicit and when I add it i find many variables that haven't been declared and often I get half way through a procedure and find another chunk of Dim statements (where the code was copied and pasted without being re-written). It's horrible yet strangely satisfying to get each module re-written, commented, variables declared and code optimised.

Nick

I agree, getting code rewritten in a bit more of a logical sense does feel a lot better (and the code most usually will run a lot better). One of my little pet peeves is when I see variables dimmed somewhere in the middle of code. There is no reason why it can't be dimmed with the other code! It shouldn't have to sit all alone, in the middle of the code, with no other variables to play with. :biggrin:
 
I *thought* I used a lot of i,j,a,b,s,t,x variables (I do for many short subs and functions). Inspecting my more serious projects shows more descriptive variables with hungarian notation, and sometimes Types or Classes.

I do like the precise, abbreviated logic of one letter variables and find it's easier to read - most of my short routines use them, and even longer ones. But I comment my code fairly extensively, and try to keep the procedures discrete so that variables are local and short lived, as much as possible. True, it's perplexing at first. But with some familiarity you start to see the logic (I always think of Jindon's code in this regard - at first, quite a puzzle, but with time I see the patterns and start to "get it").

On the subject of variables dimmed in the middle of code ... I *do* rather like the idea of block-level scope. We could make use of that (I think VB.Net has it now, but not VBA ... someone correct me if I am wrong).

ξ
 
I personally like to stick somewhat to the hungarian style created for c++ or what ever they called it. You use a small descriptor before vars, functions or callbacks to let you know what it controls. so btnName for a script which controls a button, txtbName for a text box control, iName for an integer var, pName for private data in a class, strName for a string etc.
 
I think I'm somewhat like Greg (but no where near so precise) in that I declare everything up front.


I can't stand variables declared in the middle of something.

And like Brandon, I use descriptors as well.
 
Great stuff here!


I am kinda new at VBa. I have seen "str*?" many times in other posts as a var. name, yet untill I read this thread it never clicked str stood for string. :eek:


I am liking the neat appearance by lining them up like Greg (thanks for the Hungarian Link btw), but I am still trying to pick up on writing 'good' code so naming a var. I don't need is a bit trivial. :)

For me, I am trying to improve and this thread has already helped the appearance of recent code. But, I will still pull one out when I don't know any better. :LOL:

-Jeff
 

Forum statistics

Threads
1,215,339
Messages
6,124,381
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top