[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Should libraries perform type checking?
From:       "Jason Voegele" <jason () jvoegele ! com>
Date:       2002-07-09 14:54:18
[Download RAW message or body]

> When coding a library that is meant to be reused in other code, is it
> a good idea to make it so that the library will react gracefully if
> you pass it a type that it wasn't expecting?

One option you have is to use the Design By Contract package[1], which would
allow you to put type checks (or method availability checks) in the
precondition.  This way, the user of the library could choose whether or not
the type checks are actually performed at run-time.

[snip]
> That is from cgi.rb. If you try to do this:
>
>     CGI.escapeHTML(nil)
>
> or pass it anything else that doesn't have a gsub() method, it will
> crash but the error message it gives you is not meaningful and
> requires actually looking at the code for cgi.rb to figure out what
> happened.

Yes, if there are any required methods you should certainly document that
for your library.  Additionally, you could check the availability of
required methods (either in a precondition or just at the top of the method)
to make sure the methods you need are there.  It's not common practice to
check the actual type of arguments, only that they support the required
methods.

[1] http://www.pragmaticprogrammer.com/ruby/downloads/dbc.html

Jason Voegele





[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic