dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Option Explicit

  1. #1
    Join Date
    Mar 2005
    Location
    Nottingham, UK
    Posts
    665

    Option Explicit

    Hi Folks,

    I was thinking about this earlier, and I don't understand Option Explicit.

    Now, before everyone jumps to their keyboards, I understand exactly what Option Explicit does, but what I don't understand is A; why would anyone program without Option Explicit, and B; why does VB ship with Option Explicit not set to 'ON' as default?

    I guess what I'm trying to get at is does anyone actually program with Option Explicit turned off, and if not, why is it not ON by default?

    Why does Option Explicit even exist? It seems silly to even allow implicit variable declarations in code. I am not a programming history buff, but wonder if there is some programmatic historical precedent. Would love to hear members' thoughts on this.

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Option Explicit

    I believe it is because Visual Basic is like it says, one of the basic languages. Back in the good days of QuickBasic each type of variable had its own identifier. When Visual Basic hit the scene these identifiers were gone. Now each variable "should" be declared to ensure better performance and organization. Since it is an adaptation of one of the basic languages that is my guess as to why it is not a default. It is probably an add-in because of the lack of variable identifiers.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: Option Explicit

    There is no reason to not turn on this option (well, I've never heard any good reason...). If you want, there is an option you can check in the Tools -> Options menu to make the option explicit by default.

    Also, in Quick Basic, you did not have to declare variable, so maybe it's for some sort of backward compatibility (so you can use your qb module without any changes...)

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  4. #4
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    148

    Re: Option Explicit

    Quote Originally Posted by Dan_H
    It seems silly to even allow implicit variable declarations in code. I am not a programming history buff, but wonder if there is some programmatic historical precedent. Would love to hear members' thoughts on this.
    Implicit declarations can assist a programmer in keeping the memory usage of a program down. Implicit declarations allow use of a variable to be similar to the Variant datatype. At one point the variable is an integer type and later the same variable is storing a string.

    It seems silly today because there is very little, if any, worry about using up too much memory for a program. In many programming languages, including VB, when a variable is declared the system allocates and reserves memory space for storing data in that variable. The system will not release the memory storage for that variable until either the subtroutine the variable is in terminates or the program itself terminates. Back in the day, memory as well as the processing power of computers was very limited and programmers used every trick they could think of to minimize the footprint of their program. Using a lot of variables could potentially eat up all available memory very quickly, so a programmer would reuse variables (i.e. loop counters, temp variables, etc.) or truncate what data was stored in a variable (i.e. 2-digit years). The limitation of explicit variable declaration is that it has a type and only data of that type can be stored in the variable; with implicit variable declaration a variable is more flexible in how it is reused enabling a programmer to minimize further the number a variables he/she needs for their program.

    The Variant type as an explicitly declared type did not exist for early versions of BASIC so the implicit declaration was used instead and the variable became whatever type was needed for what was being stored. These days with the Variant type available and memory not as critical an issue implicit variable declarations are most likely, like JeffB said, merely a compatability feature for older code. Although, I cannot figure why implicit would be the default rather than explicit.
    Death is life's special way of telling you you're fired.

    For I do not seek to understand in order to believe, but I believe in order to understand. For I believe this: unless I believe, I will not understand. - Anselm of Canterbury (1033–1109)

  5. #5
    Join Date
    Mar 2005
    Location
    Nottingham, UK
    Posts
    665

    Re: Option Explicit

    Thanks for the informative responses guys.

    I guess now that even fairly humble machines possess an abundance of memory its difficult to even think about the variable 'penny pinching' that programmers had to perform in the days when memory was at a premium.

    I can see how valuable flexible implicit variables would have been.

    As an aside, I am not familiar with VB.net. Has Option Explicit survived as a feature in this language?

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Option Explicit

    Quote Originally Posted by Dan_H
    As an aside, I am not familiar with VB.net. Has Option Explicit survived as a feature in this language?
    It is still present in vb.net and if you don't switch it on, the compiler by default takes Option Explicit as On.

    And remember it is used to have a clear code in place.

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Talking Re: Option Explicit

    I think Alot of it goes even further back than just Quick basic or even GW basic.

    As a basic programmer of old --- (Commodore 64)
    basic was the Key programming language of the time (1980's), if you didn't want to program in Assembler..
    coding a program was a long and difficult process on these little babies
    a total of 65536 bytes of Ram - 34000 useable for basic.

    Variable declarations where never available.. Well you declared the var as you used it ...
    1) For Loop = 1 to 10 -- Loop declared as a integer
    2) data$ ="Some data" -- Data declared as a String

    and if you wanted to use Arrays then you had the 'DIM' command

    1) DIM loop(10) -- loop declared as a array of integers (0 - 10)
    2) DIM DATA$(5) -- Data declared as a array of Strings (0 - 5)

    also in those days we didn't have code editors..

    all your code was on numberd lines..

    Code:
    10 DIM LOOP(10)
    20 FOR LOOP(1) = 1 to 100
    ----
    ----
    1580 END
    if you wanted to add a line it was find a number available in there and use it..

    Boy have we got it easy today...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)