locked
initialization issue RRS feed

  • Question

  • I was about to give an introductory programming course using Small Basic. I encountered following issue while playing with it (v1.1 on Windows 8.1 x64):

    the single liner below does not compile and display the error "the variable x is used but its value is not assigned", which is expected.

    y = x+1

    the following single liner however compiles w/o error, and automagically assigns 0 as the initial value for x:

    x = x+1
    is this expected? if yes, how do you explain it?

    Monday, July 20, 2015 4:32 PM

Answers

  • Hi SeyfiD,

    This does look like a bug, and probably stems from how Small Basic's variables work.

    All variables are always available, even if they were declared in a subroutine/If-Then/loop and haven't been assigned to yet. For example, this will print '-5' to the text window:

    TextWindow.WriteLine(x - 5)
    
    Sub Test
      x = 10
    EndSub

    As all variables are always available, they contain the "uninitialised" value up until something assigns to them. Small Basic doesn't have concrete data types (like strings and integers and decimals), but has a single data type ("Primitive") that handles cases for all three situations. This means that the uninitialised value varies depending on how you use the variable: use it as a string, uninitialised is empty string (""); use it as an integer, the value is 0; and use it as a decimal, the value is 0.0.

    In the case of your code, the compiler is probably recognising that 'x' is assigned to, so it creates 'x' without checking what value it is assigned. The compiler has to find all variables first because you can use a variable "before" (lexicographically) you assign to it. If it didn't, it wouldn't be able to recognise uses of variables before they're assigned to. Doing it this way was likely done to simplify the compiler.

    After the compiler's found all the variables, it probably then goes through the program again, checking to make sure that all variable references are correct. Because it constructed the list of variables separately, it doesn't recognise that the assignment in your example should be invalid, and instead allows it. When the code is executed, 'x' is treated as an integer (because it's being added to '1', another integer) and the uninitialised value '0' is used in the addition.

    Summary:

    • Bug? Yes.
    • Serious? Not particularly.
    • Fixable? Yes, but it would require a compiler update and would be a breaking change (older programs may become broken).

    I imagine, if reported, the Small Basic development team would consider fixing it, but it's not a show-stopper or otherwise high-priority bug, so the fix would likely be slow in coming.

    • Proposed as answer by litdev Monday, July 20, 2015 7:35 PM
    • Marked as answer by SeyfiD Monday, July 20, 2015 8:59 PM
    Monday, July 20, 2015 5:25 PM
  • Hello SefiD,

    There is following description in FAQ of Small Basic.

    What are the unique features of the Small Basic language?

    Variables

    All variables are global and are always initialized. They can be used before they're assigned.

    From this description, I think:

    • All variables are initialized as "".  (And "" is treated as 0 in math operations.)
    • But at compile time, all variables are checked whether they are initialized or not.  And if a variable is not initialized, Small Basic shows compile error at the bottom of the editor.

    Nonki Takahashi


    Wednesday, July 22, 2015 1:01 AM

All replies

  • (translated by Google translator)

    I think that in the second case, when the compiler sees a new variable "x" to the left of the sign "=", the compiler initializes it and sets a value of zero. Then, the compiler go to the right of the sign "=" and sees the variable "x" which already has a value of zero. That's why there is no error.

    Monday, July 20, 2015 5:14 PM
  • Hi SeyfiD,

    This does look like a bug, and probably stems from how Small Basic's variables work.

    All variables are always available, even if they were declared in a subroutine/If-Then/loop and haven't been assigned to yet. For example, this will print '-5' to the text window:

    TextWindow.WriteLine(x - 5)
    
    Sub Test
      x = 10
    EndSub

    As all variables are always available, they contain the "uninitialised" value up until something assigns to them. Small Basic doesn't have concrete data types (like strings and integers and decimals), but has a single data type ("Primitive") that handles cases for all three situations. This means that the uninitialised value varies depending on how you use the variable: use it as a string, uninitialised is empty string (""); use it as an integer, the value is 0; and use it as a decimal, the value is 0.0.

    In the case of your code, the compiler is probably recognising that 'x' is assigned to, so it creates 'x' without checking what value it is assigned. The compiler has to find all variables first because you can use a variable "before" (lexicographically) you assign to it. If it didn't, it wouldn't be able to recognise uses of variables before they're assigned to. Doing it this way was likely done to simplify the compiler.

    After the compiler's found all the variables, it probably then goes through the program again, checking to make sure that all variable references are correct. Because it constructed the list of variables separately, it doesn't recognise that the assignment in your example should be invalid, and instead allows it. When the code is executed, 'x' is treated as an integer (because it's being added to '1', another integer) and the uninitialised value '0' is used in the addition.

    Summary:

    • Bug? Yes.
    • Serious? Not particularly.
    • Fixable? Yes, but it would require a compiler update and would be a breaking change (older programs may become broken).

    I imagine, if reported, the Small Basic development team would consider fixing it, but it's not a show-stopper or otherwise high-priority bug, so the fix would likely be slow in coming.

    • Proposed as answer by litdev Monday, July 20, 2015 7:35 PM
    • Marked as answer by SeyfiD Monday, July 20, 2015 8:59 PM
    Monday, July 20, 2015 5:25 PM
  • I just tried those one-liners as VBScript (WSH) without Option Explicit: it did not even complain about the first one, both passed. All this seems confusing for newbies. I was planning  Small Basic->VBscript->VBA path as curriculum, but I'll probably pass Basic altogether and think of something else.

    Monday, July 20, 2015 9:27 PM
  • Hello SefiD,

    There is following description in FAQ of Small Basic.

    What are the unique features of the Small Basic language?

    Variables

    All variables are global and are always initialized. They can be used before they're assigned.

    From this description, I think:

    • All variables are initialized as "".  (And "" is treated as 0 in math operations.)
    • But at compile time, all variables are checked whether they are initialized or not.  And if a variable is not initialized, Small Basic shows compile error at the bottom of the editor.

    Nonki Takahashi


    Wednesday, July 22, 2015 1:01 AM
  • I think this is by design. I guess the theory is to display errors as little as possible.

    Ed Price, Azure & Power BI Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Wednesday, July 22, 2015 6:02 AM

    • All variables are initialized as "".  (And "" is treated as 0 in math operations.)
    • But at compile time, all variables are checked whether they are initialized or not.  And if a variable is not initialized, Small Basic shows compile error at the bottom of the editor.

    Nonki Takahashi


    Really? you just made 2 contradicting statements. If they're all initialized then why the need to check later if they're initialized or not? nonsense.

    The implementors should make a decision: will SB behave like there's an Option Explicit or not? I'd vote without it, so the error should go away (yes, I reverse the assertment in my initial post: error is not expected, just make it work as it does in VBScript, VBA w/o Option Explicit)


    • Edited by SeyfiD Wednesday, July 22, 2015 1:10 PM typo
    Wednesday, July 22, 2015 1:09 PM
  • SeyfiD,

    So what would your first example yield if there was no error?

    y = x+1

     

    Would they both result in 0?

    Thanks!


    Ed Price, Azure & Power BI Customer Program Manager (Blog, Small Basic, Wiki Ninjas, Wiki)

    Answer an interesting question? Create a wiki article about it!

    Thursday, July 23, 2015 4:49 PM
  • There are some pretty strong opinions being made here!

    BASIC was first and foremost a beginner's language, intended to be encountered in an educational setting. Small Basic is a wonderful modern incarnation of this 50-year old language and IMHO is rightly forgiving of many beginner's errors that would otherwise quickly disenchant young learners. I believe there is just about the right level of syntax checking and is certainly preferable to other languages, (e..g Python), even thought the latter is undoubtedly a more powerful language. But I digress!

    It is up to the 'teacher' to instil good programming habits in the 'learner' and this includes proper initialisation of all variables. Small Basic is 'kind' to programmers who forget to do this, and so I disagree that this is a 'bug'. Unexpected maybe, but that's the challenge of learning any language. (Have you ever tried locating a wrongly-indented line in a student's Python program and then successfully explained to said student why Python works that way?!!)

    Thursday, July 23, 2015 8:30 PM