Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Should I require Option Explicit in all my scripts

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
VB 6 has a cool feature that will require the "Option Explicit" command and will plop it automatically into the General section of all scripts, meaning all variables must be declared. Should I use this function always? Is there any good reason to leave the "Option Explicit" command out of any applications that I write?
 
"Is there any good reason to leave the "Option Explicit" command out of any applications that I write?"

Absolutely not. I would always place 'Option Explicit' into my code. This will ensure that variables are defined explicitly and you won't get unexpected results from a type-o.
 
The only "good" reason to leave it out is pure laziness. When I write tiny "throw-away" programs to test code snippets, I sometimes leave it out simply because I can't be bothered to declare my variables. However, those are throw-away programs. If the code is important to you at all, you should always use it.
 
In my experience, 'option explicit' is a very valuable tool for debugging - I code very quickly, but don't do so well on typing - i often misspell a variable name, just by typing in a hurry. The 'option explicit' flags undefined variables, and most often they are just typos on my part. Without it, you might spend some frustrating debugging time before you notice that you have used dbfFileName in one place, and dbfFileNane in another. Or worse, dbfTransaction instead of dbfTransactions.
In my code, I make 'option explicit' required, just to save me time and headaches.

hth

Nancy Payton
 
YES, YES, YES.

You should always use option explicit as this requires you to declare varables before you use them. I have had to debug many code modules that have not had option explicit set and believe me, it is painfull. As an real life example look at the following code:

Dim iInstallCodeID As Integer
Dim iPackageCodeID As Integer
Dim iPackagePremiumCodeID As Integer
Dim lContractID As Long
Dim iSubType As Integer
Dim dInstallCost As Double


On Error GoTo ValidateCDRRow_Err

Set rsCDRRow = New ADODB.Recordset

'**** get all CDR Row data for CDR Header ID
sSQL = "SELECT * FROM tblCDRDetail WHERE HeaderID = " & lHeaderNumber
rsCDRRow.Open sSQL, mcnCDR, adOpenForwardOnly, adLockReadOnly, adCmdText

ValidateCDRRow = True

With rsCDRRow
Do While Not .EOF
iContractID = 0
iInstallCodeID = 0

iContractID = GetContractID()

CheckContractNumber (lContractID)

You will notice that lContractId has been declared as a long
BUT iContractID has not been declared at all. iContractID is being reset and used in some instances wheras lContractID is being used in others. This is perfectly valid as iContractID will be declared as a variant.

As you can see, this was not what the developer in question intended. If he had put Option Explicit in his code he would not have been able to do it and I would not have spent over a week debugging it.

Hope this helps,

Chris Dukes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top