[Gambas-user] Variable reinitialized at each iteration of a loop structure

Christof Thalhofer chrisml at deganius.de
Wed Mar 29 09:04:17 CEST 2023


Am 29.03.23 um 01:48 schrieb Jussi Lahtinen:

> You made a very good point. But normally "Dim p as Integer" also
> initializes the variable, and within the loop this is the only thing left
> to do after the initial declaration (or raise error that variable is
> already declared). So, this is up to interpretation on what characteristics
> you are relying on. No logical resolution.

Hmm ... I understand, but I'm not really convinced.

So let's see what 'Dim p as Integer' anywhere in the code but not at the 
top is good for:

Either you (not you, I mean me as I was a bad one) are a bad programmer 
or you want speed and keep your program running inside a very long 
method without jumping out.

For the sake of readability you want to declare a variable at the place 
where it is needed. Maybe you have 1000 lines of code above that place 
and 1000 lines below and you think that makes sense – maybe because you 
have a lot of such variables to declare.

Gambas allows you to declare the variable anywhere and your program 
starts a loop at line 10 and repeats it with 'Next' at line 1990.

So this is the only moment where I as a programmer can imagine that I 
want to be able to declare a variable inside a loop with 'Dim p as Integer'.

If now Gambas would reinitialize the variable silently at that point to 
the content '0' it would make no sense at all to declare it at line 
1001. I would be forced to declare it at the top outside the loop.

So the only advantage of the declaration within a loop would be lost.

Does this sound logical?

> Thus now I think this works logically only, if the variable has scope
> limited to the loop where it is declared like in C. I do not like this.

I agree.

Alles Gute

Christof Thalhofer

-- 
Dies ist keine Signatur

-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230329/12d15f27/attachment.sig>


More information about the User mailing list