Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17499
| Message-ID | <5675043.5vPKIy9VtG@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-12-05 20:18 +0100 |
| Subject | Re: Cheap runtime asserts |
| Newsgroups | comp.lang.javascript |
| References | <1892433a-7696-4b03-a94c-0b1f0a984134@googlegroups.com> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
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
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