pixelsearch Posted May 18, 2023 Share Posted May 18, 2023 Hi everybody I got a theorical question concerning global variables. In the 1st version of my script, all global variables are defined in function Main() then these global variables can easily be used anywhere in the other functions, without the need of passing them as parameters. These 6 global variables are 2 Arrays + 2 Dictionaries objects + 2 integers : Opt("MustDeclareVars", 1) Global $g_aUser, $g_aUserBackup, $g_oUser, $g_oData, $g_iLinesUser, $g_idListView Main() Func Main() $g_aUser = ... $g_aUserBackup = ... $g_oUser = ... $g_oData = ... $g_iLinesUser = ... $g_idListView = ... ... EndFunc Func GetUserIndex($ID) ... EndFunc Func DayChange($sDate, $bLastDay = False) ... EndFunc Func Fill_oUser() ... EndFunc In the 2nd version of the script, I can reduce the number of global variables, by making the 2 Arrays and 2 Dictionaries objects become Local, then passing the needed ones as ByRef parameters to the other functions : Opt("MustDeclareVars", 1) Global $g_iLinesUser, $g_idListView Main() Func Main() Local $aUser, $aUserBackup, $oUser, $oData $aUser = ... $aUserBackup = ... $oUser = ... $oData = ... $g_iLinesUser = ... $g_idListView = ... ... EndFunc Func GetUserIndex($ID, ByRef $aUser, ByRef $oUser) ... EndFunc Func DayChange($sDate, ByRef $aUser, ByRef $aUserBackup, ByRef $oUser, ByRef $oData, $bLastDay = False) ... EndFunc Func Fill_oUser(ByRef $aUser, ByRef $oUser) ... EndFunc The output of the 2 scripts is exactly the same (and the processing time too +++) Having in mind that the functions will be called several hundred times, which scripting way should be preferred and why ? Thanks Edit: Mods, I created this thread in the "AutoIt Technical Discussion" part of the Forum, let's hope it's correct. Link to comment Share on other sites More sharing options...
water Posted May 18, 2023 Share Posted May 18, 2023 I would consider two aspects: readability and good coding style. A script is easy to read and understand when everything a function needs is passed as parameter. Good coding style means: Every function has one point to enter (the function call) and one point to exit (no more than one Return statement). Hence all data being read/written by the function should be passed as parameter. So I suggest to use version 2. Sometimes you might think that global variables can't be avoided. In this case please have a look at the Static keyword. pixelsearch 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
pixelsearch Posted May 18, 2023 Author Share Posted May 18, 2023 @water thanks for the advices, they were helpful So I deleted version 1 and kept version 2, the full script can be found at this link I even modified it again, to get less parameters passed to function DayChange() . It was possible by moving some code from function DayChange() to function Main() imho, a line like this one... Opt("MustDeclareVars", 1) ...is really important (I learned that from @Nine who uses it very often, if not always) Also having your few global variables starting ALL with $g_ makes the script more readable : when you review your own code, it's much easier to modify it if all your global variables start with $g_ especially when you review code placed in functions. My everlasting problem is that I nearly never start a new script with code placed in a Main() function, which means all variables are global as they're not part of a function. Later, when the script begins to be a bit "harder" to review, then I modify it, create functions that should have been created in the 1st place (including a Main function), move code inside functions, modify variables names (e.g. add $g_ to the "now-global" variables placed in functions, when they're not parameters etc...) This is time-consuming, especially I comment lines a lot ! Sure it would be better to have a full "global" view from the very beginning, before starting to code the 1st line. I'll try to remember all this... before writing a new script, fingers crossed Btw, in your answer, you mentioned the Static keyword. I'm used to it, as it's part of my one and only AutoIt abbrevation : b=Local Static $iCounter = 0\n$iCounter += 1\nConsoleWrite("$iCounter = " & $iCounter & @crlf)\n Typing b then Ctrl+b displays the following in Scite Window, very useful ! Local Static $iCounter = 0 $iCounter += 1 ConsoleWrite("$iCounter = " & $iCounter & @crlf) Link to comment Share on other sites More sharing options...
water Posted May 19, 2023 Share Posted May 19, 2023 15 hours ago, pixelsearch said: Main() function, I never use a Main function - never had any problems with this approach. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki 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