Menu

#2 Bywater BASIC variable initialization

open
nobody
None
5
2017-07-04
2015-05-12
Serge
No

I want Bywater BASIC to require from a programmer to always initialize all variables before first usage. The code like this

PRINT A

or

B = B + 1

should be illegal but the code like

A = 0
PRINT A

or

B = 0
B = B + 1

or

INPUT B
B = B + 1

should be OK. So the first time when some variable is used it should always be on the left side of the assignment.

This is to prevent me from mistyping the names of the variables.

Of cause I do not expect that this will be the default behavior. I guess the best way is to make a command-line switch that enables such behavior.

So is it possible to add such feature in Bywater BASIC?

Discussion

  • Marcos Cruz

    Marcos Cruz - 2017-06-28

    Version 3.20 includes CONST, which is exactly what you need.

     
  • Serge

    Serge - 2017-06-29

    Marcos Cruz, you are wrong. My feature request has nothing to do with the constansts. It is about variables.

     
  • Marcos Cruz

    Marcos Cruz - 2017-06-30

    You are right. The wording of the manual confused me:

    SYNTAX: CONST variable [, ...] = value
    DESCRIPTION: Assigns the value to variable.  Any later
                 assignment to the variable causus a VARIABLE
                 NOT DECLARED error.
    

    I misunderstood const inits a variable to prevent it from being used without initialization, i.e. before being declared withconst... which is nonsense :)

    What about option explicit?: With this option, all variables must be declared using dim.

     
  • Serge

    Serge - 2017-06-30

    Marcos Cruz, I do not like OPTION EXPLICIT because it requires from me to provide the same information (about the type of the variable) at least three times: (1) when I declare a variable, (2) when I initialize it, (3) when I use it.

    Also, it still does not help me to make sure that all variables in my program are initialized properly.

    When a variable is not initialized it is almost always a mistake. Except for the few cases when I really want some random data from the memory.

    Searching for a bug like this (a bug caused by an uninitialized variable) can take a lot of time.

    Please note that when you assign a value you also provide type information.

    A = 0
    A = 0.0
    A = "0"

     

    Last edit: Serge 2017-06-30
  • AF5NE

    AF5NE - 2017-07-04

    OPTION TYPO ON has been added for the next release of Bywater BASIC.
    When enabled, any attempt to read from a variable that was not previously written generates an error. The default is OPTION TYPO OFF for all dialects currently supported by Bywater BASIC.

     

Log in to post a comment.