none
Revisiting variable declaration RRS feed

  • Question

  • I teach beginning programming and would love to use Small Basic, but I would at least have to have "Option Explicit" to force declaration.  I know this has been discussed in the past, but any thoughts of adding it in the future?

    Fred


    Fred Guyton

    Monday, January 2, 2017 7:33 PM

Answers

  • Hi Fred,

    1. There are no types in Small Basic, and all variables are global scope.
    2. A compiler error occurs if a variable is used before set (sometimes this fails if it is used in a sub).
    3. If the compiler does't catch an uninitialised variable, it defaults to 0 or "" depending on if the context is number or string.

    Its my understanding that these decisions were deliberate to keep coding simple.  It does limit writng complex code (actualy forces extra care and organisation), but by then coders should be moving on with an understanding of why type, variable scope, multi files, classes, access modifiers etc have a benefit isolating code and variables and hence writing good code.

    So with all this in mind what would Option Explicit add or do?

    • Marked as answer by YLedEditor Tuesday, January 3, 2017 2:06 PM
    Monday, January 2, 2017 8:01 PM
    Moderator
  • Quickly tried and cannot see a difference between SB and eg. "Option Explicit" from VBS. But perhaps overseen something. Best try yourself different variants.

    'Script56.chm' (VB Scripting Edition  Option Explicit Statement) says:
    Forces explicit declaration of all variables in a script.
    When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements.
    If you attempt to use an undeclared variable name, an error occurs.
    Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.

    In SB:
    Anywhere in the code (even later in Sub or anywhere else), every used variable has to be assigned with a value  var ← value to compile.
    SB declares a used variable by assigning a value or "" to it.

    y = x
    'Assign()
    TextWindow.WriteLine("y= "+ y +" | x= "+ x)
    
    Sub Assign
      x = y ' 5
    EndSub
    

    Quick VBS sample for comparison:

    Option Explicit
    'Dim x, y
    y = x
    x = 5
    WScript.Echo "y: " & y & vbCrLf & "x: " & x
    'y = x
    Dim x, y
    

    • Marked as answer by YLedEditor Tuesday, January 3, 2017 2:05 PM
    Monday, January 2, 2017 10:40 PM
    Answerer

All replies

  • Hi Fred,

    This issue was actually did an article on this forum:

    see:

    https://social.msdn.microsoft.com/Forums/en-US/9aa087df-c0da-418b-95ac-ed399b2fb9e5/initialization-issue?forum=smallbasic

    I like Ed Price and Nonki answer.

    There is many answers , but  Midlandman2 is in my point of view the best answer.

    Monday, January 2, 2017 7:53 PM
    Answerer
  • Hi Fred,

    1. There are no types in Small Basic, and all variables are global scope.
    2. A compiler error occurs if a variable is used before set (sometimes this fails if it is used in a sub).
    3. If the compiler does't catch an uninitialised variable, it defaults to 0 or "" depending on if the context is number or string.

    Its my understanding that these decisions were deliberate to keep coding simple.  It does limit writng complex code (actualy forces extra care and organisation), but by then coders should be moving on with an understanding of why type, variable scope, multi files, classes, access modifiers etc have a benefit isolating code and variables and hence writing good code.

    So with all this in mind what would Option Explicit add or do?

    • Marked as answer by YLedEditor Tuesday, January 3, 2017 2:06 PM
    Monday, January 2, 2017 8:01 PM
    Moderator
  • Also , i remember,  i was using Implicit declaration , in my old Basic Tandy Computer of the 80's:

    like this:

    QBasic has five built-in types: INTEGER (%), LONG(&) integer, SINGLE(!) float, DOUBLE(#) float and STRING($).

    Implicit declaration is by adding the type character to the end of the variable name (%, &, !, #, $).

    see:  WIKIBOOKS: QBasic/Arrays and Types

    Monday, January 2, 2017 8:26 PM
    Answerer
  • Quickly tried and cannot see a difference between SB and eg. "Option Explicit" from VBS. But perhaps overseen something. Best try yourself different variants.

    'Script56.chm' (VB Scripting Edition  Option Explicit Statement) says:
    Forces explicit declaration of all variables in a script.
    When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements.
    If you attempt to use an undeclared variable name, an error occurs.
    Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.

    In SB:
    Anywhere in the code (even later in Sub or anywhere else), every used variable has to be assigned with a value  var ← value to compile.
    SB declares a used variable by assigning a value or "" to it.

    y = x
    'Assign()
    TextWindow.WriteLine("y= "+ y +" | x= "+ x)
    
    Sub Assign
      x = y ' 5
    EndSub
    

    Quick VBS sample for comparison:

    Option Explicit
    'Dim x, y
    y = x
    x = 5
    WScript.Echo "y: " & y & vbCrLf & "x: " & x
    'y = x
    Dim x, y
    

    • Marked as answer by YLedEditor Tuesday, January 3, 2017 2:05 PM
    Monday, January 2, 2017 10:40 PM
    Answerer
  • If sub is called before variable is declared no compiler error is given and default value used.

    sub1()
    x = 1
    Sub sub1
      y = x+1 'x is default value 0
      TextWindow.WriteLine(y)
    EndSub

    Additionally unset array values will default "" (or 0).

    Monday, January 2, 2017 10:45 PM
    Moderator