zuladabef Posted November 6, 2019 Share Posted November 6, 2019 I am rewriting a series of Funcs that I have for work and I am looking for some better ideas on how to organize and optimize my code. I have thousands of lines of code and a lot of duplication, minus a few variables. My funcs do a few basic operations, or combination of operations like: Get the coordinates on the screen MouseClick() Wait for something to appear (wait for the screen to load) I have 50+ functions that do something similar, and the only difference is that they have a slight change in the variable. Is there a better way to optimize this? Func _GetCoords_BottomCorner_ByImageSearchArea() Local $sPathOf_Image = @ScriptDir & "\images\" & "BottomRightBorderCorner.bmp" Local $iPersonal_XCoord_Offset = 0 Local $iPersonal_YCoord_Offset = 0 Local $iTop = 0 Local $iLeft = @DesktopWidth / 2 Local $iRight = @DesktopWidth Local $iBottom = @DesktopHeight Local $iTolerance = 10 Local $sCenterPos = True ;==>Where you found the image - Return Position Local $aCoords = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) If $aCoords[0] = 1 Then Return $aCoords ElseIf $aCoords[0] = 0 Then ;~ MsgBox(0, "", "_GetCoordsForScrollDownArrow_AssignmentRosterPage()" & @CRLF & "X: " & $aCoords[1] & @CRLF & "Y: " & $aCoords[2]) EndIf Return $aCoords EndFunc ;==>_GetCoords_BottomCorner_ByImageSearchArea Func _GetCoords_YearMonthCombo_ByImageSearchArea() Local $sPathOf_Image = @ScriptDir & "\images\" & "AttendanceYearMonthCombo.bmp" Local $iPersonal_XCoord_Offset = 0 Local $iPersonal_YCoord_Offset = 0 Local $iTop = 0 Local $iLeft = 0 Local $iRight = @DesktopWidth / 2 Local $iBottom = @DesktopHeight Local $iTolerance = 10 Local $sCenterPos = False ;==>Where you found the image - Return Position Local $aCoords = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) If $aCoords[0] = 1 Then Return $aCoords ElseIf $aCoords[0] = 0 Then MsgBox(0, "", "_GetCoordsForScrollDownArrow_AssignmentRosterPage()" & @CRLF & "X: " & $aCoords[1] & @CRLF & "Y: " & $aCoords[2]) EndIf Return $aCoords EndFunc ;==>_GetCoords_YearMonthCombo_Text_InmateAssignmentPage_ByImageSearchArea Link to comment Share on other sites More sharing options...
seadoggie01 Posted November 6, 2019 Share Posted November 6, 2019 I stop wrapping _GetCoordsByImageSearchArea in other functions... you're not saving yourself that much time. Instead, you could wrap it once and set some default values if you wanted... something like this Func My_GetCoordsByImageSearchArea($sPathOf_Image, $iLeft = 0, $iTop = 0, $iRight = @DesktopWidth, $iBottom = @DesktopHeight, $iTolerance = 10, $sCenterPos = True) Local $ret = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) Return SetError(@error, @extended, $ret) EndFunc This will give you some default values that you don't have to type every time 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 Link to comment Share on other sites More sharing options...
zuladabef Posted November 6, 2019 Author Share Posted November 6, 2019 3 hours ago, seadoggie01 said: I stop wrapping _GetCoordsByImageSearchArea in other functions... you're not saving yourself that much time. Is there a way make one function, so that the "user" can just call the Func, and the Func defines the "properties", instead of having to try and remember the filepath, X, Y, coords and stuff? Does it make sense to do something like Func My_GetCoordsByImageSearchArea($sWhatToDo) Select Case $sWhatToDo = "Look For Pixel Color" $iLeft = 0 $iTop = 0 $iRight = @DesktopWidth $iBottom = @DesktopHeight $iTolerance = 10 $sCenterPos = True Case $sWhatToDo = "Look For Text" $iLeft = 0 $iTop = 0 $iRight = @DesktopWidth $iBottom = @DesktopHeight $iTolerance = 10 $sCenterPos = True EndSelect Local $ret = _GetCoordsByImageSearchArea($sPathOf_Image, $iLeft, $iTop, $iRight, $iBottom, $iTolerance, $sCenterPos) Return SetError(@error, @extended, $ret) EndFunc Link to comment Share on other sites More sharing options...
seadoggie01 Posted November 8, 2019 Share Posted November 8, 2019 7 hours ago, iosman123 said: thanks my issue has been fixed. Are you lost? 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 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