Page 1 of 1

Building and Testing Functions the Smart Way

Posted: 09 Dec 2022, 04:08
by Trubbleguy
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....

Re: Building and Testing Functions the Smart Way

Posted: 09 Dec 2022, 04:54
by Delta Pythagorean
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__':

Re: Building and Testing Functions the Smart Way

Posted: 09 Dec 2022, 05:31
by Trubbleguy
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.