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

please give me the code for doing retesting of gmail login page using functions so the function has to give the value

1546


in my application,validation message has in japanise language.how to validate this message is appears properly or not

1526


When should I use smart identification?

604


regular expression in qtp standar check point for months from accepting months from january to december only

1479


Can we create user defined functions in qtp?

559






What is the expert view?

557


Is it possible to use two instances of UFT on the same machine? If not why?

605


In QTP TEST DIRECTOR COMBINATION WHAT IS THE PURPOSE OF TEST DIRECTOR? In Somebody says to store test cases and design test cases.?ok fine , but In test director 5.0 , QTp not launching. what u do?

1494


How is the Bitmap checkpoint different from Image checkpoint?

628


What are the features supported by UFT?

673


how to test Web application using QTP software

1335


How software tester can use constants and variables in scripts?

1689


1. What is the difference between Keyword and function in the keyword driven framework? 2. How do you associate Keywords to the script ?

991


For the below written code , i want to be stop the loop (ex:10 times), but it is focusing to first line of the code & generating error. vbwindow("vbname:=frmMain").Type micAltDwn + "6" + "2" + "D" +micAltUp 'wiil open the particular window in application vbwindow("vbname:=frmDeduction").vbbutton("vbname:=cmdNew").Click DataTable.ImportSheet"F:\QTP VBScript\Form 26-1.xls","Deduction",Global RowCount = DataTable.GetSheet("Global").GetRowCount i=RowCount For j =1 to i 'vbwindow("vbname:=frmDeduction").WinObject("nativeclass:=DataComboWndClass","abs_y:= 187").Type DataTable("Name", dtGlobalSheet) vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 187").Type DataTable("Name", dtGlobalSheet) vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 187").Type micF4 vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 187").Type micTab vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 216").Type micF4 vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 216").Type DataTable("Section", dtGlobalSheet) vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 216").Type micTab vbwindow("vbname:=frmDeduction").VbEdit("vbname:=txtAmtOfPay").Set DataTable("Amount_of_Payment", dtGlobalSheet) vbwindow("vbname:=frmDeduction").VbEdit("vbname:=txtAmtPaidDate").Set DataTable("Amount_PaidCredited_Date", dtGlobalSheet) vbwindow("vbname:=frmDeduction").VbEdit("vbname:=txtAmtPaidDate").Type micTab vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 316").Type micF4 vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 316").Type DataTable("Deduction_is", dtGlobalSheet) vbwindow("vbname:=frmDeduction").ActiveX("acx_name:=DataCombo","abs_y:= 316").Type micTab vbwindow("vbname:=frmDeduction").VbButton("vbname:=cmdSave").Click DataTable.SetNextRow Next vbwindow("vbname:=frmDeduction").VbButton("vbname:=cmdClose").Click

2643


wht is Manual Testing Frame work. Pls anybody can give appropriate answers

1600