Building and Testing Functions the Smart Way

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Trubbleguy
Posts: 18
Joined: 11 Jul 2014, 03:00

Building and Testing Functions the Smart Way

09 Dec 2022, 04:08

There are several ways to create a function while coding your script, some will code it into their current working project and run the whole project multiple times when testing the new function, I know it, because for 4 years I DID IT. Or you could Code it in its own file and save it, then run the main script to test it.
Then one day on the boards i saw a function that was different, it had :

if A_Scriptname=formatseconds.ahk
{
exitapp
}
At the start of it, simply to close it, if it was run directly!
I now fill it with all sorts of code to test the function fully and when done just block it out using /* and */
It is one snippit of code i now use regularly when creating a function or regularly run code i call from my main projects.
I have saved a lot of time using it and think there is a lot of people in this Community that might actually benefit from it so i have attached a function i use a lot, (As an example), for finding out how long a MP3 file is after stripping "Time" from the Tags. But using it directly to test it. (I Commented the Hell out of it because my ADHD was pinging today :))
Spoiler
I havent coded a function in my main code now for years....
User avatar
Delta Pythagorean
Posts: 628
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Building and Testing Functions the Smart Way

09 Dec 2022, 04:54

You can simplify that if statement to if (A_LineFile == A_ScriptFileFullPath). This checks if the current file is the same string as the full, initial file ran. Basically AHK's version of if __name == '__main__':

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
Trubbleguy
Posts: 18
Joined: 11 Jul 2014, 03:00

Re: Building and Testing Functions the Smart Way

09 Dec 2022, 05:31

Totally understood, similar amount of typing, same result, I do try to offer a noob an easier way to read how it works, some code makes sense immediately, albeit, the help file can decode a more intense line that is not immediately understood by a noob. I also see that line can be used in the default new ahk file and never need changing, just rem it to disable. Nice alternative.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: arrondark, Chunjee, gwarble and 71 guests