IanN1990 Posted February 12, 2013 Share Posted February 12, 2013 (edited) This is just an off question as i am not sure. Here is a random function i wrote. #NoTrayIcon ConsoleWrite(Timer()) Func Timer() $Temp = TimerInit() While Not WinExists('Notepad') If TimerDiff($Temp) > 5000 Then Return 1 sleep(250) WEnd Return 0 EndFunc Is it bad returning a function, while being inside a Loop or should i exit the loop first before returning? #NoTrayIcon ConsoleWrite(Timer()) Func Timer() $Temp = TimerInit() While Not WinExists('Notepad') If TimerDiff($Temp) > 5000 Then ExitLoop sleep(250) WEnd If TimerDiff($Temp) > 5000 Then Return 1 If TimerDiff($Temp) < 5000 Then Return 0 EndFunc Though sense functions return 0 by default, it could be this. #NoTrayIcon ConsoleWrite(Timer()) Func Timer() $Temp = TimerInit() While Not WinExists('Notepad') If TimerDiff($Temp) > 5000 Then ExitLoop sleep(250) WEnd If TimerDiff($Temp) > 5000 Then 1 EndFunc Edited February 12, 2013 by IanN1990 Link to comment Share on other sites More sharing options...
guinness Posted February 12, 2013 Share Posted February 12, 2013 This is correct in my humble opinion. #NoTrayIcon ConsoleWrite(Timer() & @CRLF) ; Could use WinWait of course! Func Timer() Local Const $hTimer = TimerInit() ; Create a timer variable. Local $fNotepadExists = False ; Create a boolean variable. While Not $fNotepadExists ; While not Notepad window exists. $fNotepadExists = WinExists('[CLASS:Notepad]') = 1 If TimerDiff($hTimer) > 5000 Then ExitLoop EndIf Sleep(10) WEnd Return $fNotepadExists EndFunc ;==>Timer UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
IanN1990 Posted February 12, 2013 Author Share Posted February 12, 2013 (edited) I was aware of WinWait and i do normally use built in functions when ever possible but there is no timer for what i am writting. So i made an example to demostrate if my style was wrong or harmful. Looking at your example 3 questions come to mind. Why declare the TimerInIt() as a local const ? Its only used for the function and is thrown away once the function ends. Much like $i = 0 to 10 Next Where you wouldn't locally declare the $i? (The only reason i could think of is incase the function is called again before it ends, though this could cause a recursion which i try and avoid like the plague) Is there anything wrong with using a check statement, for a While Loop. The way i saw it, if notepad doesn't exist it would equal = 1. So the while loop carrys on until the timer is reached or it does exist "makin it 0" and the while loop ends. Thirdly, does this mean i should have a exitloop ? or is returning while inside a loop considered bad or dangerous? Edited February 12, 2013 by IanN1990 Link to comment Share on other sites More sharing options...
guinness Posted February 12, 2013 Share Posted February 12, 2013 (edited) Most of the questions can be answered by navigating to the link in my signature called "Coding" but I will answer them here for you. Why declare the TimerInIt() as a local ? Its only used for the function and is thrown away once the function ends. Much like $i = 0 to 10 Next You wouldn't locally declare the $i?Because $i is in 'Loop' scope so it's automatically Local. Explicitly using Local or Global ensures that you have control over variable declaration. If you had a variable called $hTimer in Global scope and we didn't use the Local keyword, then this Global variable would be overwritten causing massive issues if that variable contained important data. By default if you don't use Local in a function and the variable doesn't exist in Global, then it's automatically Local. Oh and don't use Dim, first post of that link explains why. Look at "single entry point, single exit point" normally it's better to return from a function quickly if there is an error, but in your example returning whilst in the loop is considered bad practice and poses as many unnecessary exit points. Of course as I said sometimes you need to, it's just a question of using your common sense of what is easier to understand and doesn't conform to SESE vs conforms with SESE but is difficult to read. Here look at this as spot the difference. #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example - Bad") Local $iOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $iOK Exit ; Bad as the GUI controls are never deleted. Even though AutoIt takes care of this, we shouldn't be too reliable on AutoIt as well as lazy. EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Example() Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example - Good") Local $iOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $iOK ExitLoop ; Gpod as the GUI controls can be tidied up. EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example You could use: but GUIDelete at least takes care of this for the controls. ; Delete the previous GUI and all controls. GUICtrlDelete($iOK) GUIDelete($hGUI) Edited February 12, 2013 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
IanN1990 Posted February 13, 2013 Author Share Posted February 13, 2013 I was also aware of your topic for good coding pratices, which is what sparked this topic in a way as i wasn't sure how to take it.Because $i is in 'Loop' scope so it's automatically Local. Explicitly using Local or Global ensures that you have control over variable declaration. If you had a variable called $hTimer in Global scope and we didn't use the Local keyword, then this Global variable would be overwritten causing massive issues if that variable contained important data. By default if you don't use Local in a function and the variable doesn't exist in Global, then it's automatically Local. Oh and don't use Dim, first post of that link explains why.I found that part really useful, i had no idea that could happen and though it doesn't affect any of my code it is sure something i will be careful about in the furture.And i would agree Autoit is far to forgiving when it comes to cleaning up resources. I do try to make it a pratice as well as cleaning up as i go. Link to comment Share on other sites More sharing options...
guinness Posted February 13, 2013 Share Posted February 13, 2013 I was also aware of your topic for good coding pratices, which is what sparked this topic in a way as i wasn't sure how to take it.Great, I wish more people would start topics of this nature. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Mat Posted February 13, 2013 Share Posted February 13, 2013 You should always assume a function can be called from anywhere, and it should not affect the program. In an ideal world, you should be able to run this code in any of the examples: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Local $gui = GUICreate("Run the example?", 400, 300) Local $btn = GUICtrlCreateButton("Run it", 20, 20, 80, 30) GUISetState() Local $msg While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $btn Example() EndSwitch WEnd Func Example() ; Create a GUI with various controls. Local $hGUI = GUICreate("Example - Good") Local $iOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $iOK ExitLoop ; Gpod as the GUI controls can be tidied up. EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example It's not quite perfect, as there are a few extra considerations when designing multi-window interfaces that really would make the examples too complicated to be of any use (open the example window, then click the close button on the first window for example). AutoIt Project Listing 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