Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <5675043.5vPKIy9VtG@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 05 Dec 2012 20:18:45 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 59 NNTP-Posting-Date: 05 Dec 2012 20:18:47 CET NNTP-Posting-Host: aaa68ee1.newsspool1.arcor-online.net X-Trace: DXC=b[SJf8KAD=dg`45cDR8l?oic==]BZ:afn4Fo<]lROoRankgeX?EC@@`0E8X5N4L0EbDZm8W4\YJNlR=i2=[N6Y6jISc2lPJ3HAdMUZiLaS@B2h X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17499 glathoud wrote: ^^^^^^^^ Please fix that. > To check argument types in functions that are used in many different > places, I have been using this: > > function someCoreFunction( domnode, text ) { > > // "Cheap runtime assert": fails when `domnode` is not a DOM node. > domnode.childNodes.a; > > // Same idea, for a string. > text.substring.a; > > // Rest of the function > // ... > > } > > It helps to catch mistakes early, including during development. I am > *not* advocating to litter the code with thousands such statements, > but in a few strategical places the benefits can exceed the costs. > > Hopefully this can help someone. Constructive feedback is welcome. This approach is not going to work; IOW, it is *too* cheap. In order to let the program continue running, instead of bothering the *user* with runtime errors and then end prematurely, you will have to catch the TypeError that is thrown when execution cannot resolve the “a” property of “undefined”. There is no reasonable, cross-implementation way in which you can tell one TypeError from another in a program. Further, you are potentially dealing with host objects here. It is a bad idea to access host objects' properties without testing them for existence first. In the worst case the *successful* property access will throw an exception. Your experience may not be sufficient to know that a runtime error occurs then, but it does, and the user will probably see it (for example, Internet Explorer will display an exclamation mark icon in the status bar left-hand side). The program will not end cleanly although that could be done, which is a really bad idea in itself. The proper and only reliable way is to check passed arguments against the function requirements, and throw a user-defined exception. Or return a false-value, if feasible, so that subsequent uses of the return value fail and the problem can be tracked down to the incorrect method call). See ”typeof” and “isHostMethod”, and “jsx.throwThis()” for that. HTH PointedEars -- Sometimes, what you learn is wrong. If those wrong ideas are close to the root of the knowledge tree you build on a particular subject, pruning the bad branches can sometimes cause the whole tree to collapse. -- Mike Duffy in cljs,