[Gambas-user] variable declaration everywhere ???

Rolf-Werner Eilert rwe-sse at osnanet.de
Thu Dec 13 09:15:21 CET 2018


Am 12.12.18 um 20:04 schrieb Jussi Lahtinen:
> I suggest you try how long the declaration takes (I don't think it is 
> significant). But using global/public variables for loops is bad idea.
> Just think what happens when you call function inside of some loop and 
> they both happen to use the same variable (or the function call 
> something else which do it).
> This will create bug that can be really hard to debug.
> 
> 
> Jussi

100 % agreed, and that was not what I meant. What I meant are variables 
like those for images

Dim myImage as Image

for ...
.
.
	myImage = New Image...
.
.
next

I have an Image in a TableView_Data event, and I know how often it is 
called under runtime... In this case, I would rather choose a general 
name for the variable and declare it once for all functions that can 
need it.

Public myImage as New Image

Public Sub TableView_Data()

	myImage = ...

End

(When I looked it up for this mail, I saw I did not declare it that way 
but directly within the Data event, like above, but without the 
for...next loop of course. It runs well that way, so maybe the 
discussion is a bit academical ;) )

Regards
Rolf


> 
> On Wed, Dec 12, 2018 at 11:36 AM Rolf-Werner Eilert <rwe-sse at osnanet.de 
> <mailto:rwe-sse at osnanet.de>> wrote:
> 
>     Doesn't that slow down a bit?
> 
>     I know, on today's CPUs it may not be an instant drawback, but just
>     consider what happens if such a function is called hundreds or thousand
>     of times whithin a larger scope. Then redefining the variable will
>     quickly grow exponentially.
> 
>     Just image TableView_Data, which rebuilds the whole table whenever the
>     user moves something on the screen. Each time the Data event is called
>     for every single cell of the table. In cases where I need such
>     variables
>     there, I define them globally and just re-use them.
> 
>     In my code, I define counting variables in the function they are used
>     in. But object variables which are used very frequently I define
>     once on
>     module level and reuse them rather than defining them over and over
>     again. It's this feeling that it takes a lot of pushing values forth
>     and
>     back, storing, deleting, garbage collection stuff... I just feel more
>     secure knowing that a variable is redefined 20 times than 20,000 times
>     during runtime.
> 
>     Regards
>     Rolf
> 
> 
> 
> 
>     Am 12.12.18 um 00:38 schrieb Jussi Lahtinen:
>      > Every time you redefine it, it is redefined. And depending on
>      > implementation the variable could be valid only inside of the
>     loop. If
>      > not, then it is what it is defined.
>      >
>      > For idx as integer = 0 to things.max
>      >     dosomething(thing[idx])
>      > next
>      >
>      > Above idx is integer.
>      >
>      > ...
>      > For idx = 0 to others.max
>      >     dosomthingelse(others[idx])
>      > next
>      >
>      > Above idx is either undeclared or integer, depending on
>     implementation.
>      >
>      > ...
>      > For each idx as float in floatarray
>      >     dowhatever(idx)
>      >
>      > Above idx is float.
>      >
>      >
>      > Jussi
>      >
>      >
>      > On Wed, Dec 12, 2018 at 1:31 AM Bruce <adamnt42 at gmail.com
>     <mailto:adamnt42 at gmail.com>
>      > <mailto:adamnt42 at gmail.com <mailto:adamnt42 at gmail.com>>> wrote:
>      >
>      >     Well, I haven't tried it yet but a few things pop into my mind.
>      >
>      >     Consider a method that has lots of use of "throw away"
>      >     indexers/iterators,
>      >     For idx as integer = 0 to things.max
>      >         dosomething(thing[idx])
>      >     next
>      >     ...
>      >     For idx = 0 to others.max
>      >         dosomthingelse(others[idx])
>      >     next
>      >     ...
>      >     For each idx as float in floatarray
>      >         dowhatever(idx)
>      >
>      >     etc
>      >     Is it going to redefine idx each time?
>      >
>      >     b
>      >
>      >     On 12/12/18 9:46 am, Benoît Minisini wrote:
>      >      > Le 12/12/2018 à 00:11, Jussi Lahtinen a écrit :
>      >      >> Why would it be harder to debug?
>      >      >> David gave great example of it usage (pretty much only
>     case where I
>      >      >> use it) and I think it only makes code more readable and
>     less scope
>      >      >> for the variable.
>      >      >>
>      >      >>
>      >      >> Jussi
>      >      >>
>      >      >
>      >      > Because as soon as variables can be declared anywhere, people
>      >     will start
>      >      > to use that for writing ugly code. :-)
>      >      >
>      >
>      >     ----[ Gambas mailing-list is hosted by
>     https://www.hostsharing.net ]----
>      >
>      >
>      >
>      > ----[ Gambas mailing-list is hosted by
>     https://www.hostsharing.net ]----
>      >
> 
> 
>     ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 
> 
> 
> ----[ Gambas mailing-list is hosted by https://www.hostsharing.net ]----
> 



More information about the User mailing list