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

BB adamnt42 at gmail.com
Wed Mar 29 10:14:28 CEST 2023


Inline.

On 29/3/23 5:34 pm, Christof Thalhofer wrote:
> 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,
No, from what Benoit said, the declaration does not initialize the 
variable, the runtime does.  Or in other words, a (native) variable 
declaration is not a code command to initialize it.
>> and within the loop this is the only thing left
>> to do after the initial declaration (or raise error that variable is
>> already declared). 
I think this is confusing scope with code. As Benoit said, the scope of 
the variable is the method, whether it's declared "here" or at the top 
of the method.
>> 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 r1unning inside a 
> very long method without jumping out.
>
Exactly! Being a bad programmer with a few lines of code to my name I 
have resorted to this means about 5 times in my Gambas history and 
exactly because I was too lazy to scroll back 998 lines to enter the 
declaration and then somehow go back to where I wanted to use it.

> 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. 

only 998 lines, I must not be as bad as I thought! :-)

>
> 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?

Most assuredly so. This whole conversation seems to be based around 
different interpretations of scope, declaration and initialisation. So 
here's my 20c

1. The scope of a variable in a method is the method. IMMUTABLE! If you 
want a different variable then use one.

2. Declaration and initialization are two very different things. And 
even if natives are initialised by the interpreter if not done 
explicitly, there is no reason to expect it. (I never trust booleans to 
be false by default by the way!)

3. Syntactic sugar, although wonderful and demanded by us coders with 
almost too much Gambas experience, is a trap!  Is a newbie expected to 
understand that declaring an object variable with the NEW option is 
shorthand! i.e.

     Dim Xyzzy as New Thingo

is

     Dim Xyzzy as Thingo
     Xyzzy = New Thingo

Source code and runtime code are very different things. I believe I 
learned that the hard way back in the 1970s trying to understand why the 
machine code did not match my assembler code. (It was in fact a weird 
situation where the assembler coded a specific assembler construct as a 
NOP. When I finally understood why, it took me several weeks to explain 
that to the system vendor's engineers. It wasn't a bug, it was more of a 
design error in the machine architecture. But it did have several highly 
qualified people in the day scratching their heads. The machine may or 
may not have been one that rhymes with, well, Rhyme.)

>
>> 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.
Me too, we do not want scope limited by ill expected code structures. 
Unless Gambas should look like Algol. Arrrrggh! Or even worse python (spew).
>
> Alles Gute
>
> Christof Thalhofer
>
Almost finally, I want to mention "locally defined" loop control 
variables, as in     For X as Integer From Y to Z ...     Here too is a 
trap for the unwary.

When Benoit, allowed the introduction of this code construct he was very 
explicit in explaining that the scope of the variable was still the 
method, not the loop. I believe that is still said somewhere in the 
wiki, I just can't find it at the moment. But this is a good thing as I 
can rely on the value of the control variable after the loop is exited, 
as in when processing an unknown number of items inside the loop,

     For idx as Integer From 0 to 1000000
         ...
         If <something> Then Break
         ...
     Next

     Print X ' or whatever depending on what X is.


and now finally! This conversation initially got me a bit worried 
because I use the following code structure a fair bit in 
GridView_Click() handlers.

     Public Sub ThisGrid_Click()

         If ThisGrid.Row < 0 Then Return ' the user has not selected one 
of the rows

         Dim SomeObject as New Something
         ... blah blah etc


as object instantiation can sometimes be expensive and I wanted 
"efficient" code. Then I realised that, of course, what I was actually 
coding was


    Public Sub ThisGrid_Click()

         {Dim SomeObject as Something}

         If ThisGrid.Row < 0 Then Return ' the user has not selected one 
of the rows

         {SomeObject = New Something}
         ... blah blah etc


So even "smart alec" coders like moi can get confused by these pieces of 
sugar!


cheers

bruce



More information about the User mailing list