| | Submitted on: 6/27/2003 4:16:43 PM
By: Jim K
Level: Beginner User Rating:
By 12 Users Compatibility:VB 6.0
Users have accessed this article 1153 times. | (About the author) |
| | Why is "Option Explicit" that important ? | |
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. | I see a lot of complaints on submitters not using Option Explicit In their modules. Specially, this is common to newbies submissions. I'm tired of it, so i'll explain it for the newbies....
It's used at module level to force explicit declaration of all variables in that module.
'Option Explicit'
Remarks:
If used, the Option Explicit statement must appear in a module before any procedures.
When Option Explicit appears in a module, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements.
If you attempt to use an undeclared variable name, an error occurs at compile time.
If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Default type statement.
Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.
Conclution is: Use Option Explicit
Now.. Ther might be some of you that don't use it, maybe because you forget to type it ??
To get Option Explicit automatically inserted in any module you add to your prj, go to VB's menu - Tools - Options - Editor tab. Require variable Declarations should be Checked and then you'll never again have to think of it.
Someone maybe concider this as a dumb posting. but i mean the beginner (Newbie) has to learn it's importancy some way down the road.
Good luck with your next upload :) I hope Option Explicit are stated. | |
Other 10 submission(s) by this author
|
|
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
6/27/2003 4:24:39 PM:Neophyte Amen! I used option explicit since day
1, I'm sure hundreds of problems would
have arised hadn't I done so. In a
couple of submissions I have seen
'Option Base' or something like that,
could somebody explain what that is?
Thanks.
|
6/27/2003 4:45:34 PM:Jim K NeoPhyte!
Option Base..
Basically
used to make a more flexible way to
control the range of an array's
subscripts. Though, It's mostly used
when dimensional arrays is used in you
prj.
|
6/27/2003 4:47:35 PM:Neophyte k. thanks
|
6/27/2003 5:39:24 PM:Visualcode Okay, there are already submissions of
this
http://www.pscode.com/vb/scripts/S
howCode.asp?txtCodeId=31732&lngWId=1
http://www.pscode.com/vb/scripts/ShowCod
e.asp?txtCodeId=44963&lngWId=1
http:/
/www.pscode.com/vb/scripts/ShowCode.asp?
txtCodeId=26330&lngWId=1
So as you
can see there are enough of these, one
is good, if they truly cared, they
would look it up, having multiple
clones on the site is just a waste. No
offence but as you can probably see,
the article is not needed.
|
6/27/2003 5:53:51 PM:Keith Stanier I make a point about this in "Tips for
VB Programmers". This advise doesn't
only apply to newbies. I download code
from oldies that don't use Option
Explicit. Visualcode mustn’t use it
either. 5 globes from me.
|
6/27/2003 6:31:32 PM:Jonathan P. Ivy This is Lame.
No offence, but I've
been keeping track of my own variables
since QBasic. I don't need to option
explicit anything. It is just a tool
used by newbs or people too lazy to
remember what variables are declared
and what are not.
This submission is a
waste of space.
|
6/27/2003 6:36:03 PM:shock LOL, newbies dont want to read that,
ill sum it up for you: you put it at
the top of a module and it requires you
to declare all variable, you dont need
all that crapp, im not gonna bother to
vote
|
6/27/2003 6:37:58 PM:darren kurn Option Explicit is probably the most
important single line of code that any
VB programmer will ever use, as it does
not allow for any mistakes such as
Typos. Another thing that many
programmers could do with is learning
to name their variables properly. (what
does X mean in these programs!) a good
method is (for instance) if you are
defining an integer, use i as the first
letter of the variable (for instance
iMynum). this makes programs so much
easier to debug!
|
6/27/2003 9:35:00 PM: I've always wanted to get used to
typing that but I always forget man.
Thanks for teaching me that Tools..
Options.. thingy!!
|
6/27/2003 10:17:06 PM:Patabugen- Thanks! This is great stuff i had no
idea what it did so i never put it in.
ill get it to do it auto instead now =]
hehe Thanx! 5*
Visualcode > if this
dude hadnt posted this "copy" then i
still wouldnt use it. so it does help
dosnt it.
Shock > im a newb n im glad
i read it.
|
6/27/2003 10:17:57 PM:Masino Sinaga I agree with Neophyte, just say: Amen!
Stop debating about Option Explicit. If
you don't need it, leave it. If you
need it, write it down. All you have to
do just say: Amen! Let's appreciate
anything that has been posted by
anyone. Thanks to Jim K for teaching
about Option Explicit. I appreciate it.
|
6/27/2003 10:27:36 PM:G Nelson You use of newbie is offensive, so let
me enlighten you newbie.
Many
Dictionaries define the term is as a
variant of the English public school
term, new boy. Such as, someone in the
first year or period of school. The
term predates the Web, computers, and
even World War II.
Bottom line is it
may apply to people new to VB and then
again it may not.
|
6/27/2003 10:44:29 PM:Brian Matthew Anderson Uhhhh Uh huh-huh-huh-huh... Option
Explicit id cool... I use it when
writing a program, but when I'm
finished and the program compiles
without errors, I then delete it cause
its kinda a waste to have it there
after everything runs, like, good and
stuff... It like, uhhhh... saves
precious memory and stuff.. Uh
huh-huh-huh. Heh-heh. Huh.
|
6/27/2003 10:45:45 PM:Brian Matthew Anderson "Newbie"... Heh Heh-heh-heh-huh
|
6/28/2003 1:20:14 AM:Visualcode Keith Stanier
I do use Option
Explicit
my point was simply that
there are enough articles on this.
|
6/28/2003 2:35:42 AM:Jonathan P. Ivy I would never call Option Explicit
st*pid, however you and you're article
are expressly st*pid in the fact that
you see all those that do not use
option explicit as newbies. This only
shows you're conceit. Insults such as
this only stand to hurt how others
percieve you. It is like Bush calling
Suddam evil. It merely shows pride. You
should delete this post, and don't
submit a non-code submission like this
again.
|
6/28/2003 4:16:57 AM:Jim K P. Ivy,
If you see this posting or
me who posted it as st*pid, i wouldn't
lose my sleep over it :) I posted it
cause i wanted to enlighten the
beginners. As you can see from some of
the feedbacks, it have helped a couple
of guys already and that was the main
purpose, not to start a fight with
people like you. That is what's a waste
of time, space and st*pid. Go get some
fresh air.
To G Nelson.
I did not
use newbie as an offencive expression.
I normally use beginner, but since it
has become so common here on psc i did
not think much of that. I do appologize
to those who find it offending. It was
never ment to be.
|
6/28/2003 5:13:12 AM:Dream Jim, dont appologize, and for the ones
that flame... just delete. Personally I
didnt know about the auto setting so Im
going to check it out!
|
6/28/2003 5:14:21 AM:Dream hmm and its right there!!! lmao
|
6/28/2003 7:06:16 AM:Danny Cain This is Lame. No offence, but I've been
keeping track of my own variables since
QBasic. I don't need to option explicit
anything. It is just a tool used by
newbs or people too lazy to remember
what variables are declared and what
are not. This submission is a waste of
space."
Obviously someone who NEVER
makes typos :P
|
6/28/2003 12:26:29 PM:Haru Glory Don't blame Jim if you've already know
this. Well, i think this is not a waste
of space. This code is for beginners so
don't expect too much. I used Option
Explicit always to make my code more
clear. Danny Cain, am not a newbs and
am not lazy, i have my own reasons why
am using this code. In short, for those
people who used Option Expicit are
newbs and lazy :|
|
6/28/2003 1:43:43 PM:Nathanael B I have had this enabled from the
beginning and it's saved me a whole lot
of trouble. The author is correct, most
downloads here on PSC don't have Option
Explicit stated. People need to know
about this! (5 globes from me)
|
6/28/2003 2:34:57 PM:Brian Matthew Anderson Jim, here's the code I used to 3rate
your code:
Option Explicit
Dim I as
Integer
Private Sub
Form_Load()
Randomize(-1)
I=Int(rnd*5)
+1
Debug.?"Jim, you get ";I;"
globes!"
End Sub
Thanks to Option
Explicit, the program ran perfectly.
Unfortunately for you it gave you only
2 globes. Maybe you could suggest a way
to improve this program so that it give
you more globes next time.
|
6/28/2003 3:04:03 PM:Jim K Brian M A,
Suggestion
:)
Option Explicit
Private Sub
Form_Load()
Dim i As
Integer
Randomize (1)
i = Int(Rnd *
5) + 2
Debug.Print "Jim, you get "; i;
" globes!"
End Sub
I guess this one
gives a 5. right?? :)
PS! I never
been here for the votes. Feedbacks are
appreciated though. You gave yours.
Thanks !
|
6/28/2003 5:48:48 PM:Burbble Ok, PSC just deleted my previous
comment as I was trying to submit
it.
Basically:
Option Explicit is
a waste of time, space, and energy. I
have been programming for three years,
and I have NEVER used it. Why would I
WANT to declare EVERY variable when VB
will do it for me? Using it just forces
you to spend more time declaring every
stuÞid little variable.
Also,
submissions that lack "Option Explicit"
do NOT show an unexperienced
programmer; on the contrary, it shows
an experienced one who does not need to
be reminded to declare every
variable.
Lastly, to avoid the
comment/question in the future: No, I
have never typed the name of a variable
incorrectly and caused a huge "why
won't it work" problem.
|
6/28/2003 6:21:45 PM:Coding Genius People who speak like that are just
ignorant. I bet if you go look through
your programs you'll find tons of slow,
large variants. Especially mr ''Why
would I WANT to declare EVERY variable
when VB will do it for me''
there....who is wondering why is
applications are so huge, slow and
memory consuming. Every other popular
language in the world requires you to
declare variables (C, C++, Java,
Delphi, Cobol, Pascal, ADA) except VB.
Obviously mr '' I have been programming
for three years'' up there hasn't tried
any other languages then.
|
6/28/2003 9:20:47 PM:Jim K To you normal human beings. Thanks for
your support on my posting.
|
6/28/2003 10:04:18 PM:Marzo Sette Torres Junior (sorry for this, Burbble, but I can't
resist) Not using Option Explicit is a
waste of time, space, and energy. I
have been programming for eight years,
and I have ALWAYS used it. Why would I
WANT to declare EVERY variable when VB
will do it for me? Not using it just
forces you to spend more time tracking
down every stuÞid little typo; also,
all undeclared variables are variants,
which are (1) slow, (2) slow, (3) slow
and (4) large (1 variant = 4 longs, and
they can be 16 times as slower
depending on what you do). Also,
submissions that lack "Option Explicit"
are a trademark of the unexperienced
programmer; that is right, it shows a
progrmammer who does not know how LARGE
and ssssslllllooooowwwww variants are,
nor that not declaring variables will
force variants upon the program.
Lastly, to avoid the comment/question
in the future: Yes, I have in many
occasions typed the name of a variable
incorrectly and caused a huge "why
won't it work" problem; after all, I am
only human.
|
6/29/2003 2:05:52 AM:Haru Glory I'm going to give this 5 globes bec. it
helps a lot for those people who don't
know this. I recommend to all of you to
use Option Explicit ALWAYSSSSSS.
|
6/29/2003 5:23:40 AM:Mil-X Pro Try to think why VB has "Option
Explicit" option, there must be
something useful using it. And IMHO, a
good programmer has to use every
function etc that the programming
langguage provide in his programming
practice. Especially when sharing code
with other programmer, it help your
mate a lot. And you have nothing to
loose. Someone has to tell others the
good thing he knew, even if it is a
waste to somebody.
|
6/29/2003 5:41:41 AM:sabugen I think not using Option Explicit can
optimize your code (variable checks
etc.). Just make sure, you type your
variables correctly.. try coding with
option explicit, then when you finish
coding and checking your app, remove
the Option Explicit line then compile,
|
6/29/2003 6:24:52 AM:Coding Genius how does it possible optimize your code
by NOT using it? RoFL! Option explicit
is a Compiler Directive. It's never
built into the final exe. It just simpy
tells the compiler how you want it to
behave. Without it, all those
undeclared variables are actually
declared as variants in the final exe,
just as if you wrote them yourself.
With option explicit, all those
undeclared variables won't let you
compile your program. Now, before you
comment and say something so stup1d,
please research if first.
|
6/29/2003 6:26:05 AM:Coding Genius Also, I've never seen any code which I
consider truly the work of an
experienced programer that doesn't use
option explicit. Just go look in the
hall of fame for projects which don't
have option explicit in them.
|
6/29/2003 6:27:30 AM:Coding Genius Some of you are practically saying
''Hey man! I don't need no option
explicit. I'm experienced enough to not
have to care about data types! And I
never ever make typo's'' <- Wake da fuk
up and smell the coconut crunch.
|
6/29/2003 8:08:36 AM:Burbble For i = 0 to 9
List1.AddItem i
Next
i
Who wants to declare 'i'?
|
6/29/2003 10:08:23 AM:Burbble Also, in response to Coding Genius and
Marzo Sette Torres Junior:
I realize
that Variants are 'slower' and larger.
They are a few bits larger, and
infinitessimally slower. Using
undeclared variables on For/Next loops,
does not create a sluggish and
enormously sized program (in fact, it
doesn't change the size of the
executable at all...) . I declare every
variable that the program is processing
information from (that's just common
sense).
And to repeat again: I have
never made a careless mistake as to
type the name of a variable wrong and
start panicking about "why won't my
program work"... I read the code as I'm
typing it...
I noticed that I said
I've been programming for three years;
this is incorrect, it is actually
almost six years that I've been
programming in VB and HTML. I've also
done some in C++, PERL, and JavaScript.
|
6/29/2003 11:09:49 AM:Marzo Sette Torres Junior By what you say, you are the editor I
would hire if I were to write a book;
if you never make typoes, you must be
*really* good at finding them
(inhumanly good, in fact), and are
definitelly the person I would have
finding typoes in books. Large books.
Of course I'd sue if a single typo got
past you, for false
advertisement.
Seriously, it appears
(sorry if this impression is wrong)
that you have never programmed a really
large program... I have, and such
typoes are inevitable. I have always
read the program as I wrote and again
after I'm done writing... but
sometimes, something slips; Option
Explicit catches them.
Also, the
difference in speed and size between
variants and other variables is indeed
formidable in (1) larger projects (not
to mention games...), (2) complicated
procedures that are harder to break up
and (3) procedures that will be called
a couple thousand times per second. It
makes all the difference between
"untorelably slow" and "blazing fast".
|
6/29/2003 2:07:37 PM:Burbble First, I have written many incredibly
large programs. The largest program
I've submitted to PSC is probably
either Weather Grabber or Burbble's
Slideshow Maker. Other, more complex
programs I have written I cannot submit
because of copyright
issues.
Obviously, a small amount of
typos is a given when writing large
amounts of anything. However, I am
extremely anal when it comes to writing
anything, and I make sure it is
grammatically correct (I've probably
revised this comment a dozen times
already!).
Also, you should definitely
declare any variable being read from or
written to hundreds of times or more,
and the speed difference of the
application will be seen.
The only
variables that I don't declare when I'm
writing something is if I write a
simple (not something complex,
intricate, and repetitive) For/Next
loop (i.e. For i = 0 to 9 ... Next i
).
Basically, all I am saying is that
using Option Explicit creates more
hassle than it is worth.
|
6/29/2003 6:22:06 PM:Marzo Sette Torres Junior So... basically, you are saying that
Option Explicit being on your projects
would only make you declare a few loop
variables here and there because you
already declare everything else. Since
Option Explicit can be automatically
added to every code module by clicking
a checkbox, having it on would make you
declare a few variables here and there
that you wouldn't otherwise; where is
the hassle?
Most of the time,
declaring variables is faster than
writing the code of any but the
smallest routines, and the added
benefit (that you will never have to
worry about typoes again, having much
more time to think of the actual code)
will more than overshadow the need to
declare all variables. In a way, Option
Explicit will give a slight increase in
productivity because you don't need to
triple-check every line of code to see
if it contains typos: it actually saves
(IME) time and energy.
|
6/29/2003 7:33:34 PM:Brian Matthew Anderson LÖL Option Explicit creates more
"problems" than its worth?!? LÖL! Yeah
it obviously took a lot of effort to
type a whole 14 letters and a space to
boot. By comparison, your comments on
how much of a "Waste of time" Option
Explicit is took far more letters than
14! Even a simple "For I = 0 to 9...
Next I" loop consumes a load of time if
it is nested within several other
For...Next loops. You can't be
considered an expert programmer if you
don't use option explicit. Sure if the
program is a few hundred lines and
speed is not a big deal you don't
really need it, but you are a true Äss
if you think it doesn't matter for
large programs. You probably do what I
do but aren't man enough to admit it -
write the program with option explicit
in, debug till it works, then delete it
out at the end before uploading. You
save like 15 bytes of space on your
upload - big whoop.
|
6/30/2003 9:10:31 AM:Burbble Actually, I have never, ever, once used
Option Explicit for even a fraction of
a second in anything even remotely
related to VB. In response to "You
can't be considered an expert
programmer if you don't use option
explicit.": That is one of the most
rediculous statements I've ever heard.
Option Explicit just provides a
"helping hand", you might say, to
prevent you from typing things
incorrectly. If you don't have this
problem, then you don't need to bother
with Option Explicit. It has NOTHING to
do with how experienced a programmer
is. Actually, the amount of typos and
incorrectly declared variables would be
the signs of a more inexperienced
programmer, and NOT the use of a
superfluous compiler directive!
LOOK:
If you want to use Option Explicit
because you are worried about making
mistakes, then FINE! Use it! If you
don't, then DON'T! WHO CARES?!
|
6/30/2003 9:38:28 AM:Dream You do!
|
6/30/2003 11:17:05 AM:Cpvio hehe**Smirk**
|
6/30/2003 6:34:24 PM:Burbble lol
|
6/30/2003 8:22:07 PM:Brian Matthew Anderson Burbble, you spelled "ridiculous" as
"rediculous". This leads me to wonder
if you meant to spell your name as
"Bubble" instead of "Burbble". Maybe
you should think twice about Option
Explicit, hmmm??? No offense to your
self-proclaimed programming abilities,
which I'm sure are as vast as your
imagination allows for, but its like
Coding Genius said, "Just go look in
the hall of fame for projects which
don't have option explicit in them."
|
7/1/2003 7:57:04 AM:Burbble <b>Oh no!</b> I spelled <font
size="14">RIDICULOUS</font> wrong in a
comment I typed in less than a
minute!!! <b>Whatever shall we
do!!</b>
And... <b>LOL</b>I didn't
spell my NAME wrong! It's supposed to
be BURBBLE... hence the
burbble@hotmail.com....
<b>LOL</b>!
I am not proclaiming my
programming abilities, just my acuracy
at typing code and variable names
correctly (I've never used "ridiculous"
and a variable name.)
|
7/1/2003 7:57:33 AM:Burbble LOL It's stopping HTML code now!!
|
7/1/2003 7:57:54 AM:Burbble Ahh... It's too early for this
|
7/1/2003 7:10:16 PM:Brian Matthew Anderson I think "acuracy" is really supposed to
be spelled "accuracy". You know Bubble
(misspelling intended), it is really
too easy to debunk your programming
philosophy. The point is, you make
common typing or spelling errors even
in your 50 word responses defending
your inhuman ability to never make a
typo when declaring variables. C'mon
Bubble, enough with the deleterious
replies. If everyone really bought in
to your way of programming, I'd need a
3 Ghz computer to run Donkey Kong.
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|