Search Tools Links Login

Importance of Option Explicit


So many people (especially beginners) do not realize the importance of the simple statement Option Explicit in their code. Find out why it is so important.

Original Author: Triumph

Code


The Importance of Using Option Explicit



Many programmers (especially new ones) neglect the use of the Option Explicit statement in their programs. THIS IS VERY BAD CODING!!! I have seen many programs not run at all, much less properly because of not having this simple statement. As a matter of fact, Visual Basic makes it simple to include this sttement in all of the programs you write. Go to Tools > Options > Editor (tab) > "Require Variable Declaration". Check it. It does not appear in the program you have running, but the next new one you open will have it.


"Why is it so important to include this?" you ask. Well, there are many reasons.

1. It stops you from making costly errors.


Without Option Explicit, if you spell a variable wrong in a procedure, Visual Basic will think you are trying to create a new vairable and will create one for you. Then, if you try to read that same variable later on (this time spelling it correctly) it will not have the desired value.


2. It is good coding practice.


I know a man who interviews programmers, and the first question he asks in the interview is, "What is Option Explicit?" If they don't know or answer incorrectly, they are not hired. Then, he asks them when they use it, and, usually, if the answer is not "always", they don't get the job. Option Explicit is extremely important when working with other people. Making a spelling error in your code, which you know because you wrote, may be easy to find. But, if someone else, who does not know your code so well, tries to find it, it could take hours.

 


Overall, using Option Explicit is a good idea, and saves you time and sanity. I know this was not a long drawn out tutorial, but so many people do not use it, and it is so important that they do.

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 96 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.