<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.autoitscript.com/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Prime03</id>
	<title>AutoIt Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.autoitscript.com/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Prime03"/>
	<link rel="alternate" type="text/html" href="https://www.autoitscript.com/wiki/Special:Contributions/Prime03"/>
	<updated>2026-05-14T13:34:32Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12328</id>
		<title>Best coding practices</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12328"/>
		<updated>2014-03-20T11:08:03Z</updated>

		<summary type="html">&lt;p&gt;Prime03: /* Scopes of Variables */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what is to be considered the best coding practices within AutoIt.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* &amp;quot;(ND)&amp;quot; stands for &amp;quot;Not decided&amp;quot;, meaning that the writer decided himself the best coding practice and it has to be validated by another MVP/a Developer.&lt;br /&gt;
* You must use the latest beta to run the below examples.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is adopted however it&#039;s simplified and regroup all the types of numbers in one type.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! initialization&lt;br /&gt;
|-&lt;br /&gt;
| i || Numbers (any type) || $i = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $a = 0 or $a[0]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $s = &amp;quot;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| f || Booleans || $f = False or $f = True &lt;br /&gt;
|-&lt;br /&gt;
| b || Binaries || $b = &amp;quot;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $h = 0&lt;br /&gt;
|-&lt;br /&gt;
| k (ND) || Keywords || Null (ND)&lt;br /&gt;
|-&lt;br /&gt;
| fu (ND) || Functions || Null (ND)&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = &amp;quot;somedef&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0&lt;br /&gt;
|-&lt;br /&gt;
| v || Various || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| i || f || MyFlag&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a Local variable the number 5.&lt;br /&gt;
Local $iSomeVar = 5&lt;br /&gt;
&lt;br /&gt;
; Assign a Local variable the number 1.&lt;br /&gt;
Local $ifMyFlag = 1&lt;br /&gt;
&lt;br /&gt;
; Assign a Local variable an array of numbers.&lt;br /&gt;
Local $aiArray = 0&lt;br /&gt;
&lt;br /&gt;
; Re-declare the array to size and fill it.&lt;br /&gt;
Local $aiArray[3] = [0, 0.25, 3 / 4]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Always initialize a variable on it&#039;s declaration, and declare all the same type of variable on the same line.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
;Bad&lt;br /&gt;
Local $iSomeVar1, $aiArray = 0&lt;br /&gt;
Local $iSomeVar2 = 5&lt;br /&gt;
&lt;br /&gt;
;Good&lt;br /&gt;
Local $iSomeVar1 = 0, $iSomeVar2 = 5&lt;br /&gt;
Local $aiArray = 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You can still categorize your variables and declare for example the $iSomeVar2 on another line.&lt;br /&gt;
&lt;br /&gt;
Example2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;Constants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Local $vValue ; Bad, this is initially blank.&lt;br /&gt;
Local $vVal = 0 ; Good&lt;br /&gt;
&lt;br /&gt;
MsgBox($MB_SYSTEMMODAL, &#039;&#039;, &#039;$vValue: &#039; &amp;amp; $vValue &amp;amp; @CRLF &amp;amp; &#039;$vVal: &#039; &amp;amp; $vVal)&lt;br /&gt;
&lt;br /&gt;
; In C++ $vValue would display the previous value that was residing in the memory address $vValue points to. AutoIt&lt;br /&gt;
; is a little less forgiving in that it initialises the variable as being blank and doesn&#039;t throw any error, unlike some moder C++ compilers.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Scopes of Variables ==&lt;br /&gt;
To make the transition, the variables are also named according to their scope.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Global UDF variable !! Global variable !! Local variable&lt;br /&gt;
|-&lt;br /&gt;
| $__iSomeVar or $g__iSomeVar || $_iSomeVar or $g_iSomeVar || $iSomeVar&lt;br /&gt;
|}&lt;br /&gt;
With this method, you will avoid non wanted re-assignments.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
; Assign a Global variable the number 0.&lt;br /&gt;
Global $iSomeVar1 = 0&lt;br /&gt;
; Assign a Global variable the number 5.&lt;br /&gt;
Global $_iSomeVar2 = 5&lt;br /&gt;
&lt;br /&gt;
_SomeFunc()&lt;br /&gt;
&lt;br /&gt;
Func _SomeFunc()&lt;br /&gt;
	; Assign Local variables respectively the numbers 3 and 4.&lt;br /&gt;
	Local $iSomeVar1 = 3, $iSomeVar2 = 4&lt;br /&gt;
&lt;br /&gt;
	; Note: The user inadvertently re-assigned the global variable $iSomeVar1, because this one is not named as &amp;quot;global&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
	; Display the value of $iSomeVar1.&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;Value of $iSomeVar1: &amp;quot; &amp;amp; $iSomeVar1)&lt;br /&gt;
&lt;br /&gt;
	; Display the value of $iSomeVar2.&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;Value of $iSomeVar2: &amp;quot; &amp;amp; $iSomeVar2)&lt;br /&gt;
&lt;br /&gt;
	; Display the value of $_iSomeVar2.&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;Value of $_iSomeVar2: &amp;quot; &amp;amp; $_iSomeVar2)&lt;br /&gt;
EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&lt;br /&gt;
Always declare your global variables in the global scope, not in the functions. It will prevent another function to use it before its declaration and the declaration is implicit (see [[#jumpsec1|examples]]).&lt;br /&gt;
&lt;br /&gt;
* A variable declared locally (with the Local keyword), has a visibility which depends of the scope where it&#039;s declared.&lt;br /&gt;
:Declaration in the global scope: the variable is nonetheless visible everywhere; declare it as Local if this one is only used in the same scope.&lt;br /&gt;
:Declaration in a function: the variable is visible by the function itself and nowhere else.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Structure of a code scope:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Global scope.&lt;br /&gt;
&lt;br /&gt;
; Include the Constants file, it contains various constants; it&#039;s needed here for the $MB_SYSTEMMODAL flag of the MsgBox function).&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
; This scope is either Global or Local, depending on where do you use the variables.&lt;br /&gt;
&lt;br /&gt;
; Assign a Global variable the number 0 (which corresponds to an initialization of a variable number), its scope is Global because it&#039;s used at least in one function.&lt;br /&gt;
Global $_iVar1 = 0&lt;br /&gt;
&lt;br /&gt;
; Assign a Local variable the string &amp;quot;foo&amp;quot;, its scope is Local because it&#039;s use is restricted to this scope.&lt;br /&gt;
Local $_sVar2 = &amp;quot;foo&amp;quot;&lt;br /&gt;
&lt;br /&gt;
; Display the content of $_sVar2&lt;br /&gt;
MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;Value of $sVar2: &amp;quot; &amp;amp; $_sVar2)&lt;br /&gt;
&lt;br /&gt;
; Re-assign a Local variable the string returned by the function _MyFunc1.&lt;br /&gt;
$_sVar2 = _MyFunc1()&lt;br /&gt;
&lt;br /&gt;
; Re-Display the content of $_sVar2&lt;br /&gt;
MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;Value of $sVar2: &amp;quot; &amp;amp; $_sVar2)&lt;br /&gt;
&lt;br /&gt;
; Declare a function (its main utility is described later in Functions, we can see one here which is to create a Local scope).&lt;br /&gt;
Func _MyFunc1()&lt;br /&gt;
	; Local scope.&lt;br /&gt;
&lt;br /&gt;
	; Display the content of $_iVar1.&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;Value of $_iVar1: &amp;quot; &amp;amp; $_iVar1)&lt;br /&gt;
&lt;br /&gt;
	; Assign a Local variable the string &amp;quot;bar&amp;quot;, its scope is Local because it&#039;s use is restricted to the function&#039;s scope.&lt;br /&gt;
	Local $sVar3 = &amp;quot;bar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
	; Display the content of $sVar3.&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;Value of $sVar3: &amp;quot; &amp;amp; $sVar3)&lt;br /&gt;
&lt;br /&gt;
	; Return the $sVar3 content, it will be visible (if used) to the scope where the function is called.&lt;br /&gt;
	Return $sVar3&lt;br /&gt;
EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Concerning the Dim keyword, its recommended usage is limited to empty an existing array (Example 1) or to redeclare a function parameter (Example 2).&lt;br /&gt;
&lt;br /&gt;
Example 1:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Include the Array UDF, it&#039;s needed here for the _ArrayDisplay function.&lt;br /&gt;
#include &amp;lt;Array.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
; Assign a Local variable an array containing numbers with a size of 5.&lt;br /&gt;
; Note than an array is based 0 index, to access the first element the code is: $aiArray[0].&lt;br /&gt;
Local $aArray[5] = [1, 2, 3, 4, 5]&lt;br /&gt;
 &lt;br /&gt;
; Display the contents.&lt;br /&gt;
_ArrayDisplay($aArray)&lt;br /&gt;
 &lt;br /&gt;
; Empty the array (and keep its size).&lt;br /&gt;
Dim $aArray[5]&lt;br /&gt;
 &lt;br /&gt;
; Display the contents.&lt;br /&gt;
_ArrayDisplay($aArray)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Remark: The variable type of the emptied array is a string, every variable non initialized is a string.&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;Array.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
; Call _MyFunc1 with default parameters ($vParam1 = 0).&lt;br /&gt;
_MyFunc1()&lt;br /&gt;
&lt;br /&gt;
; Assign a Local variable an array containing integers.&lt;br /&gt;
Local $aiArray[3] = [3, 4, 5]&lt;br /&gt;
; Call _MyFunc1 with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
_MyFunc1($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func _MyFunc1($vParam1 = 0)&lt;br /&gt;
    ; If $vParam1 is NOT an array then redeclare it to an array.&lt;br /&gt;
    If IsArray($vParam1) = 0 Then&lt;br /&gt;
        Dim $vParam1[3] = [0, 1, 2]&lt;br /&gt;
    EndIf&lt;br /&gt;
&lt;br /&gt;
    ; Display the array.&lt;br /&gt;
    _ArrayDisplay($vParam1)&lt;br /&gt;
EndFunc   ;==&amp;gt;_MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And for the ReDim keyword, limit its use to resize an array you want to keep its content:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Include the Array UDF, it&#039;s needed here for the _ArrayDisplay function.&lt;br /&gt;
#include &amp;lt;Array.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
; Assign a Local variable an array containing numbers with a size of 5.&lt;br /&gt;
; Note than an array is based 0 index, to access the first element the code is: $aiArray[0].&lt;br /&gt;
Local $aArray[5] = [1, 2, 3, 4, 5]&lt;br /&gt;
 &lt;br /&gt;
; Display the contents.&lt;br /&gt;
_ArrayDisplay($aArray)&lt;br /&gt;
 &lt;br /&gt;
; Resize the array (and keep its content).&lt;br /&gt;
ReDim $aArray[3]&lt;br /&gt;
 &lt;br /&gt;
; Display the contents.&lt;br /&gt;
_ArrayDisplay($aArray)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Why using Dim over Local/Global is not always a good option:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
Dim $vVariableThatIsGlobal = &amp;quot;This is a variable that has &amp;quot;&amp;quot;Program Scope&amp;quot;&amp;quot; aka Global.&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, &amp;quot;An example of why Dim can cause more problems than solve them.&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
Example()&lt;br /&gt;
 &lt;br /&gt;
Func Example()&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &amp;quot;&amp;quot;, $vVariableThatIsGlobal) ; That looks alright to me as it displays the following text: This is a variable that has &amp;quot;Program Scope&amp;quot; aka Global.&lt;br /&gt;
 &lt;br /&gt;
	Local $vReturn = SomeFunc() ; Call some random function.&lt;br /&gt;
 &lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, $vReturn, $vVariableThatIsGlobal) ; The Global variable ($vVariableThatIsGlobal) changed because I totally forgot I had a duplicate variable name in &amp;quot;SomeFunc&amp;quot;.&lt;br /&gt;
EndFunc   ;==&amp;gt;Example&lt;br /&gt;
 &lt;br /&gt;
Func SomeFunc()&lt;br /&gt;
	; This should create a variable in Local scope if the variable name doesn&amp;quot;t already exist.&lt;br /&gt;
	; For argument sake I totally forgot that I declared a variable already with the same name.&lt;br /&gt;
	; Well I only want this to be changed in the function and not the variable at the top of the script.&lt;br /&gt;
	; Should be OK right? Think again.&lt;br /&gt;
	Dim $vVariableThatIsGlobal = &amp;quot;&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
	For $i = 1 To 10&lt;br /&gt;
		$vVariableThatIsGlobal &amp;amp;= $i ; This will return 12345678910 totally wiping the previous contents of $vVariableThatIsGlobal.&lt;br /&gt;
	Next&lt;br /&gt;
	Return $vVariableThatIsGlobal&lt;br /&gt;
EndFunc   ;==&amp;gt;SomeFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;jumpsec1&amp;quot;&amp;gt;Declaring Global variables in a Function is never a good idea:&amp;lt;/span&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
; Calling Example() first will initialise the Global variable $vVariableThatIsGlobal and therefore calling SomeFunc() won&#039;t return an error.&lt;br /&gt;
; Now look at Example 2.&lt;br /&gt;
Example()&lt;br /&gt;
 &lt;br /&gt;
Func Example()&lt;br /&gt;
	; Declaring a variable in a function can cause serious problems, hence why all Global variables should be declared at the top of a script.&lt;br /&gt;
	Global $vVariableThatIsGlobal = &#039;This is a variable that has &#039;&#039;File Scope&#039;&#039; aka Global.&#039;&lt;br /&gt;
	SomeFunc()&lt;br /&gt;
EndFunc   ;==&amp;gt;Example&lt;br /&gt;
 &lt;br /&gt;
Func SomeFunc()&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &#039;&#039;, $vVariableThatIsGlobal) ; As the variable was initialised this will not return an error.&lt;br /&gt;
EndFunc   ;==&amp;gt;SomeFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
; Calling SomeFunc() first will bypass the Global variable $vVariableThatIsGlobal being initialised and therefore AutoIt has no idea of what data the variable&lt;br /&gt;
; $vVariableThatIsGlobal contains.&lt;br /&gt;
SomeFunc()&lt;br /&gt;
 &lt;br /&gt;
Func Example()&lt;br /&gt;
	; Declaring a variable in a function can cause serious problems, hence why all Global variables should be declared at the top of a script.&lt;br /&gt;
	Global $vVariableThatIsGlobal = &#039;This is a variable that has &#039;&#039;File Scope&#039;&#039; aka Global.&#039;&lt;br /&gt;
	SomeFunc()&lt;br /&gt;
EndFunc   ;==&amp;gt;Example&lt;br /&gt;
 &lt;br /&gt;
Func SomeFunc()&lt;br /&gt;
	MsgBox($MB_SYSTEMMODAL, &#039;&#039;, $vVariableThatIsGlobal) ; As the variable wasn&#039;t initialised this will return an error of &amp;quot;variable used without being declared.&amp;quot;&lt;br /&gt;
EndFunc   ;==&amp;gt;SomeFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Declaring variables in loops (For, While, Do etc..) can have an affect on efficiency:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
; Declaring variables inside loops should be avoided as the variable is re-declared on each repetition.&lt;br /&gt;
For $i = 1 To 10 ; $i is in &#039;loop scope.&#039;&lt;br /&gt;
	Local $iInt = $i&lt;br /&gt;
Next&lt;br /&gt;
MsgBox($MB_SYSTEMMODAL, &#039;&#039;, $iInt) ; This will display 10.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
; Declaring variables outside of loops is more efficent in the long run.&lt;br /&gt;
Local $iInt = 0&lt;br /&gt;
For $i = 1 To 10 ; $i is in &#039;loop scope.&#039;&lt;br /&gt;
	$iInt = $i&lt;br /&gt;
Next&lt;br /&gt;
MsgBox($MB_SYSTEMMODAL, &#039;&#039;, $iInt) ; This will display 10.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There is no requirement to declare the iteration count variable in a loop:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Correct&lt;br /&gt;
Local Const $iCount = 99&lt;br /&gt;
Local $aArray[$iCount]&lt;br /&gt;
For $i = 0 To UBound($iCount) - 1 ; $i is only used in the loop, so there is no requirement to declare it.&lt;br /&gt;
	$aArray[$i] = $i&lt;br /&gt;
Next&lt;br /&gt;
&lt;br /&gt;
; Incorrect&lt;br /&gt;
Local Const $iCount = 99&lt;br /&gt;
Local $aArray[$iCount]&lt;br /&gt;
Local $i ; This is only used to store the iteration count value in the loop and therefore doesn&#039;t need to be declared. This is known as loop scope.&lt;br /&gt;
For $i = 0 To UBound($iCount) - 1&lt;br /&gt;
	$aArray[$i] = $i&lt;br /&gt;
Next&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
As you can see, there is the Const keyword in the example, we are going to talk about it.&lt;br /&gt;
&lt;br /&gt;
== Const, Static, Enum ==&lt;br /&gt;
&lt;br /&gt;
=== Const ===&lt;br /&gt;
We won&#039;t talk about the advantages of a constant variable, they are neglibible (for your information, an autoit constant variable is marked as read-only and remains a variable as read-only when compiled).&lt;br /&gt;
&lt;br /&gt;
The Const keyword may be used in a first by some of you to avoid re-assignments.&lt;br /&gt;
The best way to use them is not this last case, the constants should be used for real static variables, meaning that their value won&#039;t change regardless to the instance of the program.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
;Bad&lt;br /&gt;
Local Const $hGUI = GUICreate(&amp;quot;MyGUI&amp;quot;)&lt;br /&gt;
; The handle of the window is unique, it&#039;s generated by Windows and changes.&lt;br /&gt;
&lt;br /&gt;
;Good&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Static ===&lt;br /&gt;
Static variables are the solution to the global variables used in only one function.&lt;br /&gt;
e.g: Retain variable data once returned from a Function and only use that variable in that particular Function.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
Example()&lt;br /&gt;
&lt;br /&gt;
Func Example()&lt;br /&gt;
    SomeFunc() ; This will display a message box of 1, 1.&lt;br /&gt;
    SomeFunc() ; This will display a message box of 1, 2.&lt;br /&gt;
    SomeFunc() ; This will display a message box of 1, 3.&lt;br /&gt;
EndFunc   ;==&amp;gt;Example&lt;br /&gt;
&lt;br /&gt;
Func SomeFunc()&lt;br /&gt;
    ; This initialises a Static variable in Local scope. When a variable is declared just in Local scope (within a Function,)&lt;br /&gt;
    ; it&#039;s destroyed when the Function ends/returns. This isn&#039;t the case for a Static variable. The variable can&#039;t be&lt;br /&gt;
    ; accessed from anywhere else in the script apart from the Function it was declared in.&lt;br /&gt;
    Local Static $vVariableThatIsStatic = 0&lt;br /&gt;
    Local $vVariableThatIsLocal = 0&lt;br /&gt;
    $vVariableThatIsLocal += 1 ; This will always be 1 as it was destroyed once returned from SomeFunc.&lt;br /&gt;
    $vVariableThatIsStatic += 1 ; This will increase by 1.&lt;br /&gt;
    MsgBox(4096, $vVariableThatIsLocal, $vVariableThatIsStatic)&lt;br /&gt;
EndFunc   ;==&amp;gt;SomeFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Enum ===&lt;br /&gt;
This statement is often practical in certain situations:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MessageBoxConstants.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example()&lt;br /&gt;
&lt;br /&gt;
Func Example()&lt;br /&gt;
    ; Create variables in Local scope and enumerate through the variables. Default is to start from 0.&lt;br /&gt;
    Local Enum $eCat, $eDog, $eMouse, $eHamster ; $eHamster is equal to the value 3, not 4.&lt;br /&gt;
&lt;br /&gt;
    ; Create an array in Local scope with 4 elements.&lt;br /&gt;
    Local $aAnimalNames[4]&lt;br /&gt;
&lt;br /&gt;
    ; Assign each array element with the name of the respective animal. For example the name of the cat is Jasper.&lt;br /&gt;
    $aAnimalNames[$eCat] = &#039;Jasper&#039; ; $eCat is equal to 0, similar to using $aAnimalNames[0]&lt;br /&gt;
    $aAnimalNames[$eDog] = &#039;Beethoven&#039; ; $eDog is equal to 1, similar to using $aAnimalNames[1]&lt;br /&gt;
    $aAnimalNames[$eMouse] = &#039;Pinky&#039; ; $eMouse is equal to 2, similar to using $aAnimalNames[2]&lt;br /&gt;
    $aAnimalNames[$eHamster] = &#039;Fidget&#039; ; $eHamster is equal to 3, similar to using $aAnimalNames[3]&lt;br /&gt;
&lt;br /&gt;
    ; Display the values of the array.&lt;br /&gt;
    MsgBox($MB_SYSTEMMODAL, &#039;&#039;, &#039;$aAnimalNames[$eCat] = &#039; &amp;amp; $aAnimalNames[$eCat] &amp;amp; @CRLF &amp;amp; _&lt;br /&gt;
            &#039;$aAnimalNames[$eDog] = &#039; &amp;amp; $aAnimalNames[$eDog] &amp;amp; @CRLF &amp;amp; _&lt;br /&gt;
            &#039;$aAnimalNames[$eMouse] = &#039; &amp;amp; $aAnimalNames[$eMouse] &amp;amp; @CRLF &amp;amp; _&lt;br /&gt;
            &#039;$aAnimalNames[$eHamster] = &#039; &amp;amp; $aAnimalNames[$eHamster] &amp;amp; @CRLF)&lt;br /&gt;
&lt;br /&gt;
    ; Sometimes using this approach for accessing an element is more practical than using a numerical value, due to the fact changing the index value of&lt;br /&gt;
    ; the enum constant has no affect on it&#039;s position in the array. Therefore changing the location of $eCat in the array is as simple as changing the order&lt;br /&gt;
    ; it appears in the initial declaration e.g.&lt;br /&gt;
&lt;br /&gt;
    ; Local Enum $eDog, $eMouse, $eCat, $eHamster&lt;br /&gt;
&lt;br /&gt;
    ; Now $eCat is the 2nd element in the array. If you were using numerical values, you would have to manually change all references of $aAnimalNames[0] to&lt;br /&gt;
    ; $aAnimalNames[2], as well as for the other elements which have now shifted.&lt;br /&gt;
EndFunc   ;==&amp;gt;Example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Au3Check directive ==&lt;br /&gt;
As you may know (and we hope), the Au3Check tool checks your code for syntax errors, variables used without being declared etc. which is a good thing to fix your script.&lt;br /&gt;
&lt;br /&gt;
With the official custom directive used to check the helpfile examples/includes, you can apply the good coding practices listed above:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Magic Numbers ==&lt;br /&gt;
Magic numbers are arbitrary numbers interspersed throughout a program&#039;s source code which do not have an associated identifier. The downside to this is not being able to derive a meaning from the number. &lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox(262144, &amp;quot;Magic Numbers&amp;quot;, &amp;quot;It&#039;s Adventure Time!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
In this example, the magic number is 262144 with the identifier being $MB_TOPMOST according to the helpfile.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example 2:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Imagine you&#039;re a new user to AutoIt and you come across this code, where would you find -3, -4 or -5 in the help file?&lt;br /&gt;
; Since AutoIt is relatively a new concept to you, your first thought isn&#039;t to search through all the include files, I mean&lt;br /&gt;
; why would you, the help file is there for a reason.&lt;br /&gt;
Example()&lt;br /&gt;
&lt;br /&gt;
Func Example()&lt;br /&gt;
    Local $hGUI = GUICreate(&#039;&#039;)&lt;br /&gt;
    GUICtrlCreateLabel(&#039;Why magic numbers are counter productive.&#039;, 5, 5)&lt;br /&gt;
    GUICtrlSetState(Default, 128) ; Does this hide, show or disable it?&lt;br /&gt;
    GUICtrlSetState(Default, 64) ; Does this hide, show or disable it?&lt;br /&gt;
    GUISetState(@SW_SHOW, $hGUI)&lt;br /&gt;
&lt;br /&gt;
    While 1&lt;br /&gt;
        Switch GUIGetMsg()&lt;br /&gt;
            Case -3 ; Doesn&#039;t really tell much about what it does.&lt;br /&gt;
                ExitLoop&lt;br /&gt;
&lt;br /&gt;
            Case -4, -5 ; Again, no idea what these are. MouseMove? MouseClick? Restore?&lt;br /&gt;
                MsgBox(4096, &#039;&#039;, &#039;Do something when this action takes place.&#039;)&lt;br /&gt;
&lt;br /&gt;
        EndSwitch&lt;br /&gt;
    WEnd&lt;br /&gt;
&lt;br /&gt;
    GUIDelete($hGUI)&lt;br /&gt;
EndFunc   ;==&amp;gt;Example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Did you understand the numbers were these:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;GUIConstantsEx.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example()&lt;br /&gt;
&lt;br /&gt;
Func Example()&lt;br /&gt;
    Local $hGUI = GUICreate(&#039;&#039;)&lt;br /&gt;
    GUICtrlCreateLabel(&#039;Why magic numbers are counter productive.&#039;, 5, 5)&lt;br /&gt;
    GUICtrlSetState(Default, $GUI_DISABLE) ; Better, this is documented in the help file.&lt;br /&gt;
    GUICtrlSetState(Default, $GUI_ENABLE) ; Better, this is documented in the help file.&lt;br /&gt;
    GUISetState(@SW_SHOW, $hGUI)&lt;br /&gt;
&lt;br /&gt;
    While 1&lt;br /&gt;
        Switch GUIGetMsg()&lt;br /&gt;
            Case $GUI_EVENT_CLOSE ; Better, this is documented in the help file. Ah, it&#039;s the close action.&lt;br /&gt;
                ExitLoop&lt;br /&gt;
&lt;br /&gt;
            Case $GUI_EVENT_MINIMIZE, $GUI_EVENT_RESTORE ; Better, this is documented in the help file.&lt;br /&gt;
                MsgBox($MB_SYSTEMMODAL, &#039;&#039;, &#039;Do something when this action takes place.&#039;) ; Better, this is documented in the help file.&lt;br /&gt;
&lt;br /&gt;
        EndSwitch&lt;br /&gt;
    WEnd&lt;br /&gt;
&lt;br /&gt;
    GUIDelete($hGUI)&lt;br /&gt;
EndFunc   ;==&amp;gt;Example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Magic_number_(programming) Magic number (programming)]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
More coming soon :)&lt;/div&gt;</summary>
		<author><name>Prime03</name></author>
	</entry>
</feed>