Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17535
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2012-12-06 11:40 -0800 |
| References | <1892433a-7696-4b03-a94c-0b1f0a984134@googlegroups.com> <XnsA121943364577jaejunksglegooilma@0.0.0.23> |
| Message-ID | <cfb82688-a712-4e52-9ec3-ed7f0697320c@googlegroups.com> (permalink) |
| Subject | Re: Cheap runtime asserts |
| From | glathoud <glathoud@yahoo.fr> |
On Thursday, December 6, 2012 8:29:48 AM UTC+1, JJ wrote:
> glathoud <glathoud@yahoo.fr> wrote:
>
> > 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.
>
> >
>
> > Guillaume
>
>
>
> Some conditions might still won't cause an exception. For example:
>
>
>
> function theFunc(node, txt) {
>
> //the cheap assert (pre-access the property)
>
> node.childNodes[0].data;
>
> //actual code here
>
> if (node.childNodes[0].data === '') {
>
> node.childNodes[0].data = txt;
>
> }
>
> }
>
> theFunc(document.body);
>
>
>
> Now, assuming that "document.childNodes[0]" is a HTMLDivElement, the
>
> assert won't cause an exception and the next "if..." statement will be
>
> executed.
Yes, you are right. The goal is not to catch 100% of the mistakes, but to have a good chance to catch strategic mistakes earls (see my answer to Patricia).
In the case you are describing, you could instead make sure that the data property is defined IF that is what you need:
node.childNodes[0].data.a;
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