Coding style: Quotes around numeric values of variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Coding style: Quotes around numeric values of variables

22 Feb 2018, 07:43

Is it a good practice to use quotes around numeric values of variables?

Both versions do the same, but which one is better from the point of view of logic, consistency, etc? Do you use quotes here?

Code: Select all

; 1st version
foo := "1"
bar := "2"
if (foo = "1" and bar = "2")
    msgBox, %foo% and %bar%

; 2nd version
foo := 1
bar := 2
if (foo = 1 and bar = 2)
    msgBox, %foo% and %bar%
Last edited by john_c on 22 Feb 2018, 07:58, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Quotes around numeric values of variables

22 Feb 2018, 07:51

Code: Select all

MsgBox % foo := 1+2
MsgBox % foo := "1+2"
Let's guess there's a difference ;)
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Quotes around numeric values of variables

22 Feb 2018, 07:54

BoBo wrote:...
Yes, I understand, but in original examples there were no math operations :)
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Quotes around numeric values of variables

22 Feb 2018, 08:29

john_c wrote:
BoBo wrote:...
Yes, I understand, but in original examples there were no math operations :)
I'd assume that its author assumed doing math operations are the main reason using numbers.

Code: Select all

MsgBox % foo := "one plus two"
MsgBox % foo := 1 + 2
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Quotes around numeric values of variables

22 Feb 2018, 08:55

BoBo wrote:I'd assume that its author assumed doing math operations are the main reason using numbers.
So, you will use quotes here? :

Code: Select all

; Move window across multiple monitors
; http://www.softwaretalk.info/most-useful-autohotkey-scripts.htm

; Move window from the left monitor to the right monitor
!right::
	winGet, mm, minMax, a
	winRestore, a
	winGetPos, x, y,,, a
	winMove, a,, x + a_screenWidth, y
	if (mm = 1) ; We can use quotes around 1 or we can don't use them.
	            ; It will be 1 for maximized windows and 0 for
	            ; non-maximized
		winMaximize, a
return

; Move window from the right monitor to the left monitor
!left::
	winGet, mm, minMax, a
	winRestore, a
	winGetPos, x, y,,, a
	winMove, a,, x - a_screenWidth, y
	if (mm = 1)
		winMaximize, a
return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Coding style: Quotes around numeric values of variables

22 Feb 2018, 21:22

- For your example, mm = 1, I wouldn't use quotes. Although mm = "1", would also work in that particular example.

- Here's an example where quotes matter. If at least one item has double quotes, or string content that doesn't look numeric, a string comparison is done.

Code: Select all

q:: ;text compare numeric/string
MsgBox, % (01 = "1") ;0
MsgBox, % (01 = 1) ;1

vNum := "01"
MsgBox, % (vNum = "1") ;0
MsgBox, % (vNum = 1) ;1

vNum := "01"
MsgBox, % ("" vNum = "1") ;0
MsgBox, % ("" vNum = 1) ;0
return
- There is a difference in how AHK v1/v2 handle strings/numbers. I believe it works like this: numbers are stored as numbers and strings in AHK v1, however, in AHK v2, it's either/or. E.g. if you define var := 0xF in AHK v2, this is immediately converted to var := 15, there is no trace of the original appearance of the number as 0xF:

Code: Select all

;AHK v1
vNum := 1.1
MsgBox, % InStr("1.1", vNum) ;1
MsgBox, % vNum ;1.1

vNum := 0xF
MsgBox, % InStr("0xF", vNum) ;1
MsgBox, % vNum ;0xF

;AHK v2
vNum := 1.1
MsgBox(InStr("1.1", vNum)) ;0
MsgBox(vNum) ;1.1000000000000001

vNum := 0xF
MsgBox(InStr("0xF", vNum)) ;0
MsgBox(vNum) ;15
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Coding style: Quotes around numeric values of variables

23 Feb 2018, 06:35

jeeswg wrote:There is a difference in how AHK v1/v2 handle strings/numbers. I believe it works like this: numbers are stored as numbers and strings in AHK v1, however, in AHK v2, it's either/or. E.g. if you define var := 0xF in AHK v2, this is immediately converted to var := 15, there is no trace of the original appearance of the number as 0xF
I.e. the syntax becomes more strict. I think, it's good.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Exies and 123 guests