Option Explicit

W

Warrio

Hello!

Sorry if I'm asking a question already asked, but I haven't found a real
answer to my question on Internet...

Is it really worth to put Option Explicit? it would be great if you could
develop your answer!

Thanks in advance!
 
D

Douglas J. Steele

No question about it: you should ALWAYS use Option Explicit. To me, the
question is "Why isn't it the default as shipped?"

Some of the advantages include:

1) It'll help find typos in variable names.
2) It'll help ensure your variables have the appropriate scope.
3) It assures you're using the appropriate variable type (undeclared
variables will always be Variants)
 
M

Marshall Barton

Warrio said:
Sorry if I'm asking a question already asked, but I haven't found a real
answer to my question on Internet...

Is it really worth to put Option Explicit? it would be great if you could
develop your answer!


Strongly recommended.

If you don't use it, then any undeclared variable name you
use will be type Variant and have a value of Null when you
misspell the name you meant to use. Option Explicit will
catch that error immediately so you can avoid a lot of
tedious debugging just to find a typo.
 
W

Warrio

Thanks for your quick answer! but sorry I forgot to mention in which way it
would help Access to run my code faster ?

and so far, I'm happy to have the choice to declare or not my variables and
set their type, but I'm ready to change my habits ONLY if it can help my
forms run and load faster (you made your choice, I can have a different one)
....
the main part of my forms performances is dealing with the sql requests and
actions. but how much will I win if I use "Option Explicit" knowing that
today users have 1 to 2 GB processors...

Thanks again!
 
R

Rick Brandt

Warrio said:
Thanks for your quick answer! but sorry I forgot to mention in which
way it would help Access to run my code faster ?

and so far, I'm happy to have the choice to declare or not my
variables and set their type, but I'm ready to change my habits ONLY
if it can help my forms run and load faster (you made your choice, I
can have a different one) ...
the main part of my forms performances is dealing with the sql
requests and actions. but how much will I win if I use "Option
Explicit" knowing that today users have 1 to 2 GB processors...

Thanks again!

It has nothing to do with speed. It has to do with catching silly typing
errors at compile time instead of having them fail at runtime.

If you like taking silly risks then go ahead and leave it out.
 
W

Warrio

So the statement written in the Performance Analyser tool "Using an Option
Explicit statement will enable your code to run faster." is wrong?

does it deal only with the compling time or also with the runtime?
 
D

Douglas J. Steele

Actually, I suspect it will be slightly faster to have all of the variables
declared, as Access will have reserved memory for all of them in advance,
rather than having to reserve memory as they're encountered. I doubt,
however, that the difference will be that significant.

However, as we've all told you, you personally can save hours of effort
trying to debug your code when it isn't working properly if you use Option
Explicit and declare your variables.
 
W

Warrio

Thanks for the advice ; )

but I thought that Access reserves a place in the memory only when you
intialize the variable with a data not on the Dim statment?!?
 
D

Douglas J. Steele

I don't believe that's correct.

Declaring a variable should reserve memory for it. And if you don't declare
them, then the first reference to them should reserve memory, whether or not
you initialize the variable.
 
A

Albert D. Kallal

Warrio said:
Thanks for the advice ; )

but I thought that Access reserves a place in the memory only when you
intialize the variable with a data not on the Dim statment?!?

No, not quite.

With a string, or variant, then yes..the storage is not allocated until
runtime, because you don't know the size of the variable.

However, with fixed length strings, integers, long, currency etc, they
actually default to a value of zero, and the storage is allocated at
runtime..even if you DO NOT set their values....

I am surprised that the "analyzer" suggests to use option explicit. It does
mean that some variable defences will be pre-made, and thus some speed up
might occur, but you not going to measure it by much. I also suspect that
suggestion remains from 14 years ago, and back then..it would make a
diference.

The problem is that access today can easily run 80 million instructions in a
second. So, even if saved a million instructions that still only 1/80th of a
second. And, when is the last time you ran 80 million instructions? those
old comptuers ran at 8mhz, and today were at 3 ghz.

ms-access has not bee processing bound for about 7+ years now. So, if you
increase the processing speed of your computer, you will not see a
improvement in the speed of your code. The speed of memory, disk drive, and
of course in multi-user with a network are all factors FAR more important
then optimizing some processing. Where optimizing today pays off is to
reduce i/o.....not processing....

While access can execute a command to retrieve a record in a instant, you
then have to wait for the operation system, memory, and the disk drive to
get that data. We are i/o bound for the most part...
 
D

David W. Fenton

Sorry if I'm asking a question already asked, but I haven't found
a real answer to my question on Internet...

Is it really worth to put Option Explicit? it would be great if
you could develop your answer!

Sounds like a test question.
 
B

BruceM

The meaning of "intialize the variable with a data not on the Dim statment"
is unclear. As has been mentioned, the speed difference is probably of
little significance. Whether or not variable declaration is required, if
you declare all of your variables the code will run at the same speed.
Undeclared variables will be handled as Variants, which may not be to your
advantage, and in some cases probably won't work at all (I'm not sure how or
if Access handles a Variant date, for instance). By choosing not to declare
a variable you in effect declaring it a Variant, but in any case it is
stored in memory. I can't imagine that Dim varResult as Variant uses memory
less effectively than simply dropping varResult into a line of code and
letting Access sort it out.
 
D

David W. Fenton

No question about it: you should ALWAYS use Option Explicit. To
me, the question is "Why isn't it the default as shipped?"

I can answer that one:

When the VBE was incorporated into Access, Microsoft stupidly chose
to sacrifice things like default Option Explicit for a foolish
consistency with the other applications that already used the VBE.

This was dumb for so dozens of reasons I'm sure any Access
programmer can think of without trying.

And, cf course, they've gone in recent versions of Access back to
making it the default (as with DAO vs. ADO).
 
M

Marshall Barton

Variants are a psuedo object that must be processed to
retrieve their value. This extra work is unnoticalble
unless you are using a lot of variants in a loop with a very
large number of iterations. They also use more memory, so
if you have a lot of them (big array) it could affect other
processing.

Ignoring all that as immaterial in most cases, using typed
variables with compile time name checking is without a doubt
a best practice.

But your making your own bed, I don't have to sleep in it.
 
D

David W. Fenton

ms-access has not bee processing bound for about 7+ years now. So,
if you increase the processing speed of your computer, you will
not see a improvement in the speed of your code. The speed of
memory, disk drive, and of course in multi-user with a network are
all factors FAR more important then optimizing some processing.
Where optimizing today pays off is to reduce i/o.....not
processing....

All that said, and all of it completely true:

You should still use Option Explicit so you don't write garbage
code.
 
W

Warrio

I said it before! you made your decision, I respect it, respect mine!
and as someone who has some respect for himself, if you answer a message,
just answer the question asked not something else letting your emotions talk
instead of your mind!

I have my reason to write what I write and I don't need to tell them. all
what you need to know if you "care so much" about the code I'm writing....
is that I always calculate in ROI (Return On Investment)
which tells the risk I can take and the one I cannot.

and my calculations results in not declaring..
your ROI on declaring your variables is to save time on debugging, fine with
it, I said it! I respect that and understand it.

what I win with not declaring is :
1. if I declare them all, I can have useless variables within my code.
and in Access the optimizer doesn't tell you that as it does in Delphi for
example.
2. I save some memory (even if it's meaningless as Albert D. Kallal) but
with billions of lines of code, it can have a sense...
3. I save some memory in using a variable that is not declared, so
variant for many purposes thanks to the casting
4. Sepending time on debugging is not a waste of time for me, because I
learn how to find errors and try to minimize the time to find it... can be
useful not only in coding but in many stuff.

your messages telling me what I "should do"... is far away from my original
question. and if you reply to a message in a forum, you are supposed to give
some help on what people asked, not telling them about which shirt color
they should wear... I asked about how can we make the code run faster with
an "Option Explicit" statement. Albert D. Kallal was the only one with some
experience of forum to answer only the question asked! and I appreciated the
quality of his answer. That's why I thanked him.

Tolerance is the key word in here. Respect the choice of others even if it
looks wrong to you, it might look right to them, you can show the difference
and the weaknesses of the other option, but Respect it!
what do you do in life? do you shout after all the people who have another
religion, political opinion,.....? no, because it would be a the war! and
that's what happen today, because people don't have tolerance of other
options! so respect other's opinions!
: )

Best regards.
 
K

Keith Wilby

That posting really is the biggest load of clap-trap I've ever had the
misfortune to read.
your messages telling me what I "should do"... is far away from my
original question

I asked about how can we make the code run faster with an "Option Explicit"
statement.

No, your original question was "Is it really worth to put Option Explicit?"
and the correct answer is "Yes". No debate.
Tolerance is the key word in here. <snip> not telling them about which
shirt color they should wear.

Jeez. You're the one who needs to learn tolerance. David's advice is
correct. Period.
 
W

Warrio

The first message was uncompleted, I apologised and completed it!

and yes you are right after all! you are the best in the world!!! keep going
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top