logo       

Is it a good practice to enforce type safety in a dynamic language? If so, : msg#00077

programming.language-of-the-year

Subject: Is it a good practice to enforce type safety in a dynamic language? If so, how?

I'm writing a small JavaScript library, which I'm trying to keep very
OO. Here's a typical example of the functions I've written:

function Lib_hasClassName( className ) {
var classNames = this.className.split( " " );
for ( var i = 0; i < classNames.length; ++i ) {
if ( classNames[ i ] == className ) {
return true;
}
}
return false;
}

Since I'm using the this keyword, I'm assuming that the implementer
will attach it to their object, then call it. Something like:

AnObject.hasClassName = Lib_hasClassName;
AnObject.hasClassName( "aClassName" );

Or perhaps:

Lib_hasClassName.call( AnObject, "aClassName" );

Or even:

AnObject.prototype.hasClassName = Lib_hasClassName;

You get the idea.

Should I check that the object that the this keyword refers to is of
the correct type, or at least has the required methods and properties,
or should I rely on the implementer to know what they're attaching to
their object? If so, how should I communicate to the implementer that
I need a particular type of object? Clear code? Exceptions? Documentation?


James Tikalsky



------------------------ Yahoo! Groups Sponsor --------------------~-->
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/nhFolB/TM
--------------------------------------------------------------------~->


Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/pragprog/

<*> To unsubscribe from this group, send an email to:
pragprog-unsubscribe-hHKSG33TihhbjbujkaE4pw@xxxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/





<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise