Janak Ramakrishnan

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromJanak Ramakrishnan
ToMe
Subjectissue with "Why Firefox's strict JavaScript warnings are wrong"
Date10 June 2008 02:01
Hi Mark, I'd like to thank you for having documentation online, and I
think your point that Firefox doesn't warn when a global variable is
overwritten by a local one is valid, and a good point.  However, your
example of Firefox warning when it shouldn't is, I think, not correct.
 Firefox warns when you write
if( document.body.childNodes[10] == e.target ) {
but it's not because childNodes may not be defined -- it's because if
childNodes doesn't exist, you can't do childNodes[10].  That is a
perfectly reasonable warning, and one that I would expect.  You also
say that it would warn if onclick weren't defined and you had this
code:
if( document.onclick == myFunction ) {
That is not correct -- Firefox will not warn then.  Firefox will only
warn if you try to take a property of a non-object.  To stop the
warning in the first case, you could write:
if (document.body.childNodes && document.body.childNodes[10] == e.target) {
which is good programming practice.  While you may be correct that
this warning dissuades people from doing cross-browser testing, the
solution is to add the additional test that the property exists in the
first place, before you test the value of the property.

Best,
Janak
FromMe
ToJanak Ramakrishnan
SubjectRe: issue with "Why Firefox's strict JavaScript warnings are wrong"
Date14 June 2008 15:41
Janak,

> if( document.body.childNodes[10] == e.target ) {
> it's because if childNodes doesn't exist, you can't do childNodes[10].

You have misinterpreted what causes the warning. If you do this, it will
still warn you:

if( window.document )
 if( document.body )
  if( document.body.childNodes )
   if( document.body.childNodes[10] == e.target )

The same happens here, even though there is no way that document will be
undefined in a normal script:

if( document.foo == e.target )

The warning has nothing to do with childNodes, since if that was missing it
would produce an error not a warning. The warning is when comparing the
undefined property 10 against an existing object, even though it is good
practice to do it that way (as discussed in earlier mails on this topic) to
avoid wasting CPU cycles.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromJanak Ramakrishnan
ToMe
SubjectRe: issue with "Why Firefox's strict JavaScript warnings are wrong"
Date15 June 2008 06:35
Sorry, you're quite right, I misunderstood your criticism.  But I
agree that, while Firefox might reasonably loosen their warning
system, it's good practice to do
if (foo.bar && foo.bar == 'baz')
or, with the tradeoff of specificity versus length (or if you're
testing for a property with value 0, etc.),
if (typeof(foo.bar) != 'undefined' && foo.bar == 0)

I think it's good practice because most other programming languages
have a mode where they warn about such undefined properties, and also,
in some very real sense, your first test should be if the property is
defined -- that is already a browser-capability test.

Best,
Janak
FromMe
ToJanak Ramakrishnan
SubjectRe: issue with "Why Firefox's strict JavaScript warnings are wrong"
Date15 June 2008 09:59
Janak,

> it's good practice to do
> if (foo.bar && foo.bar == 'baz')

Not in a loosely typed language like JavaScript it's not. In JavaScript that
is a 100% performance penalty, for no good reason. See my earlier mails on
the subject (linked from the article).

Tarquin
FromJanak Ramakrishnan
ToMe
SubjectRe: issue with "Why Firefox's strict JavaScript warnings are wrong"
Date19 June 2008 08:31
Hi Tarquin, I think that this is an issue that we'll have disagree on.
 Other loosely typed scripting languages (like PHP and Perl) have the
option to turn on "strict" warnings.  PHP will issue a "notice" if you
use an undefined variable.  Perl will error out on uninitialized
variables if you say
use strict;
at the beginning of your script.  I understand what you're saying --
that in some cases, you are testing for the presence of a property in
the first place.  However, that is the case in PHP as well -- in the
$_REQUEST array, there may or may not be various attributes.  PHP
throws up a notice if you access a nonexistent attribute.

I think that accessing an undefined attribute for the purpose of
testing its value is different from accessing it for the purposes of,
say, assigning it to another variable, and, in a more intelligent
scripting world, the two might generate different levels of errors.
But as it stands, I think that Mozilla is in the mainstream in warning
about undefineds, and pleading special circumstances for Javascript is
not very convincing to me.  There are lots of scripting languages, and
they all need to run quickly, and there is a way around this warning
in Javascript, as I (and all the other posters -- I see how many times
you've dealt with this issue) have said.

Janak
FromMe
ToJanak Ramakrishnan
SubjectRe: issue with "Why Firefox's strict JavaScript warnings are wrong"
Date21 June 2008 09:19
Janak,

> PHP will issue a "notice" if you use an undefined variable.

PHP is not relevant since it's purpose and approach is very different.
For a start it is based around procedural code, not objects (it can use
objects and properties, but they are an afterthought) and in PHP, using
an uninitialised variable is extremely dangerous since it can be
populated using the query string and used for cross site scripting
attacks. The scope of JavaScript is different, and its warnings should
likewise be different. If all languages should be treated the same, they
should all be the same language. They are not.

> But as it stands, I think that Mozilla is in the mainstream in warning
> about undefineds

It fails to warn about real problems (overwriting variables in the
parent scope), it warns about things that are not problems, it
encourages very bad development style that fails to work cross browser
(!= null, for example), it makes authors shy away from writing proper
cross browser scripts, and last but not least, it encourages authors,
yourself included, to write scripts that perform twice as slowly as a
properly written script. All for the purpose of satisfying a stupid
debug setting, which causes problems for real browsers.

I fail to see how anyone, unless they completely fell for the
Mozilla-can-do-no-wrong nonsense, could possibly think any of that was a
good thing. I am not saying that warnings are not useful when debugging,
only that Mozilla's implementation is broken to the point of making
things worse not better.

But I guess if you are not convinced by now, there is nothing more I can
say to convince you otherwise. I can only hope that you do not go out of
your way to avoid any of those mistaken warnings, the way I see others do.
FromJanak Ramakrishnan
ToMe
SubjectRe: issue with "Why Firefox's strict JavaScript warnings are wrong"
Date21 June 2008 10:48
Hi Tarquin,

> objects and properties, but they are an afterthought) and in PHP, using
> an uninitialised variable is extremely dangerous since it can be
> populated using the query string and used for cross site scripting

This doesn't make sense -- if the variable has been automatically
populated (using register_globals), then PHP won't warn, AFAIK.  As a
side note, enabling register_globals is insane.  This warning is also
there in Perl, and in every other decent language that I know of that
allows undeclared variables.

>> But as it stands, I think that Mozilla is in the mainstream in warning
>> about undefineds
>
> It fails to warn about real problems (overwriting variables in the

I noted my appreciation for your page in my first email -- I think
that your other criticisms of Mozilla's debugging are quite valid.

> yourself included, to write scripts that perform twice as slowly as a
> properly written script. All for the purpose of satisfying a stupid

This is a deceptively-phrased statement.  While testing for existence
and then value may, as you say, be twice as slow as just testing for
value, most scripts do more than that.  I sincerely doubt that any
real script has this test as a bottleneck -- testing for existence
should only ever be done once for a given property.

[Ed. Note that in a loop, this condition could be checked hundreds of
thousands of times, and would indeed become a bottleneck. This does
happen in real-World cases.]

I understand your criticism, and your request for ease-of-use for
Javascript, but there are certain programming conventions that are
fairly universal.  Not using goto's, for instance.  If you had an
example of another language where testing an uninitialized value was
always ok, I would be more inclined to agree with you.  But
Javascript's origins are so messy, if it breaks a rule that all other
languages follow, I am not going to think that Javascript is in the
right.

[Ed. Note that JavaScript is not breaking any rules here. Firstly it is
its own language, not another language, and can do whatever makes sense in
JavaScript, even if that is not the same as other languages. Secondly, many
other loosely-typed languages allow the exact same construct, and testing
of an undefined property against a defined value.]

Janak
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.