Page 1 of 2

what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catch?

Posted: 24 Feb 2017, 13:07
by 20170201225639
a couple of my own ...


#1: putthing a whitespace between function name and left parenthesis

example:

incorrect (function will NOT be called)

Code: Select all

___a_toggle_window1_Xpos_between_2_and_3 ( _title, 0, -100 )
correct:

Code: Select all

___a_toggle_window1_Xpos_between_2_and_3( _title, 0, -100 )

i've made this mistake probably 4-5 times in the past month, and each time it took me some time to realize this is the root of the problem. i'm thinking i should write an ahk script to scan my scripts of " (" before i run them ...




#2: assuming pixelgetcolor outputs RGB (as opposed to BGR) color

example:


incorrect (you'll get a different color)

Code: Select all

PixelGetColor, _color, %MouseX%, %MouseY%
...
Gui, Color, %_color%

correct

Code: Select all

PixelGetColor, _color, %MouseX%, %MouseY%, RGB
...
Gui, Color, %_color%




#3 single line block comment

incorrect (hotkey will simply not take effect (i wonder what really happens in this case?))

Code: Select all

tab::
/* haha */
msgbox haha
return

correct

Code: Select all

tab::
/* 
haha
*/
msgbox haha
return

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 24 Feb 2017, 21:54
by boiler
Typos in variable names.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 26 Feb 2017, 16:56
by iseahound
Declaring variables mostly.


1a) Correct

Code: Select all

x1 := y1 := x2 := y2 := 0
1b) incorrect - only x1 is static, rest are instance variables.

Code: Select all

static x1 := y1 := x2 := y2 := 0
I was mistaken about this one.
3a) incorrect - the variable angelic must be global in AHK

Code: Select all

static angelic := "Graphics_Subtitle_simTextSize"         
Gui, TextSizeWindow:Add, Text, % "v" angelic, % str
3b) Correct Workaround - turn a static variable into a global one on the fly.

Code: Select all

static angelic := "Graphics_Subtitle_simTextSize"         
DeclareGlobal(angelic)
Gui, TextSizeWindow:Add, Text, % "v" angelic, % str


DeclareGlobal(angelic) {
   global
   (%angelic%)
   return  ; angelic is omnipresent. Deref angelic and make her a global var.
}

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 26 Feb 2017, 17:00
by nnnik
iseahound wrote:2a) Incorrect - only r is static

Code: Select all

static r, g, b

Code: Select all

fn()
{
	static x := 1, y := x++
	return y
}
Msgbox % fn()
Msgbox % fn()
Msgbox % fn()
Try to explain this code please

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 26 Feb 2017, 17:09
by iseahound
nnnik wrote:Try to explain this code please
I was mistaken. Seems like it gave me trouble in the past and I just avoided that afterwards. Post will be edited.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 26 Feb 2017, 19:17
by TAC109
boiler wrote:Typos in variable names.
#warn can help find these.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 26 Feb 2017, 19:54
by Guest

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 03 Apr 2017, 03:02
by lexikos
iseahound wrote:1b) incorrect - only x1 is static, rest are instance variables.
No, they are either local or global variables. I think you probably know this and are just misusing the term "instance".

They could be static if the function is assume-static.
3a) incorrect - the variable angelic must be global in AHK

Code: Select all

static angelic := "Graphics_Subtitle_simTextSize"         
Gui, TextSizeWindow:Add, Text, % "v" angelic, % str
No. You aren't passing the variable angelic to the Gui command. You are passing its contents. The variable angelic can be static, local or global. The variable Graphics_Subtitle_simTextSize can be static if you call the function normally, but it must be global if Gui command is within a subroutine-in-a-function called by a Gui event, timer or similar.
3b) Correct Workaround - turn a static variable into a global one on the fly.
No. It does not "turn a static variable into" anything. This creates a global variable based on the name passed to the function. It makes no difference where the name comes from, whether it is from a static variable, global variable, or not a variable at all.

Unless you sometimes change the value of angelic, there is simply no reason for that variable. You can do this instead:

Code: Select all

global Graphics_Subtitle_simTextSize
Gui, TextSizeWindow:Add, Text, vGraphics_Subtitle_simTextSize, % str

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 24 Apr 2017, 20:23
by Randy31416
Number one error for me, time after time, and very hard to find (time after time): writing a plain equals sign (not a colon-equals) for assignment. Too many other languages in my mental baggage use the equals for assignment and the habit (maybe even muscle-memory habit) is very hard to break.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 26 Apr 2017, 08:55
by 20170201225639
i do that too, although i've come to like the fact that ahk doesn't use equality for assignment.

my biggest gotcha with '=' (or perhaps really it's with the 'if' construct?) is that for a long time i wasn't accustomed to the fact that, in the code below, only the 1st 2nd and 4rd msgbox will appear. the 3rd won't. my paranoia about the 3rd kind of case resulted in a lot of unnecessary parentheses in my code :oops:

Code: Select all

bar := 2017
	if bar=2017
		msgbox yes 1!!!
	if (bar=2017)
		msgbox yes 2!!!

foo := "haha"
	if foo="haha"
		msgbox yes 3!!!
	if (foo="haha")
		msgbox yes 4!!!

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 18 May 2017, 08:43
by Exaskryz
20170201225639 wrote: #3 single line block comment

incorrect (hotkey will simply not take effect (i wonder what really happens in this case?))

Code: Select all

tab::
/* haha */
msgbox haha
return

correct

Code: Select all

tab::
/* 
haha
*/
msgbox haha
return
It's been a few months, and I'm not sure if you've gotten an answer to your question. But the */ has to be on its own line to be recognized as a closing comment. In the incorrect code, all of this is commented out:
tab::
/* haha */
msgbox haha
return
Which may be hard to notice because the syntax highlighter on the forums and probably in your text editor don't look for a newline */ as the closer.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 04 Jul 2017, 15:29
by SpeedMaster
GUI Text and DPI Scaling in Windows - Larger Text (120 DPI) :twisted:

explanation here:
https://autohotkey.com/board/topic/9030 ... t-120-dpi/

solution here:
https://autohotkey.com/board/topic/6893 ... entry77893

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 04 Jul 2017, 15:43
by nnnik
boiler wrote:Typos in variable names.
+Renaming variables and missing out on a few occurencies.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 04 Jul 2017, 16:09
by SpeedMaster
Object.clone() doesn't create a copy, keeps references :twisted:

explanation here:
https://autohotkey.com/board/topic/1034 ... antiation/
https://autohotkey.com/board/topic/6954 ... eferences/

solution:
use this function to create a deep copy (tnx fincs) ;)

Code: Select all

ObjFullyClone(obj)
{
	nobj := obj.Clone()
	for k,v in nobj
		if IsObject(v)
			nobj[k] := A_ThisFunc.(v)
	return nobj
}

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 10 Apr 2018, 16:45
by SpeedMaster
Just a small amount of recursive function call can lead to a stack overflow crash in AHK. :twisted:

explanation here:
https://autohotkey.com/board/topic/4372 ... ashes-ahk/

example:

Code: Select all

MsgBox % rec(0, 1000)
return
rec(deep, end)
{
	Tooltip %deep%
	if (deep = end)
		return "done"
	else
		return rec(deep + 1, end)
}

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 11 Apr 2018, 03:06
by nnnik
Well if you need 1000 layers of recursion you are doing it wrong.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 11 Apr 2018, 06:26
by Helgef
Surprisingly, I never actually made this (similar) mistake until quite recently, had I still been using v1, it would have failed silently (since I didn't use try-catch-throw for every dllcall) and it might have been quite difficult to find. Example,

Code: Select all

dllcall("MEssageBox", "ptr", 0, "str", "hello", "str", "world", "uint", 0x40) ; v1 fails silently, v2 gives the appropriate error message
Cheers.

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 11 Apr 2018, 08:13
by guest3456
SpeedMaster wrote:GUI Text and DPI Scaling in Windows - Larger Text (120 DPI) :twisted:

explanation here:
https://autohotkey.com/board/topic/9030 ... t-120-dpi/

solution here:
https://autohotkey.com/board/topic/6893 ... entry77893
looks like i was quoted years ago in those old posts

but newer versions of AHK (1.1.11+) don't need those hacks anymore:

https://autohotkey.com/docs/commands/Gui.htm#DPIScale

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 11 Apr 2018, 09:30
by tank
I had a lot of trouble with unquoted strings

Code: Select all

if var = this is a string
    ;; execute
not at all the same as

Code: Select all

if (var = this is not a string)
    ;; execute
or

Code: Select all

if var = this is a string && var2 = not a string anymore
    ;; execute
Drove me absolutely bonkers

Re: what are some ahk programming mistakes that tend to 'fail silently' with no warning and took you a long time to catc

Posted: 11 Apr 2018, 10:13
by SpeedMaster
SpeedMaster wrote:Just a small amount of recursive function call can lead to a stack overflow crash in AHK. :twisted:
nnnik wrote:Well if you need 1000 layers of recursion you are doing it wrong.
Well i am not familiar with other programming languages but I am wondering if it is possible to reach the base case of 1000 in Java or c# (reproducing similar script) :think:

Here is a similar code in Python 2.7.14 (the script crashes after 975 recursions)

Code: Select all

def countup(n):
    print(n)

    if n == 1000:
        return

    countup(n + 1)
    
countup(1)