seadoggie01 Posted February 7, 2020 Posted February 7, 2020 I have a general programming question: Where is it best to place variable declarations with regards to control structures? Sometimes I'll only need a variable inside of an IF statement and I declare it inside, like this... Func DistListItemGet($vDistList) If Not IsObj($vDistList) Then ; Find it Local $aItems = _OL_ItemFind(...) If @error Then Return SetError(1, @error, False) $vDistList = $aItems[1][1] ; Get the object $vDistList = _OL_ItemGet(...) EndIf ; Do stuff with the object here EndFunc I only use $aItems (the result of _OL_ItemFind) inside the If statement, and I won't need it later... is it acceptable programming practice to declare the variable on the inside in this case, or should all variables be declared outside of structures? I realize that declaring variables inside of loops is bad practice, but what about inside of conditionals? All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types
Nine Posted February 7, 2020 Posted February 7, 2020 For the moment you may not need the variable outside the if. But in the future it might change. And you will be face with an undeclared variable elsewhere if the condition is not True. Making debug a bit more tedious in large programs. I personally prefer playing it safe as it has no impact on size and speed of the script. So I always declare my vars outside a block of statement. Davidowicza 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
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