<?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=Czardas</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=Czardas"/>
	<link rel="alternate" type="text/html" href="https://www.autoitscript.com/wiki/Special:Contributions/Czardas"/>
	<updated>2026-04-03T18:49:36Z</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=12933</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=12933"/>
		<updated>2015-03-15T11:16:33Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: You must use AutoIt v3.3.10.0 or above to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Constant variable || Local Const $eEulersConstant = 2.7182818284590452&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile default value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| False (or True) || $b&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Declare and initialize a variable with the recommended default value.&lt;br /&gt;
Local $vUndefined = Null ; This does not require much memory.&lt;br /&gt;
&lt;br /&gt;
; Some time later:&lt;br /&gt;
$vUndefined = 0xB0AD1CEA ; Assign an appropriate value as and when needed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Scopes of Variables ==&lt;br /&gt;
Variables should also be 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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12932</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=12932"/>
		<updated>2015-03-15T11:11:47Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: You must use AutoIt v3.3.10.0 or above to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Constant variable || Local Const $eEulersConstant = 2.7182818284590452&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile default value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $fu, $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| False (or True) || $b&lt;br /&gt;
|-&lt;br /&gt;
| Default || $k&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Declare and initialize a variable with the recommended default value.&lt;br /&gt;
Local $vUndefined = Null ; This does not require much memory.&lt;br /&gt;
&lt;br /&gt;
; Some time later:&lt;br /&gt;
$vUndefined = 0xB0AD1CEA ; Assign an appropriate value as and when needed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Scopes of Variables ==&lt;br /&gt;
Variables should also be 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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12931</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=12931"/>
		<updated>2015-03-15T11:05:28Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: You must use AutoIt v3.3.10.0 or above to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Constant variable || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile default value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $fu, $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| False (or True) || $b&lt;br /&gt;
|-&lt;br /&gt;
| Default || $k&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Declare and initialize a variable with the recommended default value.&lt;br /&gt;
Local $vUndefined = Null ; This does not require much memory.&lt;br /&gt;
&lt;br /&gt;
; Some time later:&lt;br /&gt;
$vUndefined = 0xB0AD1CEA ; Assign an appropriate value as and when needed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Scopes of Variables ==&lt;br /&gt;
Variables should also be 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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12930</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=12930"/>
		<updated>2015-03-15T11:00:22Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: You must use AutoIt v3.3.10.0 or above to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile default value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $fu, $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| False (or True) || $b&lt;br /&gt;
|-&lt;br /&gt;
| Default || $k&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Declare and initialize a variable with the recommended default value.&lt;br /&gt;
Local $vUndefined = Null ; This does not require much memory.&lt;br /&gt;
&lt;br /&gt;
; Some time later:&lt;br /&gt;
$vUndefined = 0xB0AD1CEA ; Assign an appropriate value as and when needed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Scopes of Variables ==&lt;br /&gt;
Variables should also be 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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12929</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=12929"/>
		<updated>2015-03-15T09:45:40Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note&#039;&#039;&#039;: You must use AutoIt v3.3.10.0 or above to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile default value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $fu, $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| False (or True) || $b&lt;br /&gt;
|-&lt;br /&gt;
| Default || $k&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Declare and initialize a variable with the recommended default value.&lt;br /&gt;
Local $vUndefined = Null ; This does not require much memory.&lt;br /&gt;
&lt;br /&gt;
; Some time later:&lt;br /&gt;
$vUndefined = 0xB0AD1CEA ; Assign an appropriate value as and when needed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Scopes of Variables ==&lt;br /&gt;
Variables should also be 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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12920</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=12920"/>
		<updated>2015-03-10T12:09:37Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile default value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $fu, $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| True (or False) || $b&lt;br /&gt;
|-&lt;br /&gt;
| Default || $k&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Declare and initialize a variable with the recommended default value.&lt;br /&gt;
Local $vUndefined = Null ; This does not require much memory.&lt;br /&gt;
&lt;br /&gt;
; Some time later:&lt;br /&gt;
$vUndefined = 0xB0AD1CEA ; Assign an appropriate value as and when needed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12919</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=12919"/>
		<updated>2015-03-10T12:08:38Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $fu, $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| True (or False) || $b&lt;br /&gt;
|-&lt;br /&gt;
| Default || $k&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Declare and initialize a variable with the recommended default value.&lt;br /&gt;
Local $vUndefined = Null ; This does not require much memory.&lt;br /&gt;
&lt;br /&gt;
; Some time later:&lt;br /&gt;
$vUndefined = 0xB0AD1CEA ; Assign an appropriate value as and when needed.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12918</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=12918"/>
		<updated>2015-03-10T11:27:39Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $tSTRUCT = DllStructCreate($tagSTRUCT)&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by assigning data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the following table, recommended default values are shown for each data type. Some data types have more than one possibile value which can be used for initialization.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Default Value !! covering type&lt;br /&gt;
|-&lt;br /&gt;
| Binary(&amp;quot;&amp;quot;) || $d&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;&amp;quot; || $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0 || $a, $h, $i, $id, $m, $n, $o, $p, $t, $tag, $v&lt;br /&gt;
|-&lt;br /&gt;
| 0.0 || $f&lt;br /&gt;
|-&lt;br /&gt;
| Null || $fu, $o, $s, $v&lt;br /&gt;
|-&lt;br /&gt;
| True (or False) || $b&lt;br /&gt;
|-&lt;br /&gt;
| Default || $k&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; Recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;MsgBoxConstants.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;
* A variable declared globally (with the Global keyword) is visible anywhere in the script.&amp;lt;br /&amp;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;
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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;
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;
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;MsgBoxConstants.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;
&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;MsgBoxConstants.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;MsgBoxConstants.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;
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;MsgBoxConstants.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;MsgBoxConstants.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;
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;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Not recommended&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;
; Recommended&lt;br /&gt;
Local Const $iMyAge = 19&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
=== 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;MsgBoxConstants.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;
== 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;
== 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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12909</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=12909"/>
		<updated>2015-03-08T04:34:52Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Inconsistent data types are considered bad.&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; correct initialization&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to follow, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12908</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=12908"/>
		<updated>2015-03-08T04:09:46Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12907</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=12907"/>
		<updated>2015-03-08T04:08:14Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To reduce bloat, multiple variables can be declared on a single line. When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12906</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=12906"/>
		<updated>2015-03-08T02:57:27Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Variables are named following this schema:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! dollar prefix !! type (lower case) !! [optional] subtype (lower case) !! var name (first letter in upper case)&lt;br /&gt;
|-&lt;br /&gt;
| $ || a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idBtnGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idBtnQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12905</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=12905"/>
		<updated>2015-03-08T02:10:44Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idBtnGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idBtnQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12904</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=12904"/>
		<updated>2015-03-08T02:09:59Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idBtnGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idBtnQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. Regarding data types, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12903</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=12903"/>
		<updated>2015-03-08T01:42:54Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When declaring multiple variables on the same line, it is generally recommended that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idBtnGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idBtnQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. As regards type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12902</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=12902"/>
		<updated>2015-03-08T01:37:47Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When declaring multiple variables on the same line, it is a general recommendation that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idBtnGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idBtnQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In some languages it is essential to initialize variables on declaration, but this is not the case with AutoIt. As regards type, variables declared without being initialized should be considered as being undefined.&lt;br /&gt;
&amp;lt;br /&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12901</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=12901"/>
		<updated>2015-03-08T01:12:59Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
; Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
When declaring multiple variables on the same line, it is a general recommendation that you stick to declaring one data type on each line. The intention here is to make the code easier to read, however the best layout will vary according to circumstance.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; not recommended&lt;br /&gt;
Local $sString = &amp;quot;&amp;quot;, $iInteger = 0, $asArray = [&amp;quot;a&amp;quot;,&amp;quot;b&amp;quot;,&amp;quot;c&amp;quot;] ; Mixed data types&lt;br /&gt;
&lt;br /&gt;
; recommended&lt;br /&gt;
Local $iLeft = 10, $iTop = 10 ; integers&lt;br /&gt;
Local $idBtnGo = GUICtrlCreateButton(&amp;quot;Go&amp;quot;, $iLeft, $iTop), $idBtnQuit = GUICtrlCreateButton(&amp;quot;Quit&amp;quot;, 50, 10) ; controlIDs&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12900</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=12900"/>
		<updated>2015-03-08T00:28:07Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
;Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12899</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=12899"/>
		<updated>2015-03-08T00:22:34Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same (or the most logical representation of its) type and use as little memory as possible.&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 because of inconsistent data types&lt;br /&gt;
Local $iInteger = &amp;quot;0&amp;quot;&lt;br /&gt;
Local $sString = False&lt;br /&gt;
&lt;br /&gt;
;Good&lt;br /&gt;
Local $iInteger = 0&lt;br /&gt;
Local $sString = &#039;&#039;&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12898</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=12898"/>
		<updated>2015-03-08T00:03:19Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same type and use as little memory as possible.&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12897</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=12897"/>
		<updated>2015-03-08T00:00:43Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&lt;br /&gt;
When initializing variables there are several points to consider. It is bad practice to hog memory by holding data which is not immediately required. It is therefore recommended that you declare and initialize variables immediately prior to use. If for some reason you wish to assign a default value to a variable which you intend to overwrite later, then the data should be of the same type.&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12896</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=12896"/>
		<updated>2015-03-07T22:53:18Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Variable Initialization ==&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12895</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=12895"/>
		<updated>2015-03-07T22:35:04Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
The variable naming convention used in AutoIt is based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12894</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=12894"/>
		<updated>2015-03-07T22:27:34Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
Variable naming conventions in AutoIt are based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12893</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=12893"/>
		<updated>2015-03-07T22:26:57Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do so.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
Variable naming conventions in AutoIt are based on [http://en.wikipedia.org/wiki/Hungarian_notation Apps Hungarian notation]. The prefix defines the logical data type rather than the physical data type: in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs during assignment. See the table below for accepted standards.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12892</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=12892"/>
		<updated>2015-03-07T22:02:25Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Outlined in this section is a detailed explanation of what are to be considered the best coding practices within AutoIt. These recommendations are based on accepted coding practices common to a number of other programming languages. You do not need to follow them, but it is recommended that you do so.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A few notes:&lt;br /&gt;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation defines the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12891</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=12891"/>
		<updated>2015-03-07T21:40:29Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation defines the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of strings.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12890</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=12890"/>
		<updated>2015-03-07T21:37:47Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation defines the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 0&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlID || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $pRect = DllStructGetPtr($tRECT)&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tagDATE = &amp;quot;struct; word Year;word Month;word Dow;word Day; endstruct&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $oShell = ObjCreate(&amp;quot;shell.application&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $vData = ClipGet()&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;
| a || h || Handles&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Examples:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Assign a local variable the integer 7.&lt;br /&gt;
Local $iWeekDays = 7&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable the value of Pi.&lt;br /&gt;
Local $fPi = 3.14159265358979&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $asArray[7] = [&#039;mon&#039;, &#039;tue&#039;, &#039;wed&#039;, &#039;thu&#039;, &#039;fri&#039;, &#039;sat&#039;, &#039;sun&#039;]&lt;br /&gt;
&lt;br /&gt;
; Assign a local variable an array of numbers.&lt;br /&gt;
Local $anArray[4] = [0, 0.25, 3 / 4, 12]&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
The corrected example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
MsgBox($MB_TOPMOST, &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;
&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;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12870</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=12870"/>
		<updated>2015-03-07T00:03:06Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation defines the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this occurs on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12869</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=12869"/>
		<updated>2015-03-07T00:00:09Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation defines the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12868</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=12868"/>
		<updated>2015-03-06T23:59:10Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to define the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12867</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=12867"/>
		<updated>2015-03-06T23:34:20Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1st = 1, $e2nd, $e3rd&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12866</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=12866"/>
		<updated>2015-03-06T23:32:10Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $e1stValue = 1, $e2ndValue, $e3rdValue&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12865</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=12865"/>
		<updated>2015-03-06T23:31:17Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $eFirstValue = 1, $eSecondValue, $eThirdValue&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12864</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=12864"/>
		<updated>2015-03-06T23:29:24Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $eFirst = 1, $eSecond, $eThird&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12863</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=12863"/>
		<updated>2015-03-06T23:13:26Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type: this is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $eStartCount = 0, $eAddOne, $eAddTwo&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12862</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=12862"/>
		<updated>2015-03-06T23:08:28Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Names of Variables ==&lt;br /&gt;
A variant of [http://en.wikipedia.org/wiki/Hungarian_notation Hungarian notation] is used.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type. This is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $eStartCount = 0, $eAddOne, $eAddTwo&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12861</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=12861"/>
		<updated>2015-03-06T23:02:25Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&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 has been simplified and regroup all the types of numbers in one type.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type. This is done on assignment.&amp;lt;br /&amp;gt;&lt;br /&gt;
See the table below for examples.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $eStartCount = 0, $eAddOne, $eAddTwo&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12860</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=12860"/>
		<updated>2015-03-06T22:52:55Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&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.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type.&amp;lt;br /&amp;gt;&lt;br /&gt;
Variables are initialized with the &amp;quot;default&amp;quot; value (or the most logical representation) of their type. See below in the table for specific value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $eStartCount = 0, $eAddOne, $eAddTwo&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Best_coding_practices&amp;diff=12859</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=12859"/>
		<updated>2015-03-06T22:51:51Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &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;
* You must use the latest version of AutoIt (v3.3.10.0 or above) to run the examples below.&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.&amp;lt;br /&amp;gt;&lt;br /&gt;
In AutoIt the Hungarian notation strives to encode the logical data type rather than the physical data type; in this way, it gives a hint as to what the variable&#039;s purpose is, or what it represents. The prefix does not encode the actual data type.&amp;lt;br /&amp;gt;&lt;br /&gt;
Variables are initialized with the &amp;quot;default&amp;quot; value (or the most logical representation) of their type. See below in the table for specific value.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! prefix !! covering type !! example&lt;br /&gt;
|-&lt;br /&gt;
| i || Integer || $iInteger = 10&lt;br /&gt;
|-&lt;br /&gt;
| f || Floating point || $fFloat = 0.123&lt;br /&gt;
|-&lt;br /&gt;
| n || General number (no preference) || $nNumber = 123&lt;br /&gt;
|-&lt;br /&gt;
| a || Arrays || Local $aArray[0]&lt;br /&gt;
|-&lt;br /&gt;
| m || Maps || Local $mMap[]&lt;br /&gt;
|-&lt;br /&gt;
| s || Strings (chars included) || $sString = &amp;quot;hello world&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| b || Booleans || $bBool = True&lt;br /&gt;
|-&lt;br /&gt;
| d || Binaries || $dBinary = Binary(&amp;quot;0x80000000&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| id || An AutoIt controlid || $idButton = GUICtrlCreateButton(&amp;quot;OK&amp;quot;, 5, 5)&lt;br /&gt;
|-&lt;br /&gt;
| h || Handles (and GUI handles) || $hGUI = GUICreate(&amp;quot;My GUI&amp;quot;)&lt;br /&gt;
|-&lt;br /&gt;
| fu || Functions || $fuMessage = MsgBox&lt;br /&gt;
|-&lt;br /&gt;
| p || Pointers || $p = 0&lt;br /&gt;
|-&lt;br /&gt;
| tag || Structures definition || $tag = 0&lt;br /&gt;
|-&lt;br /&gt;
| t || Structures || $t = 0&lt;br /&gt;
|-&lt;br /&gt;
| o || Objects || $o = 0 or $o = Null&lt;br /&gt;
|-&lt;br /&gt;
| v || Variant || $v = 0 or $v = &amp;quot;&amp;quot; (or $v = Null)&lt;br /&gt;
|-&lt;br /&gt;
| e || Enumerations || Enum $eBegin = 0, $eAddOne, $eAddTwo&lt;br /&gt;
|-&lt;br /&gt;
| k || Keywords || $kKeyword = Default&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;MsgBoxConstants.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;
== 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;
| $g__iSomeVar || $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;MsgBoxConstants.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;MsgBoxConstants.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 MyFunc.&lt;br /&gt;
$_sVar2 = MyFunc()&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 MyFunc()&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   ;==&amp;gt;MyFunc&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 MyFunc with default parameters ($vParam1 = 0).&lt;br /&gt;
MyFunc()&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 MyFunc with $aiArray as parameter ($vParam1 = $aiArray).&lt;br /&gt;
MyFunc($aiArray)&lt;br /&gt;
&lt;br /&gt;
Func MyFunc($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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;MsgBoxConstants.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;
&lt;br /&gt;
&lt;br /&gt;
== Include-once directive ==&lt;br /&gt;
This one is designed for standard includes and UDFs, it&#039;s highly recommended to use it.&lt;br /&gt;
&lt;br /&gt;
Those includes may be included in more than one script of your project because they are needed for some includes or your script itself.&lt;br /&gt;
In that case, the code will be duplicated which is not a good thing especially if you have (and it&#039;s mainly the case) constants declared in those files, in so far as the constants cannot be redeclared/reassigned; same thing for functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Put the directive in top of your UDF (library) to avoid itself to be included more than once :&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include-once&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Talk:Best_coding_practices&amp;diff=12852</id>
		<title>Talk:Best coding practices</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Talk:Best_coding_practices&amp;diff=12852"/>
		<updated>2015-03-06T08:47:50Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Have you ever seen this thread?&lt;br /&gt;
http://www.autoitscript.com/forum/topic/152580-cant-click-2nd-div-object/#entry1095954&lt;br /&gt;
&lt;br /&gt;
I do not check this&lt;br /&gt;
&amp;quot;using &amp;quot;==&amp;quot; instead of &amp;quot;=&amp;quot; for object&#039;s properties&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
Is it true that this way is better?&lt;br /&gt;
 &lt;br /&gt;
If YES then this may be helpful as additional information to this Wiki Documents&lt;br /&gt;
&lt;br /&gt;
I&#039;m wondering what the variable initialization column is about. I can only assume that these values are suggestions. The wording would indicate they are not merely so. Either I&#039;m missing something or this needs to be ammended. It appears to me that variables are initialized as empty strings by default. czardas&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Talk:Best_coding_practices&amp;diff=12851</id>
		<title>Talk:Best coding practices</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Talk:Best_coding_practices&amp;diff=12851"/>
		<updated>2015-03-06T08:42:50Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Have you ever seen this thread?&lt;br /&gt;
http://www.autoitscript.com/forum/topic/152580-cant-click-2nd-div-object/#entry1095954&lt;br /&gt;
&lt;br /&gt;
I do not check this&lt;br /&gt;
&amp;quot;using &amp;quot;==&amp;quot; instead of &amp;quot;=&amp;quot; for object&#039;s properties&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
Is it true that this way is better?&lt;br /&gt;
 &lt;br /&gt;
If YES then this may be helpful as additional information to this Wiki Documents&lt;br /&gt;
&lt;br /&gt;
I&#039;m wondering what the variable initialization column is about. I can only assume that these values are suggestions. The wording would indicate they are not merely so. Either I&#039;m missing something or this needs to be ammended. It appears to me that variables are initialized as empty strings by default.&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=User_Defined_Functions&amp;diff=12775</id>
		<title>User Defined Functions</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=User_Defined_Functions&amp;diff=12775"/>
		<updated>2015-02-22T22:38:54Z</updated>

		<summary type="html">&lt;p&gt;Czardas: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is a listing of libraries of &#039;&#039;&#039;user defined functions&#039;&#039;&#039; (UDF). These libraries have been written to allow easy integration into your own scripts and are a very valuable resource for any programmer.&lt;br /&gt;
This list is probably not complete, but constantly supplemented.&lt;br /&gt;
If you do not find a solution here, ask a new question on the [http://www.autoitscript.com/forum/forum/2-general-help-and-support/ forum].&lt;br /&gt;
&lt;br /&gt;
== Automation ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=87956 Java UDF] - Creates an access bridge between your application and a Java application. Allowing you to automate some Java applications.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=86574 SAP] - SAP business management automation.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=149540 SAPWizard] - SAPWizard.UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=91018 WiFi] - Low level control over your wireless LAN &lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=106163 Active Directory] - Extensive library to control and manipulate the Windows active directory. Link to the [[Active_Directory_UDF_-_General|documentation]] pages. &lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=80201 Service] - Build your own service with AutoIt code.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=150231 GTK+ ] - GTK+ Framework | Widgets&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=153520 IUIAutomation MS framework] - IUIAutomation MS framework automate chrome, FF, IE, ....&lt;br /&gt;
===Browsers===&lt;br /&gt;
* Internet Explorer - Everything about Internet explorer can be automated with the IE library supplied with a standard AutoIt install.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=166542 HTMLDocumentEvents UDF] - Track IE document events&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=166304 IEJS UDF] - IEJS - IE Javascript options, an IE.au3 personal extension&lt;br /&gt;
&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=95595 Firefox] - A little less support for automation than IE, but still very good.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=61090 Opera] - The same as above for Opera. Automate the most common tasks in Opera with the Opera UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=154439 Chrome] - The same as above for Google Chrome. Automate the most common tasks in Chrome with the Chrome UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=149203 NavInfo UDF] -  With this UDF now you can check if a specified browser/software is installed an you are also able to get what version is in used..&lt;br /&gt;
&lt;br /&gt;
===Microsoft Office Automation===&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=32144 Microsoft Office Access] - Automation of Access.&lt;br /&gt;
* Microsoft Office Excel - A way to automate Office Excel is included with AutoIt. Link to the [[Excel_UDF|documentation]] pages.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=135312 Microsoft Office Excel Charts] - Creating charts using Excel.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=126305 Microsoft Office Outlook] - Extended Outlook UDF. Link to the [[OutlookEX_UDF_-_General|documentation]] pages.&lt;br /&gt;
* Microsoft Office Word UDF - A way to automate Office Word is included with AutoIt. Link to the [[Word_UDF|documentation]] pages.&lt;br /&gt;
&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=34302 ExcelCOM UDF] Yet Another -- ExcelCOM UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=50254 PowerPoint Wrapper] It allows you to have control of MS PowerPoint.&lt;br /&gt;
&lt;br /&gt;
== Hardware ==&lt;br /&gt;
* [[CommAPI]] - Serial and parallel communication (COM port, RS-232, LPT port) - without installing DLL&#039;s (using Windows API calls)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=128546 Serial Port/COM] - Serial Port /COM Port UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=68866 Webcam UDF] - Webcam UDF made by LIMITER.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=70857 Webcam UDF] - Webcam UDF made by ludocus.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=27755 SMARTDRIVE] - SMART drive Analysis.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=97487 DirectShow] - DirectShow UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=154350 Monitor Configuration] - Monitor Configuration UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=20121 Screen Resolution] - Screen Resolution Changing UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=77731 Device Management] - Device Management API&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=155469 Mouse_UDF] - AutoIt powered mouse events&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=155539 Network configuration UDF] - Network configuration UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=138989 FritzBox] - _FB_Tools - manage your FritzBox from Autoit&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=147325 MouseTrapEvent] - MouseTrapEvent UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=149083 NetInfo UDF] - UDF for test internet download speed and upload speed.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=164700 DirectSound UDF] - DirectSound UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=164701 Direct2D UDF] - Direct2D UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=121084 I/O Port Functions UDF] - This is a simple I/O (Input/Output) UDF for interacting with ports. (I/O Port Functions - x64 Parallel Port IO, Keyboard, etc + Restore PC Speaker Beep)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=15864 SPI Hardware Interface] - This script is made to comunicate with the MAX335 chip  using the SPI protocol via the LPT(printer) port.&lt;br /&gt;
&lt;br /&gt;
== Information gathering ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=29404 Computer information] - A general purpose library to get various details about a Windows machine.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=54039 WinPCap - Packet.dll UDF] - &lt;br /&gt;
* [http://opensource.grisambre.net/pcapau3/#reference WinPcap Autoit3 UDF] - This UDF allows very simply from an Autoit script to access the main functionalities offered by the WinPcap driver.&lt;br /&gt;
&lt;br /&gt;
== Databases and web connections ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=17099 SQLite] - &amp;quot;SQLite is a library that implements a self-contained, embeddable, zero-configuration SQL database engine&amp;quot;&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=19848 XML DOM Wrapper] - Supports CRUD operations on XML. Including XSL and XPath.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=20814 MySQL] - MySQL relational database management system UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=85617 MySQL] - MySQL UDFs (without ODBC).&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=145142 DBF] - dBase database read and write with DLL.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=116072 EzMySql] - EzMySql UDF - Use MySql Databases with autoit.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=127101 MS SQL] - MSSQL.au3&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=51952 MS SQL] - _SQL.au3. ADODB.Connection&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=105875 ADODB] - ADODB Example&lt;br /&gt;
* [http://www.autoitscript.com/forum/topic/94920-solved-passing-parameters-using-dllcall-to-a-c-dll/#entry684751 FireBird UDF] - FireBird, Interbase dll udf.&lt;br /&gt;
* [http://stackoverflow.com/questions/21991475/autoit-firebird-sql-combo-return-data-as-csv-instead-of-xml FireBird UDF] - additional link to FireBird UDF&lt;br /&gt;
&lt;br /&gt;
== Internet protocol suite ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=84133 WinHTTP] - Enables scripts to access the HTTP protocol for creating GET and POST requests and submitting them with conforming standards, cookies not supported.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=77503 WinInet] - Enables scripts to access standard Internet protocols, such as FTP, Gopher and HTTP. Also supports creating GET and POST requests and submitting them with conforming standards, cookies supported.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=22838 POP3] - POP3 library for retrieving email messages. Not compatible with Gmail because it uses SSL.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=167339 _POP3_Ex.au3 UDF] POP3 UDF According to the 1939 RFC, modified version with Quoted Printable decoder.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=64051 POP3 SSL] - A POP3 library that&#039;s compatible with Gmail. It uses an external executable that must be supplied with your script.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=43515 IRC] - A lightweight library for communicating with IRC servers.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=138095 SFTP] - UDF to support SFTP protocol using PSFTP.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=104150 JSON] - RFC4627 compliant JSON encode/decode&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=148114 JSON] - JSMN - A Non-Strict JSON UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=156794 JSON] - Bridge to Native Windows JSON plus OO extension for AutoIt&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=137456 cURL] - cURL UDF - a UDF for transferring data with URL syntax&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=81687 SNMP UDF] - SNMP_UDF for SNMPv1 and SNMPv2c&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=70759 SNMP - MIB protocol] (Reading toner status from SNMP device with WMI).&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=43515 IRC] IRC UDF made by Chip&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=159285 IRC] IRC UDF - Updated Version of Chips&#039; IRC UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=57022 UPnP Protocol]  - UPnP : Read and Control your devices in side out.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=154530 Prowl]  - Prowl UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=40243 IMAP]  - IMAP.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=108422 IMAP4 UDF]  - IMAP4 UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=166579 SSH UDF]  - This UDF allows to use the SSH protocol very easily in your code.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=23860 SMTP]  - Smtp Mailer That Supports Html And Attachments.&lt;br /&gt;
&lt;br /&gt;
== Data compression ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=116565 zip] - Create ZIP files and unpack ZIP files.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=85094 7z, zip, gzip, bzip2, tar] - More extensive library than the one above. Uses a external DLL that must be provided with the script.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=161847 XZip] - UDF for &amp;quot;XStandard XZIP Component&amp;quot;.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=17727 XZip] - another UDF for &amp;quot;XStandard XZIP Component&amp;quot;.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=73425 ZIP] - ZIP.au3 UDF in pure AutoIt.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=76176 UnRAR] - UnRAR.au3.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=128962 zLib] - zLib (Deflate/Inflate/GZIP) UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=135565 ZIP] - ZIP STRUCTS UDF (from scratch)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=129529 pZip] - PureZIP_L library UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=44524 Zip plugin] - Zip plugin&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=112273 LZMA Compression UDF] - LZMA Compression UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=87441 LZMA] - LZMA (Native Windows) by trancexx&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=138838 Package UDF] - Package UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=166634 MessagePack UDF] - MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it&#039;s faster and smaller.&lt;br /&gt;
&lt;br /&gt;
== Encryption and hash ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=76976 MD5,SHA1,CRC32,RC4,BASE64,XXTEA] - Several encryption and hash functions.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=78745 AES Rijndael] - Very fast AES UDF. Support ECB/CBC/CFB/OFB block cipher mode.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=107784 TrueCrypt] - TrueCrypt UDFs.&lt;br /&gt;
&lt;br /&gt;
== Media ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=94834 Simple DirectMedia Layer UDF] - Adds support for joysticks, CDs, 2D graphics, timers. See [http://www.libsdl.org/ SDL website] for more information.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=95357 FreeImage library] - Various operations on images, such as rotate, resize, flip.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=51054 Printer controller] - Print text in any font, size and colour at any position on the page, draw lines, curves, elipses, pies in any colour, and print images.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=73993 Printing] - Printing from AutoIt.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=161831 RTF_Printer] - RTF_Printer.au3 - Printing RichEdit in the background.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=89542 OCR] - Tesseract (Screen OCR) UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=89542 Real OCR] - Real OCR in AU3 - MODI with MS Office 2003.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=127263 HtmlHelp UDF] - HtmlHelp UDF&lt;br /&gt;
===Sound===&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=83481 BASS Function Library] - Sound and Music via wrappers for Bass, BassEnc, Bass FX, BassSFX, BassAsio and BassCd DLLs&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=100439 TTS] - Text-to-Speech UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=37072 MIDI] - MIDI UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=114742 SAPIListBox UDF] - SAPIListBox (Speech Recognition) UDF&lt;br /&gt;
&lt;br /&gt;
===Graphics and image===&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=70506 IrrLicht] - A 3D graphics engine suitable for creating games.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=113881 au3Irrlicht2] - Another UDF bringing Irrlicht and au3 together. Historically some kind of a follower of the UDF above, technically with a complete different approach.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=27362 Bitmap Library] - Bitmap Library.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=151011 OpenGL UDF] - OpenGL without external libraries&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=13096 ImageGetInfo] - This is an UDF for reading info from JPEG, TIFF, BMP, PNG and GIF - size, color depth, resolution etc. For JPEG files UDF also retreive various Exif information.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=148129 OpenGL UDFs (2.0)] - new set of UDFs for OpenGL + AutoIt&lt;br /&gt;
&lt;br /&gt;
===Players===&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=114143 VLC UDF] - VLC (Media Player) UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=91316 VLC Media Player] - VLC Media Player&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=27352 WMP UDF] - Windows Media Player UDF&lt;br /&gt;
&lt;br /&gt;
== GUI Additions ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=32494 XSkin] - A large library that allows skinning of your GUI and to apply custom skins.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=132864 Uskin] - A library that allows a user to skin their application GUI using the Windows &#039;&#039;.MSstyles&#039;&#039; files.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=20967 Modern tray menu] - Allows the creation of modern, fancy GUI and tray menus with icons and colors.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=71811 SetOnEvent] - Provides an easy way for an event to call functions with parameters.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=119505 GUIFrame] - Divide a GUI into adjustable frames.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=113723 Easy Scrollbars] - Easily create scrollable sections in your GUI&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=105582 GUICtrlOnChangeRegister] - Call a function when an edits content is changed.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=96258 ContextHelp.au3] - Management of context help ([http://www.autoitscript.com/forum/index.php?showtopic=72152-contexthelp/ original])&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=145149 GUIExtender] - Expand and contract sections of your GUI. ([http://www.autoitscript.com/forum/index.php?showtopic=117909 original])&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=109096 ExtMsgBox] - A very customisable replacement for MsgBox&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=108445 Toast] - Small message GUIs which pop out of the Systray&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=144207 GUI Panel UDF] - Manage child GUIs as panel ctrls&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=161750 Pie chart] - Pie chart&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=97241 3D Pie chart] - 3D Pie chart&lt;br /&gt;
&lt;br /&gt;
=== Controls ===&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=79412 Graph control UDF] - Easily create and show bar chart and line charts.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=105682 GUICtrlCreateFinder] - Allows you to create a window finder control like the one seen in AutoIt Window Info.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=111438 GUIPager] - Create and control native pager controls.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=90598 Hotkey input control] - Hotkeys Input Control UDF Library (Non-native)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=107965 GUIHotkey] - UDF for using native hotkey controls.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=103904&amp;amp;p=735769 Marquees] - Make tickertape info bars&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=96464 Colorpicker] - Create a button for the user to select a color.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=126958 Syslink] - Provides a convenient way to embed hypertext links in a window&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=74649 Progressbar with GDIplus] - You even can use full textured images&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=105814 Table UDF] - Table UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=20967 Table UDF] - GUI/Tray Menu with icons and colors&lt;br /&gt;
* [http://www.autoitscript.com/forum/topic/20967-guitray-menu-with-icons-and-colors/page-15#entry1205045 Table UDF] - GUI/Tray Menu with icons and colors (modified by LarsJ with extra examples by LarsJ and AZJIO)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=128242 Ribbon] - UDF for Windows Ribbon framework.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=166594 GUITreeViewEx] - Check/clear parent and child checkboxes in a TreeView&lt;br /&gt;
&lt;br /&gt;
== Maths ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=83091 Primes UDF] - Many functions dealing with prime number generation and calculations.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=83529 Big number UDF] - Make calculations with extremely large numbers that AutoIt normally is not able to support.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=81189 Number base conversion] - From, to and between positive bases less than 63 (decimals supported)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=106551 Decimal To fraction] - Converts any decimal number to a fraction. Example: 1.2 to 6/5&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=82722 Trigonometry math functions] - _ATan2(), _Cosh(), _Frexp(), _Hypot(), _Ldexp(), _Logb(), _Sinh(), _Tanh()&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=108803 Polynomials] - Functions for using polynomials.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=117156 NumToWord] - Convert numerals to a human readable string.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=98160 Root function] - Working out real roots of numbers.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=102686 Advanced rounding] - Support for different measures of accuracy and 8 ways to resolve tie breaks.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=94770 Roman Numerals] - integer to roman numerals made by Mat.&lt;br /&gt;
* [http://www.autoitscript.com/forum/topic/123398-snippet-dump/page-3#entry1005157 Roman Numerals] - Roman Numerals made by czardas.&lt;br /&gt;
* [http://www.autoitscript.com/forum/topic/94770-integer-to-roman-numerals/#entry1043544 Roman Numerals] - Roman Numerals made by AZJIO.&lt;br /&gt;
&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=163899 StringAPL UDF] - inline APL interpreter.&lt;br /&gt;
&lt;br /&gt;
== Misc ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=90492 Hotkey.au3] - Management of Hotkeys UDF, with several advantages over HotkeySet().&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=97826 Animated tray icons] - Make animated tray icons easily.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=101733 NoFocusLines] - Remove the dotted focus lines from buttons, sliders, radios and checkboxes which spoil the look of your GUI&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=114034 StringSize] - Automatically size controls to fit the text you want to put in them&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=162033 Spell Checker] - Spell Checker UDF - Hunspell&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=125251 TVExplorer] - TVExplorer UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=162366 BinaryCall] - BinaryCall UDF - Write Subroutines In C, Call In AutoIt&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=161628 LFN] - LFN UDF - overcome MAX_PATH limit of 256 chars&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=81267 Clipboard History/Sotrage] - UDF to save and restore the entire clipboard contents&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=51103 Resources UDF] - Resources UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=162499 ResourcesEx UDF] - ResourcesEx UDF (up to date with the current AutoIt language syntax v3.3.12.0)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=149176 NotifyIcon UDF] - NotifyIcon UDF (formerly TrayIconEx) - Create, delete and manage self notify icons&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=131037 Binary UDF] - Binary UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=99106 _DLLStructDisplay] - Show Struct in ListView&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=55994 DDEML UDF] - With DDEML UDF one can use an AutoIt script as a DDE client or server.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=157689 _FileGetMimeType UDF] - _FileGetMimeType UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=157241 FindMimeFromData] - FindMimeFromData using urlmon.dll&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=63318 PixelGetColor UDF] - Get or Read Pixel from Memory UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/topic/117033-file-locking-with-cooperative-semaphores File locking with cooperative semaphores] - Simple file locking without a server&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=142977 SQLite Array Functions] - SQLite Array Functions - a faster method for unique arrays and sorting methods&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=164444 Synology filestation UDF] - UDF for users of Synology NAS server.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=163577 Atom Table UDF] - Basically, a bunch of strings can be stored locally (at program level) or globally (at O/S level) with unique numerical identifiers. This UDF lets you add, find, delete, and query these atoms.&lt;br /&gt;
&lt;br /&gt;
== PDF ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=42776 PDFCreator] - Automation of PDFCreator allows you to create and manipulate PDF files.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=160875 Debenu Quick PDF Library] - A collection of functions for Debenu Quick PDF Library.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=118827 MPDF] - Create PDF from your application.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=164469 Debenu PDF Viewer SDK - UDF] - A collection of functions to display PDF files in your applications using Debenu PDF Viewer SDK.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=75832 FoxIt Reader UDF] - PDF Reader in AU3.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=32261 _StringToPDF UDF] - Write a string to a PDF file and specify font size, type etc.&lt;br /&gt;
&lt;br /&gt;
== Windows ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=145158 Firewall] - Windows Firewall UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=158377 UAC] - User Account Control (UAC) UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=50880 ACL] - Set ACL on windows Objects.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=124508 Startup] - Create Startup entries in the Startup Folder or Registry&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=74118 Local account UDF] - Local account UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=81880 Windows Services UDF] - Windows Services UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=111018 ITaskBarList UDF] - ITaskBarList UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=113560 FileSystemMonitor UDF] - FileSystemMonitor UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=161193 Magnifier Functions UDF] - Magnifier Functions - Windows Vista+ Magnifier Manipulation&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=163178 WRMF UDF] - WRMF - Windows Registry Monitor Call Function&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=75250 Registry UDFs] - Windows Registry UDFs&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=50551 Registry] - RegWriteAllUsers / RegDeleteAllUsers&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=134628 System restore UDF] - System restore UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=83355 Task Sheduler] - Task Scheduler UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=135994 Taskplaner/Taskscheduler COM UDF] - an UDF for using the Windows Taskplaner / Task Scheduler&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=28436 Windows Events] - Create your own Windows events.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=161193 Magnifier Functions UDF] - This UDF exposes most of the useful Magnifier API functions available since Windows Vista.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=127075 WIMGAPI UDF] - A UDF for manipulating Windows Image Files (.wim) without ImageX.exe&lt;br /&gt;
&lt;br /&gt;
== Security ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=150819 VirusTotal] - VirusTotal API 2.0 UDF.&lt;br /&gt;
[[Category:UDF]]&lt;br /&gt;
&lt;br /&gt;
== Social Media and other Website API ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=116600 Twitter] - Twitter UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=70675 iTunes] - iTunes UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=101802 iTunes] - another iTunes UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=149247 Yahoo Weather] - YWeather UDF - Yahoo Weather API.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=115437 Google Maps] - Google Maps UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=150473 Google API] - JSON queries for Google API.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=131343 PasteBin] - PasteBin (powered by PasteBin).&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=150838 PasteBin] - Pastebin UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=114801 eBay UDF] - eBay UDF.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=141340 Gmail] - Remote Gmail (UDF)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=150985 No-IP UDF] - With this UDF you can simply update your no-ip hostname(s) and retrive the ip address of an no-ip address.&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=112775 Youtube Uploader] - AYTU - AutoIt Youtube Uploader&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=98504 Google Functions] - Google Functions&lt;br /&gt;
* [http://www.autoitscript.com/forum/files/file/290-dropbox-authenticator/ Dropbox authenticator] - Dropbox authenticator&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=166547 TVmaze.com API UDF] - TVmaze.com API UDF (TV-Series)&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=113234 Teamspeak 3 UDF] - Teamspeak 3 UDF&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=166205 TeamViewer API - UDF] - UDF for TeamViewer API - a modest beginning&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=158106 Easypost UDF] - Print USPS Postage Labels&lt;br /&gt;
&lt;br /&gt;
== Android ==&lt;br /&gt;
* [http://www.autoitscript.com/forum/index.php?showtopic=160936 Android UDF] - Android UDF.&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Snippets_(_AutoIt_String_)&amp;diff=11631</id>
		<title>Snippets ( AutoIt String )</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Snippets_(_AutoIt_String_)&amp;diff=11631"/>
		<updated>2013-04-18T20:32:44Z</updated>

		<summary type="html">&lt;p&gt;Czardas: /* _StringEqualSplit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
[[category:Snippets]]&lt;br /&gt;
&lt;br /&gt;
{{Snippet Credit Header}}&lt;br /&gt;
&lt;br /&gt;
== _StringEqualSplit ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 15769-czardas&lt;br /&gt;
| AuthorName = czardas&lt;br /&gt;
}}&lt;br /&gt;
&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;
Local $aArray = _StringEqualSplit(&amp;quot;12345678910&amp;quot;, 3)&lt;br /&gt;
_ArrayDisplay($aArray)&lt;br /&gt;
&lt;br /&gt;
Func _StringEqualSplit($sString, $iNumChars)&lt;br /&gt;
    If Not IsString($sString) Or $sString = &amp;quot;&amp;quot; Then Return SetError(1, 0, 0)&lt;br /&gt;
    If Not IsInt($iNumChars) Or $iNumChars &amp;lt; 1 Then Return SetError(2, 0, 0)&lt;br /&gt;
    Return StringRegExp($sString, &amp;quot;(?s).{1,&amp;quot; &amp;amp; $iNumChars &amp;amp; &amp;quot;}&amp;quot;, 3)&lt;br /&gt;
EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringGetChrCount ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 52-geosoft&lt;br /&gt;
| AuthorName = GEOSoft&lt;br /&gt;
| Desc = Check how many times the word &#039;test&#039; appears in the string. 1 = Case-sensitive or 0 = Non Case-sensitive.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Check how many times the word &#039;test&#039; appears in the string. 1 = Case-sensitive or 0 = Non Case-sensitive.&lt;br /&gt;
ConsoleWrite(_StringGetChrCount(&amp;quot;test teste test&amp;quot;, &amp;quot;TEST&amp;quot;, 0) &amp;amp; @CRLF)&lt;br /&gt;
&lt;br /&gt;
Func _StringGetChrCount($sStr, $sChr, $iCase = 0)&lt;br /&gt;
	If $iCase &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
		$iCase = 1&lt;br /&gt;
	EndIf&lt;br /&gt;
	StringReplace($sStr, $sChr, $sChr, 0, $iCase)&lt;br /&gt;
	Return @extended&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringGetChrCount&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringEqualSplit ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 15769-czardas&lt;br /&gt;
| AuthorName = czardas&lt;br /&gt;
| ModifierURL = 35302-guinness&lt;br /&gt;
| ModifierName = guinness&lt;br /&gt;
| Desc = Splits a string into an equal number of characters. The 0th index returns the number of items.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;#include &amp;lt;Array.au3&amp;gt;&lt;br /&gt;
#include &amp;lt;String.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Local $aSplitEqual = _StringEqualSplit(&#039;abcdefghijklmnopqrstuvwxyz&#039;, 5)&lt;br /&gt;
_ArrayDisplay($aSplitEqual)&lt;br /&gt;
&lt;br /&gt;
; By czardas &amp;amp; modified by guinness &amp;gt;&amp;gt; http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__20#entry992149&lt;br /&gt;
; Version: 1.10. AutoIt: V3.3.8.1&lt;br /&gt;
; Splits a string into an equal number of characters. The 0th index returns the number of items.&lt;br /&gt;
Func _StringEqualSplit($sString, $iNumChars)&lt;br /&gt;
	If IsString($sString) = 0 Or $sString == &#039;&#039; Then&lt;br /&gt;
		Return SetError(1, 0, 0)&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	If IsInt($iNumChars) = 0 Or $iNumChars &amp;lt; 1 Then&lt;br /&gt;
		Return SetError(2, 0, 0)&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	Local $aReturn = StringRegExp(_StringRepeat(&#039;0&#039;, 5) &amp;amp; $sString, &#039;(?s).{1,&#039; &amp;amp; $iNumChars &amp;amp; &#039;}&#039;, 3)&lt;br /&gt;
	$aReturn[0] = UBound($aReturn, 1) - 1&lt;br /&gt;
	Return $aReturn&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringEqualSplit&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringIsNum ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 38301-smartee&lt;br /&gt;
| AuthorName = smartee&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(StringIsNum(&#039;Example&#039;) &amp;amp; @CRLF)&lt;br /&gt;
ConsoleWrite(StringIsNum(&#039;123456&#039;) &amp;amp; @CRLF)&lt;br /&gt;
ConsoleWrite(StringIsNum(&#039;Example &amp;amp; 123456&#039;) &amp;amp; @CRLF)&lt;br /&gt;
&lt;br /&gt;
Func StringIsNum($sString)&lt;br /&gt;
    Return StringRegExp($sString, &amp;quot;^([0-9]*(\.[0-9]+){1}|[0-9]+(\.[0-9]*){0,1})$&amp;quot;) = 1&lt;br /&gt;
EndFunc   ;==&amp;gt;StringIsNum&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringRemoveLine ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 4813-smoke-n&lt;br /&gt;
| AuthorName = SmOke_N&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
Global $string = &#039;This is an example&#039; &amp;amp; @CRLF &amp;amp; &#039;Of deleting a line&#039; &amp;amp; @CRLF &amp;amp; &#039;If you know at least the beginning text of the line.&#039;&lt;br /&gt;
&lt;br /&gt;
MsgBox(0, &#039;Original&#039;, $string)&lt;br /&gt;
&lt;br /&gt;
Global $deleteline = _StringRemoveLine($string, &#039;Of deleting&#039;)&lt;br /&gt;
&lt;br /&gt;
MsgBox(0, &#039;Deleted Line&#039;, $deleteline)&lt;br /&gt;
&lt;br /&gt;
Func _StringRemoveLine($hFile, $sDelete)&lt;br /&gt;
    If FileExists($hFile) Then $hFile = FileRead($hFile);Remove If FileExists($hFile) Then &amp;lt;&amp;lt; only&lt;br /&gt;
    Local $nSNS = StringInStr($hFile, @CRLF &amp;amp; $sDelete) - 1&lt;br /&gt;
    Local $sSFH = StringLeft($hFile, $nSNS)&lt;br /&gt;
    Local $sRL = StringTrimLeft($hFile, StringLen($sSFH) + 2)&lt;br /&gt;
    Local $sLLEN = StringLen(StringLeft($sRL, StringInStr($sRL, @CRLF)))&lt;br /&gt;
    If Not $sLLEN Then $sLLEN = StringLen($sRL)&lt;br /&gt;
    Return $sSFH &amp;amp; StringTrimLeft($hFile, $sLLEN + $nSNS + 2)&lt;br /&gt;
EndFunc ;==&amp;gt;_StringRemoveLine()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringReplaceBlank ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 4813-smoke-n&lt;br /&gt;
| AuthorName = SmOke_N&lt;br /&gt;
| Desc = Remove blank lines from a File&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Remove blank lines from a File&lt;br /&gt;
Local $sString = &amp;quot;I am a string&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; _&lt;br /&gt;
		&amp;quot;With Empty Lines&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; _&lt;br /&gt;
		&amp;quot;Please Remove those empty lines&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MsgBox(4096, &amp;quot;Before&amp;quot;, $sString)&lt;br /&gt;
&lt;br /&gt;
$sString = _StringReplaceBlank($sString, 1)&lt;br /&gt;
&lt;br /&gt;
MsgBox(4096, &amp;quot;Replaced: &amp;quot; &amp;amp; @extended &amp;amp; &amp;quot; lines.&amp;quot;, $sString)&lt;br /&gt;
&lt;br /&gt;
Func _StringReplaceBlank($sString, $sSpaces = &amp;quot;&amp;quot;)&lt;br /&gt;
	If $sSpaces Then&lt;br /&gt;
		$sSpaces = &amp;quot;\s*&amp;quot;&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	$sString = StringRegExpReplace($sString, &amp;quot;(?s)\r\n&amp;quot; &amp;amp; $sSpaces &amp;amp; &amp;quot;\r\n&amp;quot;, @CRLF)&lt;br /&gt;
&lt;br /&gt;
	If @extended Then&lt;br /&gt;
		Return SetError(0, @extended, $sString)&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	Return SetError(1, 0, $sString)&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringReplaceBlank&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringTrimLeftIsEqual ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 35302-guinness&lt;br /&gt;
| AuthorName = guinness&lt;br /&gt;
| Desc = Strip a character/word from the leftmost part of a string.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(_StringTrimLeftIsEqual(&#039;C:\Test&#039;, &#039;C:\&#039;) &amp;amp; @CRLF) ; Returns Test as the string &#039;C:\&#039; is stripped from the left.&lt;br /&gt;
ConsoleWrite(_StringTrimLeftIsEqual(&#039;C:\Test\&#039;, &#039;C&#039;) &amp;amp; @CRLF) ; Returns :\Test as the character &#039;C&#039; is stripped from the left.&lt;br /&gt;
ConsoleWrite(_StringTrimLeftIsEqual(&#039;C:\Test\&#039;, &#039;Test&#039;) &amp;amp; @CRLF) ; Returns the initial string as the string &#039;Test&#039; was not found to the leftmost part of the string.&lt;br /&gt;
&lt;br /&gt;
; Version: 1.00. AutoIt: V3.3.8.1&lt;br /&gt;
; Strip a character/word from the leftmost part of a string.&lt;br /&gt;
Func _StringTrimLeftIsEqual($sString, $sStringTrim)&lt;br /&gt;
    Local $aStringTrim[2] = [0, StringLen($sStringTrim)]&lt;br /&gt;
    Return StringTrimLeft($sString, $aStringTrim[Number(StringLeft($sString, $aStringTrim[1]) == $sStringTrim)])&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringTrimLeftIsEqual&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringTrimRightIsEqual ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 35302-guinness&lt;br /&gt;
| AuthorName = guinness&lt;br /&gt;
| Desc = Strip a character/word from the rightmost part of a string.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(_StringTrimRightIsEqual(&#039;C:\Test&#039;, &#039;Test&#039;) &amp;amp; @CRLF) ; Returns C:\ as the string &#039;Test&#039; is stripped from the right.&lt;br /&gt;
ConsoleWrite(_StringTrimRightIsEqual(&#039;C:\Test\&#039;, &#039;\&#039;) &amp;amp; @CRLF) ; Returns C:\Test as the character &#039;\&#039; is stripped from the right.&lt;br /&gt;
ConsoleWrite(_StringTrimRightIsEqual(&#039;C:\Test\&#039;, &#039;Test&#039;) &amp;amp; @CRLF) ; Returns the initial string as the string &#039;Test&#039; was not found to the rightmost part of the string.&lt;br /&gt;
&lt;br /&gt;
; Version: 1.00. AutoIt: V3.3.8.1&lt;br /&gt;
; Strip a character/word from the rightmost part of a string.&lt;br /&gt;
Func _StringTrimRightIsEqual($sString, $sStringTrim)&lt;br /&gt;
    Local $aStringTrim[2] = [0, StringLen($sStringTrim)]&lt;br /&gt;
    Return StringTrimRight($sString, $aStringTrim[Number(StringRight($sString, $aStringTrim[1]) == $sStringTrim)])&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringTrimRightIsEqual&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringRegExpSplit ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 69238-dany&lt;br /&gt;
| AuthorName = dany&lt;br /&gt;
| Desc = Split a string based on a regular expression.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Split a string based on a regular expression.&lt;br /&gt;
; http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__120#entry1036533&lt;br /&gt;
#include &amp;lt;Array.au3&amp;gt; ; Only needed for example.&lt;br /&gt;
Global $sString = &#039;List:&#039; &amp;amp; _&lt;br /&gt;
        &#039;1. Bananas&#039; &amp;amp; _&lt;br /&gt;
        &#039;2. Coconuts&#039; &amp;amp; _&lt;br /&gt;
        &#039;3. Apples&#039;&lt;br /&gt;
_ArrayDisplay(_StringRegExpSplit($sString, &#039;[0-9]+\. ?&#039;)) ; Use the numbering (#.) to split on.&lt;br /&gt;
Global $bBinary = Binary($sString)&lt;br /&gt;
_ArrayDisplay(_StringRegExpSplit($bBinary, &#039;3[0-9]2E(20)?&#039;))&lt;br /&gt;
&lt;br /&gt;
; #FUNCTION# ===================================================================&lt;br /&gt;
; Name...........: _StringRegExpSplit&lt;br /&gt;
; Description ...: Split a string according to a regular expression.&lt;br /&gt;
; Syntax.........: _StringRegExpSplit($sString, $sPattern)&lt;br /&gt;
; Parameters ....: $sString - String: String to split.&lt;br /&gt;
;                  $sPattern - String: Regular expression to split on.&lt;br /&gt;
; Return values .: Success - Array: Array of substrings, the total is in $array[0].&lt;br /&gt;
;                  Failure - Array: The count is 1 ($array[0]) and the full string is returned ($array[1]) and sets @error:&lt;br /&gt;
;                  |1 Delimiter not found.&lt;br /&gt;
;                  |2 Bad RegExp pattern, @extended contains the offset of the error in the pattern.&lt;br /&gt;
;                  |3 No suitable placeholder delimiter could be constructed.&lt;br /&gt;
; Author ........: dany&lt;br /&gt;
; Modified ......: czardas, AZJIO&lt;br /&gt;
; Remarks .......:&lt;br /&gt;
; Related .......:&lt;br /&gt;
; ==============================================================================&lt;br /&gt;
Func _StringRegExpSplit($sString, $sPattern)&lt;br /&gt;
	Local $sSplit, $sDelim, $aError[2] = [1, $sString]&lt;br /&gt;
	For $i = 1 To 31&lt;br /&gt;
		$sDelim &amp;amp;= Chr($i)&lt;br /&gt;
		If Not StringInStr($sString, $sDelim) Then ExitLoop&lt;br /&gt;
		If 32 = StringLen($sDelim) Then Return SetError(3, 0, $aError)&lt;br /&gt;
	Next&lt;br /&gt;
	$sSplit = StringRegExpReplace($sString, $sPattern, $sDelim)&lt;br /&gt;
	If @error Then Return SetError(2, @extended, $aError)&lt;br /&gt;
	If @extended = 0 Then Return SetError(1, 0, $aError)&lt;br /&gt;
	If Not IsBinary($sString) Then Return StringSplit($sSplit, $sDelim, 1)&lt;br /&gt;
	$sSplit = StringSplit($sSplit, $sDelim, 1)&lt;br /&gt;
	For $i = 2 To $sSplit[0]&lt;br /&gt;
		$sSplit[$i] = &#039;0x&#039; &amp;amp; $sSplit[$i]&lt;br /&gt;
	Next&lt;br /&gt;
	Return $sSplit&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringRegExpSplit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=Snippets_(_AutoIt_String_)&amp;diff=11630</id>
		<title>Snippets ( AutoIt String )</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=Snippets_(_AutoIt_String_)&amp;diff=11630"/>
		<updated>2013-04-18T19:30:36Z</updated>

		<summary type="html">&lt;p&gt;Czardas: /* _StringEqualSplit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__TOC__&lt;br /&gt;
&lt;br /&gt;
[[category:Snippets]]&lt;br /&gt;
&lt;br /&gt;
{{Snippet Credit Header}}&lt;br /&gt;
&lt;br /&gt;
== _StringEqualSplit ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 15769-czardas&lt;br /&gt;
| AuthorName = czardas&lt;br /&gt;
}}&lt;br /&gt;
&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;
Global $aArray = _StringEqualSplit(&amp;quot;abcdefghijklmnopqrstuvwxyz&amp;quot;, 5)&lt;br /&gt;
_ArrayDisplay($aArray)&lt;br /&gt;
&lt;br /&gt;
Func _StringEqualSplit($sString, $iNumChars)&lt;br /&gt;
    If Not IsString($sString) Or $sString = &amp;quot;&amp;quot; Then Return SetError(1, 0, 0)&lt;br /&gt;
    If Not IsInt($iNumChars) Or $iNumChars &amp;lt; 1 Then Return SetError(2, 0, 0)&lt;br /&gt;
    Return StringRegExp($sString, &amp;quot;(?s).{1,&amp;quot; &amp;amp; $iNumChars &amp;amp; &amp;quot;}&amp;quot;, 3)&lt;br /&gt;
EndFunc&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringGetChrCount ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 52-geosoft&lt;br /&gt;
| AuthorName = GEOSoft&lt;br /&gt;
| Desc = Check how many times the word &#039;test&#039; appears in the string. 1 = Case-sensitive or 0 = Non Case-sensitive.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Check how many times the word &#039;test&#039; appears in the string. 1 = Case-sensitive or 0 = Non Case-sensitive.&lt;br /&gt;
ConsoleWrite(_StringGetChrCount(&amp;quot;test teste test&amp;quot;, &amp;quot;TEST&amp;quot;, 0) &amp;amp; @CRLF)&lt;br /&gt;
&lt;br /&gt;
Func _StringGetChrCount($sStr, $sChr, $iCase = 0)&lt;br /&gt;
	If $iCase &amp;lt;&amp;gt; 0 Then&lt;br /&gt;
		$iCase = 1&lt;br /&gt;
	EndIf&lt;br /&gt;
	StringReplace($sStr, $sChr, $sChr, 0, $iCase)&lt;br /&gt;
	Return @extended&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringGetChrCount&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringEqualSplit ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 15769-czardas&lt;br /&gt;
| AuthorName = czardas&lt;br /&gt;
| ModifierURL = 35302-guinness&lt;br /&gt;
| ModifierName = guinness&lt;br /&gt;
| Desc = Splits a string into an equal number of characters. The 0th index returns the number of items.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;#include &amp;lt;Array.au3&amp;gt;&lt;br /&gt;
#include &amp;lt;String.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Local $aSplitEqual = _StringEqualSplit(&#039;abcdefghijklmnopqrstuvwxyz&#039;, 5)&lt;br /&gt;
_ArrayDisplay($aSplitEqual)&lt;br /&gt;
&lt;br /&gt;
; By czardas &amp;amp; modified by guinness &amp;gt;&amp;gt; http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__20#entry992149&lt;br /&gt;
; Version: 1.10. AutoIt: V3.3.8.1&lt;br /&gt;
; Splits a string into an equal number of characters. The 0th index returns the number of items.&lt;br /&gt;
Func _StringEqualSplit($sString, $iNumChars)&lt;br /&gt;
	If IsString($sString) = 0 Or $sString == &#039;&#039; Then&lt;br /&gt;
		Return SetError(1, 0, 0)&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	If IsInt($iNumChars) = 0 Or $iNumChars &amp;lt; 1 Then&lt;br /&gt;
		Return SetError(2, 0, 0)&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	Local $aReturn = StringRegExp(_StringRepeat(&#039;0&#039;, 5) &amp;amp; $sString, &#039;(?s).{1,&#039; &amp;amp; $iNumChars &amp;amp; &#039;}&#039;, 3)&lt;br /&gt;
	$aReturn[0] = UBound($aReturn, 1) - 1&lt;br /&gt;
	Return $aReturn&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringEqualSplit&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringIsNum ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 38301-smartee&lt;br /&gt;
| AuthorName = smartee&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(StringIsNum(&#039;Example&#039;) &amp;amp; @CRLF)&lt;br /&gt;
ConsoleWrite(StringIsNum(&#039;123456&#039;) &amp;amp; @CRLF)&lt;br /&gt;
ConsoleWrite(StringIsNum(&#039;Example &amp;amp; 123456&#039;) &amp;amp; @CRLF)&lt;br /&gt;
&lt;br /&gt;
Func StringIsNum($sString)&lt;br /&gt;
    Return StringRegExp($sString, &amp;quot;^([0-9]*(\.[0-9]+){1}|[0-9]+(\.[0-9]*){0,1})$&amp;quot;) = 1&lt;br /&gt;
EndFunc   ;==&amp;gt;StringIsNum&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringRemoveLine ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 4813-smoke-n&lt;br /&gt;
| AuthorName = SmOke_N&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
Global $string = &#039;This is an example&#039; &amp;amp; @CRLF &amp;amp; &#039;Of deleting a line&#039; &amp;amp; @CRLF &amp;amp; &#039;If you know at least the beginning text of the line.&#039;&lt;br /&gt;
&lt;br /&gt;
MsgBox(0, &#039;Original&#039;, $string)&lt;br /&gt;
&lt;br /&gt;
Global $deleteline = _StringRemoveLine($string, &#039;Of deleting&#039;)&lt;br /&gt;
&lt;br /&gt;
MsgBox(0, &#039;Deleted Line&#039;, $deleteline)&lt;br /&gt;
&lt;br /&gt;
Func _StringRemoveLine($hFile, $sDelete)&lt;br /&gt;
    If FileExists($hFile) Then $hFile = FileRead($hFile);Remove If FileExists($hFile) Then &amp;lt;&amp;lt; only&lt;br /&gt;
    Local $nSNS = StringInStr($hFile, @CRLF &amp;amp; $sDelete) - 1&lt;br /&gt;
    Local $sSFH = StringLeft($hFile, $nSNS)&lt;br /&gt;
    Local $sRL = StringTrimLeft($hFile, StringLen($sSFH) + 2)&lt;br /&gt;
    Local $sLLEN = StringLen(StringLeft($sRL, StringInStr($sRL, @CRLF)))&lt;br /&gt;
    If Not $sLLEN Then $sLLEN = StringLen($sRL)&lt;br /&gt;
    Return $sSFH &amp;amp; StringTrimLeft($hFile, $sLLEN + $nSNS + 2)&lt;br /&gt;
EndFunc ;==&amp;gt;_StringRemoveLine()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringReplaceBlank ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 4813-smoke-n&lt;br /&gt;
| AuthorName = SmOke_N&lt;br /&gt;
| Desc = Remove blank lines from a File&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Remove blank lines from a File&lt;br /&gt;
Local $sString = &amp;quot;I am a string&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; _&lt;br /&gt;
		&amp;quot;With Empty Lines&amp;quot; &amp;amp; @CRLF &amp;amp; @CRLF &amp;amp; _&lt;br /&gt;
		&amp;quot;Please Remove those empty lines&amp;quot;&lt;br /&gt;
&lt;br /&gt;
MsgBox(4096, &amp;quot;Before&amp;quot;, $sString)&lt;br /&gt;
&lt;br /&gt;
$sString = _StringReplaceBlank($sString, 1)&lt;br /&gt;
&lt;br /&gt;
MsgBox(4096, &amp;quot;Replaced: &amp;quot; &amp;amp; @extended &amp;amp; &amp;quot; lines.&amp;quot;, $sString)&lt;br /&gt;
&lt;br /&gt;
Func _StringReplaceBlank($sString, $sSpaces = &amp;quot;&amp;quot;)&lt;br /&gt;
	If $sSpaces Then&lt;br /&gt;
		$sSpaces = &amp;quot;\s*&amp;quot;&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	$sString = StringRegExpReplace($sString, &amp;quot;(?s)\r\n&amp;quot; &amp;amp; $sSpaces &amp;amp; &amp;quot;\r\n&amp;quot;, @CRLF)&lt;br /&gt;
&lt;br /&gt;
	If @extended Then&lt;br /&gt;
		Return SetError(0, @extended, $sString)&lt;br /&gt;
	EndIf&lt;br /&gt;
&lt;br /&gt;
	Return SetError(1, 0, $sString)&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringReplaceBlank&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringTrimLeftIsEqual ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 35302-guinness&lt;br /&gt;
| AuthorName = guinness&lt;br /&gt;
| Desc = Strip a character/word from the leftmost part of a string.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(_StringTrimLeftIsEqual(&#039;C:\Test&#039;, &#039;C:\&#039;) &amp;amp; @CRLF) ; Returns Test as the string &#039;C:\&#039; is stripped from the left.&lt;br /&gt;
ConsoleWrite(_StringTrimLeftIsEqual(&#039;C:\Test\&#039;, &#039;C&#039;) &amp;amp; @CRLF) ; Returns :\Test as the character &#039;C&#039; is stripped from the left.&lt;br /&gt;
ConsoleWrite(_StringTrimLeftIsEqual(&#039;C:\Test\&#039;, &#039;Test&#039;) &amp;amp; @CRLF) ; Returns the initial string as the string &#039;Test&#039; was not found to the leftmost part of the string.&lt;br /&gt;
&lt;br /&gt;
; Version: 1.00. AutoIt: V3.3.8.1&lt;br /&gt;
; Strip a character/word from the leftmost part of a string.&lt;br /&gt;
Func _StringTrimLeftIsEqual($sString, $sStringTrim)&lt;br /&gt;
    Local $aStringTrim[2] = [0, StringLen($sStringTrim)]&lt;br /&gt;
    Return StringTrimLeft($sString, $aStringTrim[Number(StringLeft($sString, $aStringTrim[1]) == $sStringTrim)])&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringTrimLeftIsEqual&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringTrimRightIsEqual ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 35302-guinness&lt;br /&gt;
| AuthorName = guinness&lt;br /&gt;
| Desc = Strip a character/word from the rightmost part of a string.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(_StringTrimRightIsEqual(&#039;C:\Test&#039;, &#039;Test&#039;) &amp;amp; @CRLF) ; Returns C:\ as the string &#039;Test&#039; is stripped from the right.&lt;br /&gt;
ConsoleWrite(_StringTrimRightIsEqual(&#039;C:\Test\&#039;, &#039;\&#039;) &amp;amp; @CRLF) ; Returns C:\Test as the character &#039;\&#039; is stripped from the right.&lt;br /&gt;
ConsoleWrite(_StringTrimRightIsEqual(&#039;C:\Test\&#039;, &#039;Test&#039;) &amp;amp; @CRLF) ; Returns the initial string as the string &#039;Test&#039; was not found to the rightmost part of the string.&lt;br /&gt;
&lt;br /&gt;
; Version: 1.00. AutoIt: V3.3.8.1&lt;br /&gt;
; Strip a character/word from the rightmost part of a string.&lt;br /&gt;
Func _StringTrimRightIsEqual($sString, $sStringTrim)&lt;br /&gt;
    Local $aStringTrim[2] = [0, StringLen($sStringTrim)]&lt;br /&gt;
    Return StringTrimRight($sString, $aStringTrim[Number(StringRight($sString, $aStringTrim[1]) == $sStringTrim)])&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringTrimRightIsEqual&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;br /&gt;
&lt;br /&gt;
== _StringRegExpSplit ==&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL = 69238-dany&lt;br /&gt;
| AuthorName = dany&lt;br /&gt;
| Desc = Split a string based on a regular expression.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
; Split a string based on a regular expression.&lt;br /&gt;
; http://www.autoitscript.com/forum/topic/139260-autoit-snippets/page__st__120#entry1036533&lt;br /&gt;
#include &amp;lt;Array.au3&amp;gt; ; Only needed for example.&lt;br /&gt;
Global $sString = &#039;List:&#039; &amp;amp; _&lt;br /&gt;
        &#039;1. Bananas&#039; &amp;amp; _&lt;br /&gt;
        &#039;2. Coconuts&#039; &amp;amp; _&lt;br /&gt;
        &#039;3. Apples&#039;&lt;br /&gt;
_ArrayDisplay(_StringRegExpSplit($sString, &#039;[0-9]+\. ?&#039;)) ; Use the numbering (#.) to split on.&lt;br /&gt;
Global $bBinary = Binary($sString)&lt;br /&gt;
_ArrayDisplay(_StringRegExpSplit($bBinary, &#039;3[0-9]2E(20)?&#039;))&lt;br /&gt;
&lt;br /&gt;
; #FUNCTION# ===================================================================&lt;br /&gt;
; Name...........: _StringRegExpSplit&lt;br /&gt;
; Description ...: Split a string according to a regular expression.&lt;br /&gt;
; Syntax.........: _StringRegExpSplit($sString, $sPattern)&lt;br /&gt;
; Parameters ....: $sString - String: String to split.&lt;br /&gt;
;                  $sPattern - String: Regular expression to split on.&lt;br /&gt;
; Return values .: Success - Array: Array of substrings, the total is in $array[0].&lt;br /&gt;
;                  Failure - Array: The count is 1 ($array[0]) and the full string is returned ($array[1]) and sets @error:&lt;br /&gt;
;                  |1 Delimiter not found.&lt;br /&gt;
;                  |2 Bad RegExp pattern, @extended contains the offset of the error in the pattern.&lt;br /&gt;
;                  |3 No suitable placeholder delimiter could be constructed.&lt;br /&gt;
; Author ........: dany&lt;br /&gt;
; Modified ......: czardas, AZJIO&lt;br /&gt;
; Remarks .......:&lt;br /&gt;
; Related .......:&lt;br /&gt;
; ==============================================================================&lt;br /&gt;
Func _StringRegExpSplit($sString, $sPattern)&lt;br /&gt;
	Local $sSplit, $sDelim, $aError[2] = [1, $sString]&lt;br /&gt;
	For $i = 1 To 31&lt;br /&gt;
		$sDelim &amp;amp;= Chr($i)&lt;br /&gt;
		If Not StringInStr($sString, $sDelim) Then ExitLoop&lt;br /&gt;
		If 32 = StringLen($sDelim) Then Return SetError(3, 0, $aError)&lt;br /&gt;
	Next&lt;br /&gt;
	$sSplit = StringRegExpReplace($sString, $sPattern, $sDelim)&lt;br /&gt;
	If @error Then Return SetError(2, @extended, $aError)&lt;br /&gt;
	If @extended = 0 Then Return SetError(1, 0, $aError)&lt;br /&gt;
	If Not IsBinary($sString) Then Return StringSplit($sSplit, $sDelim, 1)&lt;br /&gt;
	$sSplit = StringSplit($sSplit, $sDelim, 1)&lt;br /&gt;
	For $i = 2 To $sSplit[0]&lt;br /&gt;
		$sSplit[$i] = &#039;0x&#039; &amp;amp; $sSplit[$i]&lt;br /&gt;
	Next&lt;br /&gt;
	Return $sSplit&lt;br /&gt;
EndFunc   ;==&amp;gt;_StringRegExpSplit&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | Return To Contents]]&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=AutoIt_Snippets&amp;diff=11460</id>
		<title>AutoIt Snippets</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=AutoIt_Snippets&amp;diff=11460"/>
		<updated>2012-11-19T11:22:24Z</updated>

		<summary type="html">&lt;p&gt;Czardas: removed one word.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome To The AutoIt Snippets Page - ( &#039;&#039;&#039;&#039;&#039;Snippet - A short reusable piece of computer code&#039;&#039;&#039;&#039;&#039; ).  &lt;br /&gt;
&lt;br /&gt;
Snippets are generally single functions or small pieces of code which can be incorporated into a script to add extra functionality. This section covers a wide variety of subjects and uses. Examples may include anything from finding if an internet connection is working to retrieving the date Windows was installed. This page is intended to give easy access to the functions.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
[[category:Snippets]]&lt;br /&gt;
&lt;br /&gt;
{{Snippet Credit Header}}&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
=== _IsInternetConnected ===&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL=35302-guinness&lt;br /&gt;
| AuthorName=guinness&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(&amp;quot;Internet Is Connected&amp;quot; &amp;amp; &amp;quot; = &amp;quot; &amp;amp; _IsInternetConnected() &amp;amp; @CRLF) ; ( Returns &amp;quot;True&amp;quot; Or &amp;quot;False&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
Func _IsInternetConnected()&lt;br /&gt;
    Local $aReturn = DllCall(&#039;connect.dll&#039;, &#039;long&#039;, &#039;IsInternetConnected&#039;)&lt;br /&gt;
    If @error Then&lt;br /&gt;
        Return SetError(1, 0, False)&lt;br /&gt;
    EndIf&lt;br /&gt;
    Return $aReturn[0] = 0&lt;br /&gt;
EndFunc   ;==&amp;gt;_IsInternetConnected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | ReturnToContents]]&lt;br /&gt;
&lt;br /&gt;
=== _PasswordCrypt ===&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL=4920-valuater&lt;br /&gt;
| AuthorName=Valuater&lt;br /&gt;
| ModifierURL=35302-guinness&lt;br /&gt;
| ModifierName=guinness&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;Crypt.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Local $sGenericPassword_1 = &#039;Password@AutoIt&#039;, $sGenericPassword_2 = &#039;NewPassword@AutoIt&#039;, $sSavePath = @ScriptDir &amp;amp; &#039;\License.dat&#039;&lt;br /&gt;
ConsoleWrite(&#039;1. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_1, $sSavePath) &amp;amp; @CRLF) ; Write the password to a file located in the @ScriptDir. The password we wrote is returned by the function.&lt;br /&gt;
ConsoleWrite(&#039;2. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_1, $sSavePath) &amp;amp; @CRLF) ; Since the password has been written already, we now want to check if the user has entered the password correctly. Returns True or False.&lt;br /&gt;
ConsoleWrite(&#039;3. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_2, $sSavePath, 1) &amp;amp; @CRLF) ; Overwrite the old password with a new one.&lt;br /&gt;
ConsoleWrite(&#039;4. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_1, $sSavePath) &amp;amp; @CRLF) ; Check the password matches. This will fail as we&#039;re checking the old password against the new one.&lt;br /&gt;
FileDelete($sSavePath)&lt;br /&gt;
&lt;br /&gt;
Func _PasswordCrypt($sPassword, $sFilePath, $iOverwrite = 0) ; By guinness, idea by Valuater.&lt;br /&gt;
    If FileExists($sFilePath) And $iOverwrite = 0 Then&lt;br /&gt;
        Return BinaryToString(_Crypt_DecryptData(IniRead($sFilePath, &#039;PasswordKey&#039;, &#039;Password&#039;, &#039;&#039;), @ComputerName, $CALG_AES_256)) == $sPassword&lt;br /&gt;
    Else&lt;br /&gt;
        If IniWrite($sFilePath, &#039;PasswordKey&#039;, &#039;Password&#039;, _Crypt_EncryptData($sPassword, @ComputerName, $CALG_AES_256)) Then&lt;br /&gt;
            Return $sPassword&lt;br /&gt;
        EndIf&lt;br /&gt;
    EndIf&lt;br /&gt;
    Return SetError(1, 0, &#039;&#039;)&lt;br /&gt;
EndFunc   ;==&amp;gt;_PasswordCrypt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | ReturnToContents]]&lt;br /&gt;
&lt;br /&gt;
== Snippet Creation Help ==&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Creation )| Snippets Creation Examples ]]&lt;br /&gt;
&lt;br /&gt;
== AutoIt Snippets Collection ==&lt;br /&gt;
&lt;br /&gt;
=== AutoIt &amp;lt;small&amp;gt;- AutoIt Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( AutoIt Audio )| Audio Related ]] ( Last Updated - 16:58, 21 May 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt )| AutoIt Miscellaneous ]] ( Last Updated - 12:22, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt Array )| AutoIt Array Related]] ( Last Updated - 13:17, 21 May 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt Mouse &amp;amp; Keyboard )| AutoIt Mouse &amp;amp; Keyboard ]] ( Last Updated - 07:52, 3 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt String )| AutoIt String Related ]] ( Last Updated - 12:45, 1 August 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== GUI &amp;lt;small&amp;gt;- Anything GUI related.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Checkboxes )| Checkboxes &amp;amp; Radio ]]&lt;br /&gt;
* [[Snippets ( GUI )| GUI ]]  ( Last Updated - 12:11, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Graphics )| Graphics And Images ]] ( Last Updated - 12:37, 1 August 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Hardware &amp;lt;small&amp;gt;- Hardware Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Hardware Information )| Hardware Information ]] ( Last Updated - 11:02, 30 April 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Numbers &amp;lt;small&amp;gt;- Math, Number &amp;amp; Time Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Math &amp;amp; Numbers )| Math &amp;amp; Numbers ]] ( Last Updated - 12:07, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Time &amp;amp; Date )| Time &amp;amp; Date ]] ( Last Updated - 14:11, 21 May 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Miscellaneous &amp;lt;small&amp;gt;- All Other Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Miscellaneous )| Miscellaneous ]] ( Last Updated - 12:35, 1 August 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Networking &amp;lt;small&amp;gt;- Network, Wireless and Internet Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Network )| Network ]] ( Last Updated - 12:33, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Internet )|Internet ]] ( Last Updated - 12:30, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Wireless )|Wireless ]]&lt;br /&gt;
&lt;br /&gt;
=== Windows &amp;lt;small&amp;gt;- Windows Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( CMD ) |CMD - Commandline ]]&lt;br /&gt;
* [[Snippets ( Files &amp;amp; Folders )| Files &amp;amp; Folders ]] ( Last Updated - 12:16, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Registry )| Registry ]]&lt;br /&gt;
* [[Snippets ( Windows Information )| Windows Information ]] ( Last Updated - 11:27, 30 April 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Windows OS )| Windows OS ]] ( Last Updated - 14:35, 21 May 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Windows Settings )| Windows Settings ]] ( Last Updated - 12:07, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Windows Users )| Windows Account Management ]] ( Last Updated - 14:13, 21 May 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
== Other Links ==&lt;br /&gt;
&lt;br /&gt;
[http://www.autoitscript.com/wiki/Free_Software Free Software Written in AutoIt]&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
	<entry>
		<id>https://www.autoitscript.com/w/index.php?title=AutoIt_Snippets&amp;diff=11459</id>
		<title>AutoIt Snippets</title>
		<link rel="alternate" type="text/html" href="https://www.autoitscript.com/w/index.php?title=AutoIt_Snippets&amp;diff=11459"/>
		<updated>2012-11-19T04:56:35Z</updated>

		<summary type="html">&lt;p&gt;Czardas: Changes made to the wording of the first paragraph to improve readfability..&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Welcome To The AutoIt Snippets Page - ( &#039;&#039;&#039;&#039;&#039;Snippet - A short reusable piece of computer code&#039;&#039;&#039;&#039;&#039; ).  &lt;br /&gt;
&lt;br /&gt;
Snippets are generally single functions or small pieces of AutoIt code which can be incorporated into a script to add extra functionality. This section covers a wide variety of subjects and uses. Examples may include anything from finding if an internet connection is working to retrieving the date Windows was installed. This page is intended to give easy access to the functions.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
[[category:Snippets]]&lt;br /&gt;
&lt;br /&gt;
{{Snippet Credit Header}}&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
=== _IsInternetConnected ===&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL=35302-guinness&lt;br /&gt;
| AuthorName=guinness&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
ConsoleWrite(&amp;quot;Internet Is Connected&amp;quot; &amp;amp; &amp;quot; = &amp;quot; &amp;amp; _IsInternetConnected() &amp;amp; @CRLF) ; ( Returns &amp;quot;True&amp;quot; Or &amp;quot;False&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
Func _IsInternetConnected()&lt;br /&gt;
    Local $aReturn = DllCall(&#039;connect.dll&#039;, &#039;long&#039;, &#039;IsInternetConnected&#039;)&lt;br /&gt;
    If @error Then&lt;br /&gt;
        Return SetError(1, 0, False)&lt;br /&gt;
    EndIf&lt;br /&gt;
    Return $aReturn[0] = 0&lt;br /&gt;
EndFunc   ;==&amp;gt;_IsInternetConnected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | ReturnToContents]]&lt;br /&gt;
&lt;br /&gt;
=== _PasswordCrypt ===&lt;br /&gt;
&lt;br /&gt;
{{Snippet Header&lt;br /&gt;
| AuthorURL=4920-valuater&lt;br /&gt;
| AuthorName=Valuater&lt;br /&gt;
| ModifierURL=35302-guinness&lt;br /&gt;
| ModifierName=guinness&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;autoit&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;Crypt.au3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Local $sGenericPassword_1 = &#039;Password@AutoIt&#039;, $sGenericPassword_2 = &#039;NewPassword@AutoIt&#039;, $sSavePath = @ScriptDir &amp;amp; &#039;\License.dat&#039;&lt;br /&gt;
ConsoleWrite(&#039;1. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_1, $sSavePath) &amp;amp; @CRLF) ; Write the password to a file located in the @ScriptDir. The password we wrote is returned by the function.&lt;br /&gt;
ConsoleWrite(&#039;2. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_1, $sSavePath) &amp;amp; @CRLF) ; Since the password has been written already, we now want to check if the user has entered the password correctly. Returns True or False.&lt;br /&gt;
ConsoleWrite(&#039;3. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_2, $sSavePath, 1) &amp;amp; @CRLF) ; Overwrite the old password with a new one.&lt;br /&gt;
ConsoleWrite(&#039;4. &#039; &amp;amp; _PasswordCrypt($sGenericPassword_1, $sSavePath) &amp;amp; @CRLF) ; Check the password matches. This will fail as we&#039;re checking the old password against the new one.&lt;br /&gt;
FileDelete($sSavePath)&lt;br /&gt;
&lt;br /&gt;
Func _PasswordCrypt($sPassword, $sFilePath, $iOverwrite = 0) ; By guinness, idea by Valuater.&lt;br /&gt;
    If FileExists($sFilePath) And $iOverwrite = 0 Then&lt;br /&gt;
        Return BinaryToString(_Crypt_DecryptData(IniRead($sFilePath, &#039;PasswordKey&#039;, &#039;Password&#039;, &#039;&#039;), @ComputerName, $CALG_AES_256)) == $sPassword&lt;br /&gt;
    Else&lt;br /&gt;
        If IniWrite($sFilePath, &#039;PasswordKey&#039;, &#039;Password&#039;, _Crypt_EncryptData($sPassword, @ComputerName, $CALG_AES_256)) Then&lt;br /&gt;
            Return $sPassword&lt;br /&gt;
        EndIf&lt;br /&gt;
    EndIf&lt;br /&gt;
    Return SetError(1, 0, &#039;&#039;)&lt;br /&gt;
EndFunc   ;==&amp;gt;_PasswordCrypt&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[#top | ReturnToContents]]&lt;br /&gt;
&lt;br /&gt;
== Snippet Creation Help ==&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Creation )| Snippets Creation Examples ]]&lt;br /&gt;
&lt;br /&gt;
== AutoIt Snippets Collection ==&lt;br /&gt;
&lt;br /&gt;
=== AutoIt &amp;lt;small&amp;gt;- AutoIt Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( AutoIt Audio )| Audio Related ]] ( Last Updated - 16:58, 21 May 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt )| AutoIt Miscellaneous ]] ( Last Updated - 12:22, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt Array )| AutoIt Array Related]] ( Last Updated - 13:17, 21 May 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt Mouse &amp;amp; Keyboard )| AutoIt Mouse &amp;amp; Keyboard ]] ( Last Updated - 07:52, 3 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( AutoIt String )| AutoIt String Related ]] ( Last Updated - 12:45, 1 August 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== GUI &amp;lt;small&amp;gt;- Anything GUI related.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Checkboxes )| Checkboxes &amp;amp; Radio ]]&lt;br /&gt;
* [[Snippets ( GUI )| GUI ]]  ( Last Updated - 12:11, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Graphics )| Graphics And Images ]] ( Last Updated - 12:37, 1 August 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Hardware &amp;lt;small&amp;gt;- Hardware Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Hardware Information )| Hardware Information ]] ( Last Updated - 11:02, 30 April 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Numbers &amp;lt;small&amp;gt;- Math, Number &amp;amp; Time Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Math &amp;amp; Numbers )| Math &amp;amp; Numbers ]] ( Last Updated - 12:07, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Time &amp;amp; Date )| Time &amp;amp; Date ]] ( Last Updated - 14:11, 21 May 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Miscellaneous &amp;lt;small&amp;gt;- All Other Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Miscellaneous )| Miscellaneous ]] ( Last Updated - 12:35, 1 August 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
=== Networking &amp;lt;small&amp;gt;- Network, Wireless and Internet Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( Network )| Network ]] ( Last Updated - 12:33, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Internet )|Internet ]] ( Last Updated - 12:30, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Wireless )|Wireless ]]&lt;br /&gt;
&lt;br /&gt;
=== Windows &amp;lt;small&amp;gt;- Windows Examples.&amp;lt;/small&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
* [[Snippets ( CMD ) |CMD - Commandline ]]&lt;br /&gt;
* [[Snippets ( Files &amp;amp; Folders )| Files &amp;amp; Folders ]] ( Last Updated - 12:16, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Registry )| Registry ]]&lt;br /&gt;
* [[Snippets ( Windows Information )| Windows Information ]] ( Last Updated - 11:27, 30 April 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Windows OS )| Windows OS ]] ( Last Updated - 14:35, 21 May 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Windows Settings )| Windows Settings ]] ( Last Updated - 12:07, 1 August 2012 (BST) )&lt;br /&gt;
* [[Snippets ( Windows Users )| Windows Account Management ]] ( Last Updated - 14:13, 21 May 2012 (BST) )&lt;br /&gt;
&lt;br /&gt;
== Other Links ==&lt;br /&gt;
&lt;br /&gt;
[http://www.autoitscript.com/wiki/Free_Software Free Software Written in AutoIt]&lt;/div&gt;</summary>
		<author><name>Czardas</name></author>
	</entry>
</feed>