Jump to content

Different ways of doing type checks and contracts


Recommended Posts

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 & ")")
EndFunc

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
Moved
Link to comment
Share on other sites

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)

Link to comment
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
 Share

×
×
  • Create New...