Jump to content

defining variables inside vs outside loop..


Recommended Posts

Assume I have an indefinite loop like so:

local $NOW
while true
    $NOW = stringformat("%02d:%02d:%02d",@HOUR,@MIN,@SEC)
    .
    .
wend

vs

while true
    local $NOW = stringformat("%02d:%02d:%02d",@HOUR,@MIN,@SEC)
    .
    .
wend


I understand that the 1st version gives "better performance" as the interpreter does not need to allocate/create $NOW each and every time it iterates.

However, I find 2nd version more manageable in terms of maintenance.
ie, I can move the loop elsewhere and everything It needs are self contained.
Likewise, it will not get modified elsewhere because it is localized within the loop.

My questions are
1. How significant is the performance degradation of defining variables within an infinite loop?
2. What is the best practice?

Thx!

Link to comment
Share on other sites

Try to run this :

Example_1()
Example_2()

Func Example_1()
    Local $iCounter = 1
    While $iCounter < 10
        ConsoleWrite("Example_1 : Value of Counter is " & $iCounter & @CRLF)
        $iCounter += 1
    WEnd
EndFunc

Func Example_2()
    While $iCounter < 10
        Local $iCounter = 1
        ConsoleWrite("Example_2 : Value of Counter is " & $iCounter & @CRLF)
        $iCounter += 1
    WEnd
EndFunc

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

the 2nd example will not execute as the variable is not defined when while $iCounter < 10 was executed.

It will not terminate as that variable is always kept at 1,2 ... 1,2 ... 1,2 indefinitely.

PS:
My question was more on defining temporary variables inside or outside of a loop, but not as the loop's condition.

Old school languages require variables to be defined at the start. however, I find that very difficult to maintain afterwards.

Edited by Burgaud
Link to comment
Share on other sites

4 hours ago, Burgaud said:

the 2nd example will not execute as the variable is not defined when while $iCounter < 10 was executed.

yes, it makes sense why this errors:

If $a Then ConsoleWrite("error")

because $a has not been assigned a value

therefore this works:

$a=True
If $a Then ConsoleWrite("error")

although this works as well, without an explicit assignment

Local $a
If $a Then ConsoleWrite("error")

which is a little odd.

Then you say, "Oh but it does make sense because if no init value is given, the statement Local $a is assigns the default of 0"

but then you have the case:

$a=True
Local $a
If $a Then ConsoleWrite("error")

???

 

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

On 4/27/2021 at 12:38 AM, Burgaud said:

However, I find 2nd version more manageable in terms of maintenance.

You know how to properly do it. If you believe that the "2nd version more manageable", ...we can have a chat in a few years maintaining disorganized code and see how this 2nd approach worked out.

 

5 hours ago, Burgaud said:

Old school languages require...

  New school is the same.

Might as well not declare a thing. You don't have to declare a thing in AutoIt. You'll get to repent in due time. Experience will teach you. ( we need a drop mic. emoji )🎤

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...