PDA

View Full Version : To declare or not to declare



georgiboy
05-17-2024, 12:10 AM
Hi everyone,

I intend this thread to be a general discussion on the topic of variable declaration.

I’m interested in hearing your personal opinions on whether or not to declare variables. Please share your experiences and provide arguments for and against variable declaration, along with examples if possible.

I’m not looking for a definitive answer on whether we should declare variables, as I understand this is largely a matter of personal style. Instead, I hope this discussion will offer insights that help others decide whether variable declaration aligns with their coding style.

Personally, I believe in declaring variables, and here are a few reasons why. I may post some examples later in the thread:


Memory Management: By specifying the data type, VBA allocates the right amount of memory for the variable.
Code Readability: Well named variables make your code easier to understand.
Error Checking: Declaring variables helps catch errors where a variable might be used incorrectly.
Optimization: The VBA compiler can optimize the code better if it knows the data types in advance.

REMEMBER, this is a discussion and not an argument so play nice.

Have a nice day...

George

Aflatoon
05-17-2024, 02:12 AM
Devil's advocate:
Memory management: is irrelevant in pretty much all code that I've seen in forums.
Code readability: doesn't actually require you to declare your variables. Also, variable naming and where to declare is a whole other flame war. ;)
Error checking: can also give a false sense of security as the compiler isn't necessarily 100% reliable on that front
Optimisation: see memory management :)

Against declaring:
Saves typing and therefore reduces the risk of RSI and/or arthritis in later life

I'm not sure I've ever seen any other arguments against it.

Aussiebear
05-17-2024, 03:08 AM
I am a firm believer in declaring your variables. Keeps your code tidy and readable, more so if the code become the responsibility of someone else to maintain it. For those prolific writers of code, it enables you to remember what your code is representing months down the track.

JKwan
05-17-2024, 06:06 AM
I declare my variables for the purpose of error checking (primarily). I had my misses in the past and cost me dearly.

Paul_Hossler
05-17-2024, 06:25 AM
My $0.02 ---

If a variable is not declared, then it's a Variant, which means anything can be assigned or assigned again

I prefer to get a runtime or development time error since I want all the help I can get



'Option Explicit

Sub NotDelcared()
x = 1234
x = "asdfg"
x = True
Set x = Worksheets(1)
MsgBox x.Name
End Sub


Sub Delcared()
Dim n As Long
Dim s As String
Dim b As Boolean
Dim o As Worksheet
n = 1234
s = "asdfg"
b = True
Set o = Worksheets(1)
MsgBox o.Name
n = s
End Sub


31571

georgiboy
05-17-2024, 07:40 AM
I have to agree on that part @Paul_Hossler,

In my experience, I am able to capture errors earlier in the code and (to me) they make more sense as to where the errors form. Take the below for example, without declarations the error forms on the last line and (again, to me) does not seem completely obvious as to why:

Sub NotDeclared()
s = "Hello world"

' some code...

n = s

' Lots and
' lots and
' lots of code

MsgBox n + 1 '<<< Error comes here
End Sub

Whereas with the variables being declared (below), the error comes much earlier in the code and seems (to me) more obvious as to what the problem is as it comes when passing a value to a variable. Worth noting also that the error was triggered before the 'Lots of code' part, so if you are into writing long sub routines then it could save you some scrolling up and down:

Sub Declared()
Dim s As String, n As Long

s = "Hello world"

' some code...

n = s '<<< Error comes here

' Lots and
' lots and
' lots of code

MsgBox n + 1
End Sub

I would still like to see some arguments for not declaring as I am sure there are some logical arguments and thought processes behind both methods.

Bob Phillips
05-17-2024, 01:48 PM
I am very pro declaring variables, and adding Option Explicit to all modules.

Rory's arguments, Devil's advocate I know, but for, even if the compiler is not 100% accurate, surely it is better to get whatever comfort it does provide. And you can also get extra comfort when using the variable, I declare the name with a form of camel-case, but type them in lower-case. If I type it correctly, it upshifts, if I don't I get an immediate feedback that I have erred. Against, the extra typing is totally irrelevant in my eyes, we don't write enough code to make declaring variables onerous. You can save later when you use a variable by typing a few letters, then use Ctl-spacebar to auto-complete.

Bill Jelen doesn't believe Option Explicit, he and I have had this discussion so often. but I haven't convinced him.

Aflatoon
05-19-2024, 02:28 AM
For the record, I always declare. Even as lazy as I am, I can’t see any benefit to not doing so; and there are definite benefits to doing it, as everyone has mentioned.

still waiting for a certain someone to show up in this thread…

Aussiebear
05-19-2024, 03:18 AM
I doubt if he will....

mark007
05-20-2024, 03:37 AM
Surely you all have 'Require variable declaration' ticked to force it!





Dim ImportantValue as string
ImportantValue="Important"


If ImpotantValue="" then
Msgbox "Why have I spent hours debugging this code to find out why this message keeps appearing?"
end if

Aussiebear
05-20-2024, 12:14 PM
Surely you all have 'Require variable declaration' ticked to force it!
True, but you need to want to do this.

Jan Karel Pieterse
05-21-2024, 02:16 AM
Declaring actually SAVES typing. Especially if a variable is used more than once or twice: type one or two characters, hit control+Space and bam! And declaring vairables using the correct type allows intellisense to work.

georgiboy
05-21-2024, 02:24 AM
declaring vairables using the correct type allows intellisense to work.

This is a good point, saves even more typing.

Aussiebear
05-21-2024, 03:11 AM
Right so at this stage, its 99.9% in favour of declaring and 0.1% as not declaring. Mind you the 0.1% is for the guy who thinks its not necessary but couldn't find the courage to post.

Aflatoon
05-21-2024, 04:44 AM
Declaring actually SAVES typing. Especially if a variable is used more than once or twice: type one or two characters, hit control+Space and bam!

Not if you also only ever use 1-3 characters for your variable names... ;)

Bob Phillips
05-23-2024, 02:05 AM
Not if you also only ever use 1-3 characters for your variable names... ;)

Many moons ago, in my days as a Cobol programmer, a colleague would declare their variables as a,b,c,d, etc. Drove me mad.

Aussiebear
05-23-2024, 02:55 AM
Many moons ago, in my days as a Cobol programmer, a colleague would declare their variables as a,b,c,d, etc. Drove me mad.

oooookay...... !!!! :devil2:

On second thoughts, you are not mad, just English.

Paul_Hossler
05-23-2024, 05:11 AM
Many moons ago, in my days as a Cobol programmer, a colleague would declare their variables as a,b,c,d, etc. Drove me mad.

How about aa, a1, a_1, _a, _a1, ...

Aussiebear
05-23-2024, 05:24 AM
Hmmm.... you are more English than I thought.