Data type prefixes

I had this question after viewing A Guide to Writing Understandable and Maintainable VBA Code.

I see you suggested using data type prefixes that match the type of data being used (such as intXXX for an integer variable.)
However, in one code example you used this:

Dim nPos As Integer

First time I saw this, I was disturbed because it didn't follow the standard, but after having to go through some code where I had to change integer variables to longs, I found it frustrating to have to change all of my "i" (or "int") prefixes to "l" (or "lng")

So where do you stand on using an "n" prefix for numbers versus specific data types like "i" and "l" and maybe even "dbl" or "sng"?

--
Also, what prefixes do you add when creating a variety of objects such as Range and Worksheet?
LVL 22
rspahitzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Slightly off-topic, but keep in mind that this Hungarian notation--as it is known--is a stylistic preference, and may not apply to every programming language you work in. If you ever move from VBA into .NET, then Hungarian notation is not as prevalent. The reason for this is that the IDE--Visual Studio--provides you with handy features which let you quickly inspect the type of a variable, no matter its location in the source code. There is nothing inherently wrong with using Hungarian notation; as I mentioned, it's a matter of preference.
0
Wayne Taylor (webtubbs)Commented:
While I agree with the bulk of the article, on data-type prefixes I don't - for the reason you mention where variables change data-types. And if it's that important to determine the data-type of a variable, right-click it and select "Definition". You can then return to where you were by right-clicking again and selecting "Last Position".
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
As I've stated in the article, keeping the prefix shorter and more generic should suffice. It might be important to see whether the var content is a string or number or float immediately, but that's it.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rspahitzAuthor Commented:
@käµfm³d
"If you ever move from VBA into .NET,"
Absolutely.  I actually made that comment in the referenced link.

@Wayne
Good points.  I'd forgotten about the "Last Position" feature.  I'll have to use that more. Related, if you keep your procedures small, it becomes very easy to glance up and see the related Dim state for any variable.

@Qlemo
"keeping the prefix shorter"
Yup, always a good plan to reduce excess text as long as the message is still there.
0
Martin LissOlder than dirtCommented:
I had this question after viewing A Guide to Writing Understandable and Maintainable VBA Code.

I see you suggested using data type prefixes that match the type of data being used (such as intXXX for an integer variable.)
However, in one code example you used this:

Dim nPos As Integer

First time I saw this, I was disturbed because it didn't follow the standard, but after having to go through some code where I had to change integer variables to longs, I found it frustrating to have to change all of my "i" (or "int") prefixes to "l" (or "lng")

So where do you stand on using an "n" prefix for numbers versus specific data types like "i" and "l" and maybe even "dbl" or "sng"?

--
Also, what prefixes do you add when creating a variety of objects such as Range and Worksheet?
I probably wrote the sub with the "nPos" variable more than 15 years ago. I wouldn't do it that way now and I should have updated it before I posted it, and I'll do that if I need to update the article.

When changing things like "i" to "int" it would take longer but I wouldn't try to change all "i" to "int" but rather one by one by variable name.

As long as you are consistent I think that single character prefixes are OK, but you'll need to make exceptions to differentiate string variable from single variables and there are possibly more that I can't think of right now.

Range = rng
Worksheet = ws or wks
Workbook = wb or wkb
0
Martin LissOlder than dirtCommented:
While I agree with the bulk of the article, on data-type prefixes I don't - for the reason you mention where variables change data-types. And if it's that important to determine the data-type of a variable, right-click it and select "Definition". You can then return to where you were by right-clicking again and selecting "Last Position".
Sure but that takes time (I know it's not very much time) whereas a prefix takes no time at all.
0
Martin LissOlder than dirtCommented:
As I've stated in the article, keeping the prefix shorter and more generic should suffice. It might be important to see whether the var content is a string or number or float immediately, but that's it.
IMO it adds value to know that something is a date or a currency variable or a collection versus a dictionary and I don't see any downside in doing it in any case.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rspahitzAuthor Commented:
All great responses.
These days I tend to lean toward the current standards in .NET (no prefixes since the tool helps out), but the traditional standard (short prefixes) in VBA.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.