Best way to avoid Global declaration Plague from ruining the main script?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Best way to avoid Global declaration Plague from ruining the main script?

11 Nov 2018, 15:48

I'm currently using something similar from the code below in order to avoid the main script to be ruined by functions that do heavy use of Global declarations!

If anybody knows better alternatives, like forcing the main script to static mode, please share here! Thanks!

Code: Select all

MainScript()
MainScript()
{

Static

gui, add, text, vTxtCtrl, Main Script
gui, add, edit, vEdtCtrl,
gui, show, w200 h200
return

guiclose:	;______________ gui close ______________
exitapp

}
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Best way to avoid Global declaration Plague from ruining the main script?

12 Nov 2018, 03:42

Let me start off by saying,i'm one of those lunatics that thinks there are valid reasons to use GoTo so long as it's not abused,such as performance(using goto in 'C' is way faster and the only way in some instances to avoid a stackoverflow & even in ahk it's ~35% faster than a function call ). And i'm sorry i'm not offering a constructive solution here, but jumping through very complicated hoops to avoid using Global Variables on PRINCIPLE Alone,is a waste of time in my opinion,not that i expect you to place any stock in it.

FYI, i read http://wiki.c2.com/?GlobalVariablesAreBad , and i agree with most points,just not a pathological aversion to them...especially not in a scripting language whose greatest value is saving us time.


Sorry for not being constructive here,but just had it to put it out there...It's my trigger :crazy: :wtf: ;)
live ? long & prosper : regards
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Best way to avoid Global declaration Plague from ruining the main script?

12 Nov 2018, 09:51

CyL0N wrote:
12 Nov 2018, 03:42
especially not in a scripting language whose greatest value is saving us time.
Yha, like writing a thousand of lines script and just to find out in the end that the script is so buggy that can't even be fixed!

I believe that Global declaration is the culprit of most bugs\errors generated by ahk scripts!

I already saw a lot of people begging for help because their thousand of lines scripts are just unusable because of unknown\undetectable bugs\errors, and believe me, no one can find a workaround to fix them!

The above never happened to me because I am very careful when I write my scripts, the problem is that being too careful requires time, patience, it's tedious and makes all the process of writing scripts too slow!

The most essential thing here to note is that, Global variables cause too many problems, yes, too many problems! (Not using them is the best way to go!)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Best way to avoid Global declaration Plague from ruining the main script?

12 Nov 2018, 10:15

- The one problem you can have is that if you declare a variable inside a function as local, that makes all other variables global. What I do is add 'local' to every function that I write, and then specify 'global' inside a function, for any global variables.
- Any variable that I intend to be global, I add a prefix to, 'vGbl'. Other possibilities could be 'AX_' for something that I think could be a good 'A_' variable, or 'g_'.
- I haven't had any problems regarding global variables.
- My one worry is hotkey subroutines interrupting each other, and then one subroutine setting the global variables of another. However, using functions and unique variable names avoids any problems.
- Also, Critical and Thread can be useful. Ideally a subroutine which could not be interrupted, even when MsgBox is open, and where hotkeys were optionally cached or not cached, would be useful. I.e. a mix of the Critical and Thread functionality.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

Re: Best way to avoid Global declaration Plague from ruining the main script?

12 Nov 2018, 10:37

Fair Enough But...
User wrote:
12 Nov 2018, 09:51
...,the problem is that being too careful requires time, patience, it's tedious and makes all the process of writing scripts too slow!
I completely agree,variable management is paramount for very large scripts,but 90% of what i write are 500<lines & most are functions anyway,so that takes care of that,probably holds true for most users that go beyond hotkeys...


And to respond to your original query, for me i write most of my 'scripts' as functions,for quick integration & use within other scripts,always using ByRef for functions that expect large inputs,which is probably why i've never had Global Var issues,i actually rarely use them except as custom A_* vars because i implicitly take a functional approach to scripting.And i also compulsively have functions return associative arrays to remove the need for additional variables to work with returned data(i.e MyFunc().returnedValue1,MyFunc().returnedValue2) without additional variables Global or Otherwise.


Cheers :salute:
live ? long & prosper : regards
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Best way to avoid Global declaration Plague from ruining the main script?

12 Nov 2018, 11:04

If you use statis like this you should probably use a class instead.
Also you can - no you should use globals for specific data.
That is the data of your core program that combines all the modules.
Recommends AHK Studio
John1
Posts: 236
Joined: 11 May 2020, 11:54

Re: Best way to avoid Global declaration Plague from ruining the main script?

17 Sep 2022, 12:03

When using for every single variable a good unique name than i am save with global variables? or is there more how global variables can ruin a huge script?
User avatar
Chunjee
Posts: 1589
Joined: 18 Apr 2014, 19:05
Contact:

Re: Best way to avoid Global declaration Plague from ruining the main script?

18 Sep 2022, 10:17

CyL0N wrote:
12 Nov 2018, 03:42
even in ahk it's ~35% faster than a function call
CyL0N wrote:
12 Nov 2018, 03:42
in a scripting language whose greatest value is saving us time.
You're saving the computer time. Goto costs the reader extra time.


GOTO makes a program hard to read. The main reasons functions are used instead of GOTOs is that they are easier to modify, debug, and understand without going into details of how they are implemented.
It leads to "spaghetti code", where the flow of the program isn't obvious and is very hard to follow. It also breaks encapsulation, with data that is read and modified in many different places.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 129 guests