Different ways of doing type checks and contracts

2 posts in this topic

#1 ·  Posted (edited)

Edit: Just realised this was posted in the wrong forum! I guess the Mods will move it to either "AutoIt General Help and Support" or "AutoIt Technical Discussion".

Do you use type checking? Or do you choose not to type check?

I was trying to think of the simplest way to do a type check without typing arguments more than once and I came up with:

Func displayPerson($firstName, $lastName, $age)
  ; -- TYPE CHECK --
  Local $typeCheck = ("" _
    & IsString($firstName) _
    & IsString($lastName) _
    & IsNumber($age) _
  If (StringInStr($typeCheck, "0")) Then MsgBox(16, "Type Error: displayPerson()", $typeCheck)

  ; -- FUNCTION --
  MsgBox(0, "", $firstName & " " & $lastName & " (" & $age & ")")

The only catch with this method is that it produces a very simplistic error message. Even still, the fact that you only have to type out arguments once makes it a reasonable approach, in my opinion. The same logic can also be used for making function contracts (for example: $firstName mustn't be an empty string etc...).

What do you think? How do you go about such things?

Edited by Melba23

Share this post

Link to post
Share on other sites

#2 ·  Posted (edited)

My preference is to let the automatic string <--> number conversions act as much as possible, of course unless the expected datatype is mandatory (array having this-that dimension, object, map, dllstruct, ...)

If a given parameter is expected and used as a number in a function, passing a string containing a number is harmless and can save cycles. Vice-versa: number --> string is pretty common and also harmless.

Type checking is one thing, but domain checking is another. I think the burden of supplying correct arguments in both type and value must be supported by the caller, not the callee, just because it's at this point you can do something: at the callee stage, you just can error out and emergency stop.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Karnalsyn
      By Karnalsyn
      As with a lot of programming, the less manual repetition done to achieve the same outcome...the better.
      I package programs for network deployment at the organization I work for. Sometimes these applications require verbose messaging to the user, and sometimes by special request we have to turn that off and deploy silently.
      The way I currently handle these 2 scenarios is this.
      I have a function that builds the splashwin display screen to variable size depending on message length, and displays the desired message to the end user.
      Then at each point throughout my script as I need to make the user aware of what is currently happening, I inject a splashwin function call with the unique message pertaining to that specific event. ie. "Installing Microsoft Office, please wait..."
      In effort to build a more universal script to handle any type of request submitted. I've incorporated a switch command of '/silent'
      If cmdlineraw detects the usage of that switch when the script is triggered it sets a variable flag, ie. $silent = "on"
      Then at every splashwin call I make, I'm prefixing it with an IF statement that checks for the flag, and then does NOT display the message if that flag is "on". Or does display it if its not. Has worked for me just fine. But while I grow my programming skills and look for more streamlined ways of handling areas I find myself creating repetition. I'm curious if I can relocate that flag check. Taking it out of every single splashwin call, and injecting it into the function itself.
      Example of how I currently display or hide the splashwin based on command line...
      If StringInStr($cmdlineraw, "/silent", 0) Then
          $SilentSwitch = "ON"
      If $SilentSwitch = "OFF" Then SplashWin($Uninstalling & $ProductInfo[1][0])
      Some scripts have dozens of those splashwin calls littered throughout, so Is this the best approach I can take already? Or as stated earlier, can I pull the IF statement away from the splashwin call. And left the function as a whole either activate or deactivate as a result of the command line check.
    • IamKJ
      By IamKJ
      I am trying to allow the GUI to gather info as to when to execute a function.  I am having trouble doing this.  So far this is what I have.
      ;Timer Func timer () If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer $iMsgBoxAnswer = MsgBox(36,"Timer","Please format your answer in 00:00:00:000") Select Case $iMsgBoxAnswer = 6 ;Yes Global $infotime = InputBox ('Time', 'What time to execute?') Do $rawtimer = ToolTip(@Hour & ':' & @Min & ':' & @Sec & ':' & _MSec()) until $rawtimer = $infotime if $rawtimer = $infotime Then msgbox (0,'Worked','Worked') Else EndIf Case $iMsgBoxAnswer = 7 ;No Exit EndSelect EndFunc Func _MSec() Local $stSystemTime = DllStructCreate('ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort') DllCall('kernel32.dll', 'none', 'GetSystemTime', 'ptr', DllStructGetPtr($stSystemTime)) $sMilliSeconds = StringFormat('%03d', DllStructGetData($stSystemTime, 8)) $stSystemTime = 0 Return $sMilliSeconds EndFunc I have also tried _GUIToolTip_GetText in order to read the tooltip until the time specified, but it still doesn't work.  Any help would be great.
    • XOblivion
      By XOblivion
      im some what new to autoit and need help figuring out best way to make a simple clicker for few idle games i play(taptitdue, sakura clicker, elndless frontier etc.) ive played around with autoit recorder to make simple copy mouse clicks. but now i want to make a script that allows me to select multiple functions before starting the script for game ex: click section A or click section A + B to run At set intervals if that makes sense . i dont need scripts made by other just info on what things i should use to make it my self.
      need to be able to select between games and be able to select multiple functions to run inconjuntion or independent for each game. thank you in advance
    • BetaLeaf
      By BetaLeaf
      Hi guys. I am trying to make a soundboard app. 
      Here is the code I have so far
      #include "Misc.au3" #RequireAdmin;needed to work in some games. #include "array.au3" Opt("WinTitleMatchMode", -2) If @OSArch = "x64" Then Global $VLC_Path = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" Global $VLC_WorkingDir = "C:\Program Files (x86)\VideoLAN\VLC\" Else Global $VLC_Path = "C:\Program Files\VideoLAN\VLC\vlc.exe" Global $VLC_WorkingDir = "C:\Program Files\VideoLAN\VLC\" EndIf Global $sectionData = IniReadSectionNames(@ScriptDir & "\SoundBoard.ini") If @error Then IniWriteSection(@ScriptDir & "\SoundBoard.ini", "Sound1", 'File="' & @UserProfileDir & '\Music\SampleTrack.mp3"' & @CRLF & 'StartTime="12"' & @CRLF & 'EndTime="34"' & @CRLF & 'PlaybackDevice="Microsoft Soundmapper"' & @CRLF & 'Hotkey="+{numpad9}"') MsgBox(16, "SoundBoard", "SoundBoard.ini is missing. It has been created for you.") ShellExecute(@ScriptDir & "\SoundBoard.ini", "", "", "edit") InputBox("SoundBoard", "Notes:" & @CRLF & "StartTime and EndTime are in seconds. Available Hotkeys can be found at the following url:", "") Exit EndIf For $i = 1 To $sectionData[0] Local $iArray = IniReadSection(@ScriptDir & "\SoundBoard.ini", $sectionData[$i]) For $j = 1 To UBound($iArray) - 1 Local $result = Assign("SoundBoard" & $i & "_" & $j, IniRead(@ScriptDir & "\SoundBoard.ini", $sectionData[$i], $iArray[$j][0], $iArray[$j][1]), 2) If $result = 1 Then Consolewrite("Variable Assigned: SoundBoard" & $i & "_" & $j & @CRLF & "Data=" & $iArray[$j][1]&@CRLF) Else Consolewrite("Variable was not assigned: SoundBoard" & $i & "_" & $j & @CRLF & "Data=" & $iArray[$j][1]&@CRLF) EndIf Next Next For $i = 1 To $sectionData[0] Local $Hotkey = Eval("SoundBoard"&$i&"_5") ConsoleWrite("Processing Hotkey "&$Hotkey&@CRLF) ;NEED HELP HERE ; HotKeySet($Hotkey,"") Next While 1 Sleep(500);idle to prevent unnecessary work. 10 is the minimal we can set this value to. WEnd Func LoadVLC($iPlayFile, $iPlayFileStartTime, $iPlayFileEndTime, $iPlayAudioDevice = "Microsoft Soundmapper") ShellExecuteWait($VLC_Path, '--qt-start-minimized --play-and-exit --start-time="' & $iPlayFileStartTime & '" --stop-time="' & $iPlayFileEndTime & '" --aout=waveout --waveout-audio-device="' & $iPlayAudioDevice & '" "' & $iPlayFile & '"', $VLC_WorkingDir, "", @SW_HIDE) Beep(500, 200) EndFunc ;==>LoadVLC For example, I have a song called "MoonlightSonata.mp3" and I want to activate it with hotkey !{numpad9}. However, hotkeyset does not allow sending of flags so I cannot use LoadVLC as it is now. I need to create a function that stores the flags using the data from the earlier Inireads. Ik how to use the data but not how to create the function to store that data. I looked at IsFunc() second example but I do not understand. Sorry if I am not being clear on what I am trying to do. My brain is fried right now after trying various things for an hour. Any help would be appreciated. 
      The idea is to be able to have a dedicated hotkey for each sound I want to play so I will need to have my script create a new function by itself using the data from the INI.
      hotkeyset("$hotkey1","MySoundBoard1") hotkeyset("$hotkey2","MySoundBoard2") func MySoundBoard1() LoadVLC("MoonlightSonata.mp3") ;the other flags are optional and will not be set for simplicity. endfunc func MySoundBoard1() LoadVLC("TheBananaSong.mp3") endfunc Honestly I don't care how it is done as long as I have a dedicated hotkey for each entry in my ini.  An example of such an entry looks like
      [Sound1] File="C:\Users\BetaL\Music\SampleTrack1.mp3" StartTime="12" EndTime="34" PlaybackDevice="Microsoft Soundmapper" Hotkey="!{numpad8}" [Sound2] File="C:\Users\BetaL\Music\SampleTrack2.mp3" StartTime="24" EndTime="43" PlaybackDevice="Microsoft Soundmapper" Hotkey="!{numpad9}" Am I making any sense? Please let me know.
      Edit: Huge thanks to @Melba23 for the learning experience, his time, and help.
    • dynamitemedia
      By dynamitemedia
      i have the following snippet...   now its working but i have it inside a function and want to be able to use   $aThumb[$i]  outside the function in the rest of the script, i tried return and keep getting this error  "Invalid keyword at the start of this line.:"  
      Global $iRows = UBound($a, $UBOUND_ROWS) Global $iCols = UBound($a, $UBOUND_COLUMNS) $oID = $oID + 1 $oURL = $oString.selectSingleNode("./url") $oName = $oString.selectSingleNode("./name") $oCategory = $oString.selectSingleNode("./category") $oThumb = $oString.selectSingleNode("./image") $oLanguage = $oString.selectSingleNode("./language") $aThumb = [$iRows] _ArrayAdd($aThumb, $oThumb.text) For $i = 1 To UBound($aThumb) - 1 ConsoleWrite($oID & @TAB & $aThumb[$i] & @CRLF) Next Next ConsoleWrite( "rows: " & $iRows & @CRLF) Thanks for your help