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


Groups > comp.lang.javascript > #11167 > unrolled thread

Misspelling Can Bite You

Started byGene Wirchenko <genew@ocis.net>
First post2012-02-02 14:02 -0800
Last post2012-02-07 21:54 +0000
Articles 9 — 6 participants

Back to article view | Back to comp.lang.javascript


Contents

  Misspelling Can Bite You Gene Wirchenko <genew@ocis.net> - 2012-02-02 14:02 -0800
    Re: Misspelling Can Bite You "Richard Cornford" <Richard@litotes.demon.co.uk> - 2012-02-03 01:37 +0000
      Re: Misspelling Can Bite You Gene Wirchenko <genew@ocis.net> - 2012-02-02 18:09 -0800
      Re: Misspelling Can Bite You Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-03 04:15 +0100
    Re: Misspelling Can Bite You Dr J R Stockton <reply1205@merlyn.demon.co.uk> - 2012-02-03 22:49 +0000
      Re: Misspelling Can Bite You "Mel Smith" <med_cutout_syntel@aol.com> - 2012-02-04 13:21 -0700
        Re: Misspelling Can Bite You Dr J R Stockton <reply1205@merlyn.demon.co.uk> - 2012-02-05 16:59 +0000
          Re: Misspelling Can Bite You "Mel Smith" <med_cutout_syntel@aol.com> - 2012-02-06 10:28 -0700
            Re: Misspelling Can Bite You Dr J R Stockton <reply1206@merlyn.demon.co.uk> - 2012-02-07 21:54 +0000

#11167 — Misspelling Can Bite You

FromGene Wirchenko <genew@ocis.net>
Date2012-02-02 14:02 -0800
SubjectMisspelling Can Bite You
Message-ID<po1mi75fb0u5rqp9or413nv26799lpnndj@4ax.com>
Dear JavaScripters:

     Once again, it is time to thank me for pointing a possible source
of errors in JavaScript code or to chuckle over how I messed up again,
maybe both.

     I misspelled the name of the control variable for a switch.  I
had
          switch (this.DateType)
when it should have been
          switch (this.DataType)

     JavaScript was quite happy to execute the code.  After all, the
switch might have had a case to handle undefined.

Sincerely,

Gene Wirchenko

[toc] | [next] | [standalone]


#11172

From"Richard Cornford" <Richard@litotes.demon.co.uk>
Date2012-02-03 01:37 +0000
Message-ID<NcednWLYUtTEp7bSnZ2dnUVZ8hqdnZ2d@giganews.com>
In reply to#11167
Gene Wirchenko wrote:
> Dear JavaScripters:
>
>     Once again, it is time to thank me for pointing a possible
> source of errors in JavaScript code or to chuckle over how I
> messed up again, maybe both.
>
>     I misspelled the name of the control variable for a switch.
>  I had
>          switch (this.DateType)
> when it should have been
>          switch (this.DataType)
>
>     JavaScript was quite happy to execute the code.  After all,
> the switch might have had a case to handle undefined.


Really? Which browser, because the language's syntax won't stand for a - 
case - clause inside a Block statement (and a - switch - isn't much use 
without at least one - case - clause), so there should have been a 
syntax error to prevent compilation, and so prevent any execution.

However, the situation you describe, of mistyping a keyword and still 
having code execute is a possibility. One example would be mistyping - 
if - to give:-

id(x)
{
  y = 12;
}

- which would produce a runtime error, "id is not a function", rather 
than a syntax error.

That (and possibly your code above) raises a point about the placement 
of braces, because if you do:-

id(x){
  y = 12;
}

- with the opening brace on the end of the same line as the mistyped - 
if - keyword and its expression, you get a syntax error instead of a 
runtime error. That is because the absence of a carriage return prevents 
the language's automatic semi-colon insertion from acting to 'correct' 
the syntax of the latter in the way it 'corrected' the syntax of the 
former.

Personally I prefer the coding style represented in that final example, 
but I have to admit that over the years I have been looking for a (any?) 
justification for choosing that coding style over its alternatives and 
have been unable to find anything substantial to indicate a choice of 
any one common style over another with regard to the placement of 
braces.

This syntax error in preference to a runtime error (syntax errors are 
always reported while runtime errors only occurred if the code 
containing them is actually executed, and that is difficult to guarantee 
during testing) is pretty much the only justification for a 'brace at 
the end of the first line' coding style that I have seen, and there may 
be direct counter examples that exists but haven't yet been presented, 
or occurred, to me.

Incidentally, you should find yourself an editor with a customisable 
syntax highlighting system and have the keywords colored such that they 
are distinct from all other sequences of characters in the code. That 
way it becomes very visually apparent if you mistype a keyword, and you 
get to spot/find the mistakes sooner/easier and so fix them more 
quickly.

Richard. 

[toc] | [prev] | [next] | [standalone]


#11173

FromGene Wirchenko <genew@ocis.net>
Date2012-02-02 18:09 -0800
Message-ID<o9gmi7p1oe0g23npaneoii4mabj2b4hkln@4ax.com>
In reply to#11172
On Fri, 3 Feb 2012 01:37:31 -0000, "Richard Cornford"
<Richard@litotes.demon.co.uk> wrote:

>Gene Wirchenko wrote:
>> Dear JavaScripters:
>>
>>     Once again, it is time to thank me for pointing a possible
>> source of errors in JavaScript code or to chuckle over how I
>> messed up again, maybe both.
>>
>>     I misspelled the name of the control variable for a switch.
>>  I had
>>          switch (this.DateType)
>> when it should have been
>>          switch (this.DataType)
>>
>>     JavaScript was quite happy to execute the code.  After all,
>> the switch might have had a case to handle undefined.
>
>
>Really? Which browser, because the language's syntax won't stand for a - 

     IE 9.

>case - clause inside a Block statement (and a - switch - isn't much use 
>without at least one - case - clause), so there should have been a 
>syntax error to prevent compilation, and so prevent any execution.

     Huh?  The sequence was syntactically correct.  The only problem
with it was a misspelling of a property name.  That corrected, the
code ran fine.

>However, the situation you describe, of mistyping a keyword and still 
>having code execute is a possibility. One example would be mistyping - 
>if - to give:-

     I described the result of misspelling a property name, not a
keyword.

[snip]

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#11174

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-02-03 04:15 +0100
Message-ID<1344140.x7N3WIZj58@PointedEars.de>
In reply to#11172
Richard Cornford wrote:

> Gene Wirchenko wrote:
>>     Once again, it is time to thank me for pointing a possible
>> source of errors in JavaScript code or to chuckle over how I
>> messed up again, maybe both.
>>
>>     I misspelled the name of the control variable for a switch.
>>  I had
>>          switch (this.DateType)
>> when it should have been
>>          switch (this.DataType)
>>
>>     JavaScript was quite happy to execute the code.

JavaScript may not even have been used.

>>     After all,
>> the switch might have had a case to handle undefined.
> 
> Really? Which browser, because the language's syntax won't stand for a -

There is not *the* language with regard to browsers, unless you consider 
"JavaScript" to be a stand-in for "ECMAScript (implementations)", which is 
wrong.

> case - clause inside a Block statement (and a - switch - isn't much use
> without at least one - case - clause), so there should have been a
> syntax error to prevent compilation, and so prevent any execution.

I think he meant

  switch (this.bar)
  {
    case undefined:
      …
  }

where the object referred to by `this' had a `foo' property instead.  Only 
that in this case `foo' and `bar' were much more similar.

Barring `this' referring to a host object or `bar' being a specially 
designed accessor property¹, any conforming implementation of ECMAScript Ed. 
1 to 5.1 would execute that code without throwing an exception, with or 
without the `case undefined' clause, because you can attempt to access any 
property of the object referred to by `this' or the object converted from 
the value of `this' (ES 5.x strict mode) without it throwing an exception.  
Accessing a non-existing property merely results in `undefined'.  (I am 
presently not aware of any feature in ES 5.1 that would change that 
behavior.  Strict mode is not it.  SpiderMonkey's __noSuchMethod__ property² 
only allows it for method calls.  ES Harmony is going to allow it with 
dynamic proxies, but not on regular objects.³)

¹ Object.defineProperty(this, "bar", {
    get: function () {
      throw new TypeError();
    }
  });

² this.__noSuchMethod__ = function (id, args) {
    throw new Error(id + "(" + args.join(", ") + ")");
  };

  this.bar();

³ var o = Proxy.create({
    get: function (rcvr, name) {
      if (name == "bar")
      {
        throw new TypeError();
      }
    }
  }, this);

  o.bar;

  See also 
<http://www.youtube.com/watch?v=A1R8KGKkDjU&feature=plcp&context=C38a9548UDOEgsToPDskJEdhctNqhQUmYwp-
S69WbK>
   
> However, the situation you describe, of mistyping a keyword and still
> having code execute is a possibility. One example would be mistyping -
> if - to give:-
> 
> id(x)
> {
>   y = 12;
> }
> 
> - which would produce a runtime error, "id is not a function", rather
> than a syntax error.
> […]

While this is valuable information indeed, I think you have misunderstood 
the OP.


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>

[toc] | [prev] | [next] | [standalone]


#11180

FromDr J R Stockton <reply1205@merlyn.demon.co.uk>
Date2012-02-03 22:49 +0000
Message-ID<clw4wWGWSGLPFw+L@invalid.uk.co.demon.merlyn.invalid>
In reply to#11167
In comp.lang.javascript message <po1mi75fb0u5rqp9or413nv26799lpnndj@4ax.
com>, Thu, 2 Feb 2012 14:02:39, Gene Wirchenko <genew@ocis.net> posted:

>     Once again, it is time to thank me for pointing a possible source
>of errors in JavaScript code or to chuckle over how I messed up again,
>maybe both.
>
>     I misspelled the name of the control variable for a switch.  I
>had
>          switch (this.DateType)
>when it should have been
>          switch (this.DataType)
>
>     JavaScript was quite happy to execute the code.  After all, the
>switch might have had a case to handle undefined.



Putting the following script element last, or making it a body onload
function, will list all the script words of more than 2 letters, in
alphabetical order, with the number of times used.  The result will be
browser-dependent; but not gravely so.  Demo in my js-misc1.htm.


<script type="text/javascript">

  var skripts = document.getElementsByTagName("SCRIPT")
  var L = skripts.length, Str = "", Out = []
  while (L--) Str += skripts[L].innerHTML
  Str = Str.split(/\W+/).sort()
  var was = "", K = 0
  L = Str.length
  while (L--) {
  if (Str[L] == was) K++ ; else {
    if (was.length > 2 && /^[a-z]/i.test(was)) Out.push(K + "  " + was)
    K = 1 ; was = Str[L] } }
  document.write("<pre>" + Out.reverse().join("\n") + "<\/pre>")

</script>


The code could, no doubt, be optimised.  The output might better be to a
textarea or a new page.  It could be well to NOT scan words in strings
or comment.


Code could be added to pick out cases where two adjacent words are
unreasonably similar.

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#11194

From"Mel Smith" <med_cutout_syntel@aol.com>
Date2012-02-04 13:21 -0700
Message-ID<9p5i9vFn1kU1@mid.individual.net>
In reply to#11180
Dr. J said:

> Code could be added to pick out cases where two adjacent words are
> unreasonably similar.


Dr J and Gene:

    This morning I was caught (and mislead) by the following set of two 
statements:

    var rgn = document.getElementById("iselectrgn") ;
    var rgnopts = rgn.getElementsByTagname("option") ;

    Because I *thought* I had checked the statements thoroughly, I looked 
everywhere *after* this set, and finally went back and checked them again 
letter-by-letter, and then ...

-Mel Smith



[toc] | [prev] | [next] | [standalone]


#11200

FromDr J R Stockton <reply1205@merlyn.demon.co.uk>
Date2012-02-05 16:59 +0000
Message-ID<FIgu7uHWVrLPFwPZ@invalid.uk.co.demon.merlyn.invalid>
In reply to#11194
In comp.lang.javascript message <9p5i9vFn1kU1@mid.individual.net>, Sat,
4 Feb 2012 13:21:14, Mel Smith <med_cutout_syntel@aol.com> posted:

>Dr. J said:
>
>> Code could be added to pick out cases where two adjacent words are
>> unreasonably similar.
>
>
>Dr J and Gene:
>
>    This morning I was caught (and mislead) by the following set of two
>statements:
>
>    var rgn = document.getElementById("iselectrgn") ;
>    var rgnopts = rgn.getElementsByTagname("option") ;
>
>    Because I *thought* I had checked the statements thoroughly, I looked
>everywhere *after* this set, and finally went back and checked them again
>letter-by-letter, and then ...

Readily found by Firefox/Opera/Safari Error Console, Chrome JavaScript
Console, and by an IE8 pop-up, though.

-- 
 (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk  Turnpike 6.05  WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

[toc] | [prev] | [next] | [standalone]


#11209

From"Mel Smith" <med_cutout_syntel@aol.com>
Date2012-02-06 10:28 -0700
Message-ID<9pagtqFvc2U1@mid.individual.net>
In reply to#11200
Dr. J said:
>>
>>    var rgn = document.getElementById("iselectrgn") ;
>>    var rgnopts = rgn.getElementsByTagname("option") ;
>>
>>    Because I *thought* I had checked the statements thoroughly, I looked
>>everywhere *after* this set, and finally went back and checked them again
>>letter-by-letter, and then ...
>

> Readily found by Firefox/Opera/Safari Error Console, Chrome JavaScript
> Console, and by an IE8 pop-up, though.

    Yes, I use IE 7 on my dev machine, and 'once-in-a-while' (when I'm 
nearing the production stage) I check out the appearance on Chrome and FF.

    However, I didn't *this* time and spent a couple of confusing and 
frustrating hours tracking down this 'devil'.

-Mel Smith

[toc] | [prev] | [next] | [standalone]


#11479

FromDr J R Stockton <reply1206@merlyn.demon.co.uk>
Date2012-02-07 21:54 +0000
Message-ID<$qJAjIGv2ZMPFwzn@invalid.uk.co.demon.merlyn.invalid>
In reply to#11209
In comp.lang.javascript message <9pagtqFvc2U1@mid.individual.net>, Mon,
6 Feb 2012 10:28:19, Mel Smith <med_cutout_syntel@aol.com> posted:

>Dr. J said:
>>>
>>>    var rgn = document.getElementById("iselectrgn") ;
>>>    var rgnopts = rgn.getElementsByTagname("option") ;
>>>
>>>    Because I *thought* I had checked the statements thoroughly, I looked
>>>everywhere *after* this set, and finally went back and checked them again
>>>letter-by-letter, and then ...
>>
>
>> Readily found by Firefox/Opera/Safari Error Console, Chrome JavaScript
>> Console, and by an IE8 pop-up, though.
>
>    Yes, I use IE 7 on my dev machine, and 'once-in-a-while' (when I'm
>nearing the production stage) I check out the appearance on Chrome and FF.
>
>    However, I didn't *this* time and spent a couple of confusing and
>frustrating hours tracking down this 'devil'.

One fault with those Error Consoles is that they lack a "Stay On Top"
checkbox, or one for "Jump up when written to".

Re my earlier code : given a reference to a script element, one can get
.innerHTML and (in all my browsers) read as a string the script as
transmitted.  Is there any easy way of getting that string, MINUS all
comment and MINUS all inner strings (or, in each case, removing the
content of the comments and strings) while leaving all the other code?

-- 
 (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk  Turnpike 6.05  WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web