noellarkin Posted January 16, 2023 Share Posted January 16, 2023 I've been trying to clean up some of the programs I'm writing by converting globals into a single JSON string and passing that to functions. For example, instead of: Global $API_KEY = "sdfsdfsdf" Global $API_ENDPOINT = "https://dsfsdfsdf.com/api" Global $API_SECRETKEY = "sdfsdf" Func _MakeAPIRequest("GET") ---------- EndFunc I'm doing something more like: Local $Session = {"api_key":"dfsdfsdf","api_endpoint":"https://sdfsdfsdf.com","api_secretkey":"sdfsdfsdf"} Func _MakeAPIRequest($Session, "GET") ..... EndFunc And inside the function, I'm using Ward's JSON UDF to extract the value for the keys. Is this the best way to reduce the number of globals I'm using? Are there any other ways? Additionally, many of the community UDFs use a large number of globals. Normally this isn't an issue, but I have had problems with some UDFs in the past that stored things like socket values as Globals (FF.au3) and thus wouldn't be able to work as multiprocesses (CoProc UDF etc) without throwing out errors. How do you deal with community UDFs? Do you edit them before use? That would be bad for maintenance purposes I guess. Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted January 16, 2023 Share Posted January 16, 2023 (edited) Hi @noellarkin, in general this is a good point. Avoiding global Variables is almost always a good idea. The way of setting up a JSON string is not bad. I guess I will give it a try in one of my next projects - thanks. Your second code snippet isn't correct. Outside of functions the variables are in a global scope, in AutoIt, no matter if you try to define it as Local. Maybe it's just a typo, but it has to be: Global $Session = {"api_key":"dfsdfsdf","api_endpoint":"https://sdfsdfsdf.com","api_secretkey":"sdfsdfsdf"} Func _MakeAPIRequest($Session, "GET") ..... EndFunc You could also try to use Enum(s) to avoid many global vars. Have a look on the second example. Besides that I also try to define variables only as global when they are really often in use in different functions. Otherwise I define Local or Local Const (if feasible) within the functions. I personally often start my programm by a processing function (called _Actions()) and within this function which is the program flow wrapper I define the other function calls. So it's often not necessary to have global vars at all 😀 . Maybe I could come up with an conrete example, but no time for this right now. Best regards Sven Edited January 16, 2023 by SOLVE-SMART Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
jugador Posted January 16, 2023 Share Posted January 16, 2023 expandcollapse popup;__ExampleA() __ExampleB() Func __ExampleA() ConsoleWrite('> Example1 ' & @crlf) __MakeAPIRequest1(__All_Global_A()) ConsoleWrite('> Example2 ' & @crlf) __MakeAPIRequest2(__All_Global_A(0), __All_Global_A(1), __All_Global_A(2)) ConsoleWrite('> Example3 ' & @crlf) __MakeAPIRequest2(__All_Global_A('$API_KEY'), __All_Global_A('$API_ENDPOINT'), __All_Global_A('$API_SECRETKEY')) EndFunc Func __ExampleB() ConsoleWrite('> Example1 ' & @crlf) __MakeAPIRequest1(__All_Global_B()) ConsoleWrite('> Example2 ' & @crlf) __MakeAPIRequest2(__All_Global_B(0), __All_Global_B(1), __All_Global_B(2)) ConsoleWrite('> Example3 ' & @crlf) __MakeAPIRequest2(__All_Global_B('$API_KEY'), __All_Global_B('$API_ENDPOINT'), __All_Global_B('$API_SECRETKEY')) EndFunc Func __MakeAPIRequest1($Session, $method = 'Get') ConsoleWrite('$_KEY: ' & $Session[0] & @crlf) ConsoleWrite('$_ENDPOINT: ' & $Session[1] & @crlf) ConsoleWrite('$_SECRETKEY: ' & $Session[2] & @crlf) EndFunc Func __MakeAPIRequest2($_KEY, $_ENDPOINT, $_SECRETKEY, $method = 'Get') ConsoleWrite('$_KEY: ' & $_KEY & @crlf) ConsoleWrite('$_ENDPOINT: ' & $_ENDPOINT & @crlf) ConsoleWrite('$_SECRETKEY: ' & $_SECRETKEY & @crlf) EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __All_Global_A ; ======================================================================================== Func __All_Global_A($foo = -1) Local Enum $API_KEY = 0, $API_ENDPOINT, $API_SECRETKEY Local $ApiSecret[3] $ApiSecret[$API_KEY] = "___KEY___" $ApiSecret[$API_ENDPOINT] = "https://API_ENDPOINT.com/api" $ApiSecret[$API_SECRETKEY] = "A___SECRETKEY" If $foo = -1 Then Return $ApiSecret If IsString($foo) Then Return $ApiSecret[Execute($foo)] If $foo >= UBound($ApiSecret) Then Return SetError(1) Return $ApiSecret[$foo] EndFunc ; #FUNCTION# ============================================================================= ; Name...........: __All_Global_B ; ======================================================================================== Func __All_Global_B($foo = -1) Local $o_SDist = ObjCreate( "Scripting.Dictionary" ) If Not IsObj($o_SDist) Then Return SetError(1) $o_SDist.Add('$API_KEY', '___KEY___') $o_SDist.Add('$API_ENDPOINT', 'https://API_ENDPOINT.com/api') $o_SDist.Add('$API_SECRETKEY', 'A___SECRETKEY') Local $ApiSecret If IsString($foo) Then $ApiSecret = $o_SDist.Item($foo) $o_SDist = 0 Return $ApiSecret EndIf $ApiSecret = $o_SDist.items() $o_SDist = 0 If $foo = -1 Then Return $ApiSecret If $foo >= UBound($ApiSecret) Then Return SetError(2) Return $ApiSecret[$foo] EndFunc SOLVE-SMART 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now