what is use of optionexplicit in variable declaration

Answer Posted / h.w thushara indika

If we used “Option Explicit” then we have to declare our
variables explicitly before they use. Examples in above
posts show this.

Explicit declaration has both pros and cons.

Advantages of Option Explicit:

1. Clearity of the Program enhanced by proper coding rules
• Professionals always instruct to use ‘Option Explicit’
on coding. This enables the programmer to enhance his code
to pre-declared standards.

2. Program code is easier to debug and read
• Programmer has clear idea about each variable he will
use because he has already declared what kind of data type
which will have by each variable. Therefore, even another
programmer can read the code and understand it easily than
an ‘option explicit’ off document.

3. Reducing mistyped variables instead of existing usable
variables
• User may use alternate names mistakenly for already
used variables. If you have ‘option explicit’, then the
mistyped variable will generate an compile error describing
an non-declared variable. But, if you haven’t ‘option
explicit’ in your program, this will be an programming error
which will give an erroneous result to end user.

4. Give more reliability on data
• ‘Option Explicit”cause programmer to have more
understanding on declared variables. This enables the
variable to have additional attention, and reduce any
attempt to misuse a variable unintentionally.

5. Can implement Error detection mechanisms using data types
• Data types define what kind of data that can be hold by
a given variable. If you try to assign date value
“21-08-1985” to character variable, then system will
automatically generate an exception or error. You can use
this error or exception to track the user, but you may want
to build several other mechanisms to track bad inputs if you
didn’t used ‘Option Explicit’

6. Faster memory allocation
• When declaring variables, VB assigns particular amount
of memory to that variable depending on its data type.
Typically, 32bits for Integer values etc… But if you are
looking to allocate memory while run-time, that will reduce
program’s performance.

7. No run-time dynamics
• Programmer can complete the building process as he has
given and knows all the resources needed by the program to
run. But, if program is designing memory capacities and
other things while run time, potential for generating an
error is much higher.

Disadvantages of Option Explicit:

1. Declaring variable names and its data types and all
other things take some time. It is not just about the
coding; programmer must design it at the specification stage
of the program. (That is even before he sit before an
computer to code the program)

2. After declaring an variable, the variable can have
fixed Range of values. This limits the productivity or the
usability of a variable, because when we don’t need the
existing value in a variable, still we can’t assign another
value which is not similar to variables’ data type.

3. When assigning values to declared variables, compiler
or interpreter need to check whether the input matches the
data type. This slows down the processing.

However, importance of ‘Option Explicit’ Statement has
increased by providing alternative mechanisms on ‘Option
Explicit’ disadvantages. For example, newer VB versions have
integer data types in different modes – 16 bit, 32 bit etc…

H.W Thushara Indika from Sri Lanka

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is there anyway to automatically update the Datasource name in Database Checkpoints object when we migrate tests to a new release?

3267


Hi How to retrieve data from web element line by line?

1569


Can we create a qtp test from qc?

582


i need to add 1000 users in a domain i written the script for that and it is running successfuly but the problem is that my system is not the user of that domain in which i have to add users so for adding users in that domain i have to install qtp on that another system, can anybody suggest me the way to add those users from my system ultimately i want to know how to access remote computer through script

1408


What is the pros and cons between QTP and Rational Robot

3276






What is exact meaning of Database Checkpoint in QTP 9.2 and what are the different types of the database check points?

2259


What is different command used in command Prompt while using QTP?

1737


give me the code to save all messages of inbox of gmail into a folder and notepad

1609


What is a recovery scenario?

565


How to do the scripting. Are there any inbuilt functions in QTP? What is the difference between them? How to handle script issues?

563


Explain the features of quick test pro(qtp)?

563


Hi all can any one give me roles and responsibilities for QTP (not WINRUNNER)

3208


What Folder Structure following in Keyword Driven Frame work?

2270


How do I generate Test Results window using descriptive method ( run time ) after my test execution process? Please have a look of my below code. ========================================================= Dim qtApp Dim qtTest Dim qtResultsOpt Set qtApp = CreateObject("QuickTest.Application") qtApp.Launch qtApp.Visible = True qtApp.Options.Run.CaptureForTestResults = "Always" qtApp.Options.Run.RunMode = "Fast" qtApp.Options.Run.ViewResults = True qtApp.Open "C:\Automation\Example", True Set qtTest = qtApp.Test qtTest.Settings.Run.OnError = "NextStep" Set qtResultsOpt = CreateObject ("QuickTest.RunResultsOptions") qtResultsOpt.ResultsLocation = "C:\Automation\Example\Res1" qtTest.Run qtResultsOpt MsgBox qtTest.LastRunResults.Status qtTest.Close Set qtResultsOpt = Nothing Set qtTest = Nothing Set qtApp = Nothing ========================================================== This code is working fine, but Test Result window is not displaying after execution. Anybody can please help me regading the same. Thanks Akshaya Madali Capgemini India Pvt Ltd 9823213538

3604


what is error and fault in terms of software quality?

1453