Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17499

Re: Cheap runtime asserts

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> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
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+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: Cheap runtime asserts
Newsgroups comp.lang.javascript
References <1892433a-7696-4b03-a94c-0b1f0a984134@googlegroups.com>
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

Followups directed to: comp.lang.javascript

Show key headers only | View raw


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, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-04 22:00 -0800
  Re: Cheap runtime asserts Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-05 20:18 +0100
    Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-06 11:19 -0800
      Re: Cheap runtime asserts Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-06 23:41 +0100
  Re: Cheap runtime asserts Patricia Shanahan <pats@acm.org> - 2012-12-05 12:00 -0800
    Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-06 11:34 -0800
  Re: Cheap runtime asserts JJ <jaejunks@glegooilma-swapit.com> - 2012-12-06 07:29 +0000
    Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-06 11:40 -0800
      Re: Cheap runtime asserts JJ <jaejunks@glegooilma-swapit.com> - 2012-12-07 12:03 +0000
        Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-10 23:46 -0800
  Re: Cheap runtime asserts Stefan Weiss <krewecherl@gmail.com> - 2012-12-06 23:00 +0100
    Re: Cheap runtime asserts glathoud <glathoud@yahoo.fr> - 2012-12-11 00:07 -0800

csiph-web