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

Helpful script writing tricks and HowTo's
20170201225639
Posts: 23
Joined: 01 Feb 2017, 22:57

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

24 Feb 2017, 13:07

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] [Download] GeSHi © Codebox Plus

___a_toggle_window1_Xpos_between_2_and_3 ( _title, 0, -100 )

correct:

Code: [Select all] [Download] GeSHi © Codebox Plus

___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] [Download] GeSHi © Codebox Plus

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



correct

Code: [Select all] [Download] GeSHi © Codebox Plus

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?))



correct
iseahound
Posts: 57
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

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

26 Feb 2017, 16:56

Declaring variables mostly.


1a) Correct

Code: [Select all] [Download] GeSHi © Codebox Plus

x1 := y1 := x2 := y2 := 0


1b) incorrect - only x1 is static, rest are instance variables.

Code: [Select all] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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.
}
Last edited by iseahound on 26 Feb 2017, 17:11, edited 2 times in total.
User avatar
nnnik
Posts: 1974
Joined: 30 Sep 2013, 01:01
Location: Germany

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

26 Feb 2017, 17:00

iseahound wrote:2a) Incorrect - only r is static

Code: [Select all] [Download] GeSHi © Codebox Plus

static r, g, b

Code: [Select all] [Download] GeSHi © Codebox Plus

fn()
{
static x := 1, y := x++
return y
}
Msgbox % fn()
Msgbox % fn()
Msgbox % fn()
Try to explain this code please
Recommends AHK Studio
iseahound
Posts: 57
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

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

26 Feb 2017, 17:09

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.
TAC109
Posts: 169
Joined: 02 Oct 2013, 19:41

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

26 Feb 2017, 19:17

boiler wrote:Typos in variable names.
#warn can help find these.
lexikos
Posts: 5320
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

03 Apr 2017, 03:02

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

global Graphics_Subtitle_simTextSize
Gui, TextSizeWindow:Add, Text, vGraphics_Subtitle_simTextSize, % str
Randy31416
Posts: 42
Joined: 15 Jan 2014, 19:09

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

24 Apr 2017, 20:23

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.
20170201225639
Posts: 23
Joined: 01 Feb 2017, 22:57

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

26 Apr 2017, 08:55

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] [Expand] [Download] GeSHi © Codebox Plus

User avatar
Exaskryz
Posts: 1950
Joined: 17 Oct 2015, 20:28

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

18 May 2017, 08:43

20170201225639 wrote:#3 single line block comment

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



correct


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.

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 6 guests