FreeBASIC variable initialization

New to FreeBASIC? Post your questions here.
Post Reply
tick_da
Posts: 10
Joined: May 11, 2015 16:40

FreeBASIC variable initialization

Post by tick_da »

Hello.

I tried FreeBASIC and I like it.

But one feature is missing. I want FreeBASIC 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 in QB mode.

So is it possible to add such feature in FreeBASIC?
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC variable initialization

Post by fxm »

Welcome to the forum!

From your post, I gather that you use lang "qb" or "fblite" and not lang by default ("fb").
tick_da wrote:This is to prevent me from mistyping the names of the variables.
Explicitly declare the variables can highlight, at compile time, for example the typos on the variable names used (with the faulty line numbers specified in the compiler messages).
This is much better than investigate from bad behavior (perhaps not obvious to identify) at runtime!
Warning using lang "qb" or "fblite":
Per default, implicit variables are considered as Single in lang "qb", and as Integer in lang "fblite"!

By using lang "fb" (the default language), it is mandatory to declare each variable before its use.
With lang "qb" or "fblite", you can also impose the variable declarations by using 'Option Explicit' at program beginning.
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: FreeBASIC variable initialization

Post by tick_da »

fxm, I am aware that FreeBASIC in FB mode has mandatory and explicit variable declaration. But I think that this feature is much worse way of protecting me from mistyping a variable name because it does not protect me from forgetting to initialize a variable. And I have to initialize all variables anyway. Having zero as a default value can sometimes hide a programming error.

Besides, when I write something like

I = 0
F = 0.0
S = "0"

it is easy to figure out that 'I' is an integer variable, 'F' is a suitable equivalent to 'float', and 'S' is a string.

So mandatory initialization achieves almost the same type safety as mandatory declaration but with more run-time overhead. This additional overhead however is not a problem because to write a secure program I will have to check input values anyway.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: FreeBASIC variable initialization

Post by vdecampo »

Single character variable names (outside of a loop iterator) are a very poor programming practice. If you use more coherent variable names you would have less chance of using the wrong one. Best practice is to always use a variable name that makes sense to what the variable actually is.

IMHO
-Vince
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: FreeBASIC variable initialization

Post by RockTheSchock »

Arrays, variables, strings, and user defined types (UDTs) are initialized to zero or null strings by default when they are created.

To avoid the overhead of default variable initialization, the Any initializer can be used with Dim to tell the compiler to only reserve the place for the variable in memory but not initialize it, so the variable will contain garbage. In this case the programmer should not make assumptions about the initial values.
Well, there is no point in mandatory initialization, because with lang fb you must either use Dim or Var:
- Dim declaration with implicit (zero values) or explicit initialisation
- Var declaration and initialisation whose type is implied from the initializer expression
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: FreeBASIC variable initialization

Post by tick_da »

RockTheSchock, I didn't know about the VAR keyword. Now I have tried it. This is almost what I want. But the VAR keyword is redundant. Why do I have to type four additional characters 'var '? As I said earlier in simple cases the compiler can figure out the type without DIM or VAR. After all if I assign an array to a new variable name then there is a good chance that I really want a new variable in my program and I want this variable to be of array type.

And I didn't find any information how to initialize an array with the VAR keyword. Is it possible?

Also the VAR keyword will help only if I do not use INPUT keyword to get the value. If I use INPUT then I must use DIM (otherwise the strings will be treated like zero integer values). It seems inconsistent to me.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: FreeBASIC variable initialization

Post by RockTheSchock »

FreeBasic is a statically typed language like C, Java, Pascal ...
So Var and Dim are used to ensure that each variable has a type. Additionally with Dim you can define arrays.
tick_da wrote:RockTheSchock, I didn't know about the VAR keyword. Now I have tried it. This is almost what I want. But the VAR keyword is redundant. Why do I have to type four additional characters 'var '?
And I didn't find any information how to initialize an array with the VAR keyword. Is it possible.
Did you read the links I gave you? Please, read them carefully again!
http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDim
http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgVar

Initialise an array:

Code: Select all

Dim array(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}
tick_da wrote: As I said earlier in simple cases the compiler can figure out the type without DIM or VAR. After all if I assign an array to a new variable name then there is a good chance that I really want a new variable in my program and I want this variable to be of array type.
Yes, it could, thats what dynamically typed languages do. But it adds an overhead. Dynamically typed languages are often a bit slower.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: FreeBASIC variable initialization

Post by stylin »

tick_da wrote:RockTheSchock, I didn't know about the VAR keyword. Now I have tried it. This is almost what I want. But the VAR keyword is redundant. Why do I have to type four additional characters 'var '?
The general idea is that it's better to be explicit with your intent. The compiler always knows the first time you use a variable name within any given context, but as a human this isn't always clear; an assignment looks like an assignment, nothing more. "fb mode" helps to avoid mistyping variable names by requiring DIM or VAR, but it also helps to avoid the cases where you reuse a variable name by accident, changing its value and potentially breaking code further down. These two bugs were some of the most common I've encountered when reading QB code, where functions tend to be very long and global variables tend to be used extensively.
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: FreeBASIC variable initialization

Post by tick_da »

RockTheSchock, my question was about '-lang qb' mode, not about '-lang fb' mode or '-lang fblite' mode. Anyone who wants to declare all scalar variables can use '-lang qb' mode. But I don't want mandatory variable declaration because it does almost nothing to prevent me from simple mistakes. For example, take a look at the following code

dim a (5) as integer
a(50) = 666
print a(50)

It prints 666 in FreeBASIC 1.02.1. This is incorrect behavior. In Microsoft QBasic 1.1 I see the error 'Subscript out of range' and the second line is highlighted. This is correct behavior. FreeBASIC 1.02.1 behaves just like C and this make it impossible to use FreeBASIC 1.02.1 as tool for teaching beginning programmers who didn't have any prior programming experience.

As you can see mandatory variable declaration is not enough. Run-time checks are necessary.

That's why I don't want mandatory variable declaration. I want mandatory variable initialization in '-lang qb' mode.

And I already wrote about overhead. I will need to check INPUT values anyway (for security and for correctness). Also some values are unknown at compile-time. So there is really no choice about overhead.

Also, you wrote how to initialize an array with the DIM keyword. But I wrote that it is impossible to initialize an array with the VAR keyword.
stylin wrote:it also helps to avoid the cases where you reuse a variable name by accident, changing its value and potentially breaking code further down
I'm not sure what you mean. Take a look at the following code

dim a (5) as integer
a(50) = 666
a(50) = 777
print a(50)

The third line does not (and should not) produce any compilation errors.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: FreeBASIC variable initialization

Post by vdecampo »

Specify command line argument -exx to enable array bound checking.

-Vince
fxm
Moderator
Posts: 12577
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FreeBASIC variable initialization

Post by fxm »

Why you do not want to move to -lang fb, and use VAR for the scalars in order to impose their initializations?
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: FreeBASIC variable initialization

Post by tick_da »

vdecampo, thank you. I tried FreeBASIC only for a couple of days. That's why I still didn't learn all command line arguments that I need. I was wrong when I thought that FreeBASIC cannot check array bounds. But still '-exx' is not only a compile-time check it is also a run-time check. So my opinion was right in a sense that compile-time checking is not enough. I need run-time checks. That's why I need mandatory variable initialization.

The following code works as expected with the '-exx' switch

dim a (5) as integer
dim i as integer
input i
a(i) = 666
print a(i)

It gives me an error when I INPUT less than 0 or more than 5. And that's a good thing.

fxm, because then I will have to declare arrays with the DIM keyword. And I don't want to declare anything. I want to initialize. And it is not possible to initialize an array with the VAR keyword (i.e. without declaration). And also that means I have to type four additional characters per variable name.
Dean_Saurdine
Posts: 8
Joined: Dec 10, 2014 18:58

Re: FreeBASIC variable initialization

Post by Dean_Saurdine »

This answers this post and "Can I change default initial values?"

My FreeBASIC attitude is:
have_fun:
be_verbose
be_explicit
GoTo have_fun

Use fb dialect. It is the best BASIC dialect since it's 0.23 release.

oldBASIC:
DEFINT A=0
DEFINT B=1
DEFINT C=2

FreeBASIC:
Dim As Integer A = 0 _ Rem description
B = 1 _ Rem description
C = 2 Rem description

Well, I tried to align the = symbol. It is very human readable when you do.

And remember: you are in charge. You define the variable. The variable name, type and value are just human-readable versions of memory space. Assign an initial value. If you do not then the computer will "pick" a value (the machine code version) and put it into the memory space, because no memory space can be empty.

Happy coding from a person who began learning without a video monitor,
Dean Saurdine
Dean_Saurdine
Posts: 8
Joined: Dec 10, 2014 18:58

Re: FreeBASIC variable initialization

Post by Dean_Saurdine »

culpa lata

The `Preview` feature was not working for me when I tried to post the code, and I missed items in my code. Trying again... ...the code is from my template file, which saves a lot of typing and helps enforce a consistent coding style. In mathematics you have a formula followed by a symbol list. In FreeBASIC you have a name list followed by an assignment statement. Same concept, different order. Comments are free and they will matter when you review your code a decade from now.

Code: Select all

REM variable definitions
Dim As type  name = value  , _  Rem description
                      name = value  , _  Rem description
                      name = value       Rem description
Happy coding,
Dean Saurdine
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: FreeBASIC variable initialization

Post by Tourist Trap »

tick_da wrote: But one feature is missing. I want FreeBASIC to require from a programmer to always initialize all variables before first usage.
I'm not sure I'm understanding what you need, since default initialization is convenient I find. But you I think can use the ANY keyword to overpass it.
Comparison of C/C++ and FreeBASIC (documentation)
uninitialized variable
int a;
dim a as integer = any
Post Reply