Best coding practices: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 1: Line 1:
Outlined in this section, is a detailed explanation of what are to be considered the best coding practices within AutoIt.
Outlined in this section is a detailed explanation of what is to be considered the best coding practices within AutoIt.


__TOC__
__TOC__
Line 28: Line 28:
| p || Pointers || $p = 0
| p || Pointers || $p = 0
|-
|-
| tag || Structures definition || $tag = "" (should directy be filled)
| tag || Structures definition || $tag = "" (should directly be filled)
|-
|-
| t || Structures || $t = 0
| t || Structures || $t = 0

Revision as of 18:22, 15 August 2013

Outlined in this section is a detailed explanation of what is to be considered the best coding practices within AutoIt.

Variable Naming

The Hungarian notation is adopted however it's simplified and regroup all the types of numbers in one type.

prefix covering type initialization
i Numbers (any type) $i = 0
a Arrays $a = 0 or $a[0]
s Strings (chars included) $s = ""
f Booleans $f = False or $f = True
b Binaries $b = ""
h Handles (and GUI handles) $h = 0
k (not decided) Keywords Null (not decided)
fu (not decided) Functions Null (not decided)
p Pointers $p = 0
tag Structures definition $tag = "" (should directly be filled)
t Structures $t = 0
o Objects $o = 0


The variables are named following this schema :

type (lower case) [optional] subtype (lower case) var name (first letter in upper case)
i f MyFlag

Example :

; Assign a Local variable the number 5.
Local $iSomeVar = 5

; Assign a Local variable the number 1.
Local $ifMyFlag = 1

; Assign a Local variable an array of numbers.
Local $aiArray = 0

; Re-declare the array to size and fill it.
Local $aiArray[3] = [0, 0.25, 3 / 4]


Always initialize a variable on it's declaration, and declare all the same type of variable on the same line.

Example :

;good
Local $iSomeVar1 = 0, $iSomeVar2 = 5
Local $aiArray = 0

;bad
Local $iSomeVar1, $aiArray = 0
Local $iSomeVar2 = 5

You can still categorize your variables and declare for example the $iSomeVar2 on another line.


Scopes of variables

To make the transition, the variables are also named according to their scope.

Global UDF variable Global variable Local variable
$__iSomeVar $_iSomeVar $iSomeVar

Remark: With this method, you will avoid non wanted re-assignments.


Example :

#include <Constants.au3>

; Assign a Global variable the number 0.
Global $iSomeVar1 = 0
; Assign a Global variable the number 5.
Global $_iSomeVar2 = 5

_SomeFunc()

Func _SomeFunc()
	; Assign Local variables respectively the numbers 3 and 4.
	Local $iSomeVar1 = 3, $iSomeVar2 = 4

	; Note: The user inadvertently re-assigned the global variable $iSomeVar1, because this one is not named as "global".

	; Display the value of $iSomeVar1.
	MsgBox($MB_SYSTEMMODAL, "", "Value of $iSomeVar1: " & $iSomeVar1)

	; Display the value of $iSomeVar2.
	MsgBox($MB_SYSTEMMODAL, "", "Value of $iSomeVar2: " & $iSomeVar2)

	; Display the value of $_iSomeVar2.
	MsgBox($MB_SYSTEMMODAL, "", "Value of $_iSomeVar2: " & $_iSomeVar2)
EndFunc


More coming soon :)