Mike said:
You're assuming that Javascript programmers actually validate return
values - even C programmers don't do that as much as that should ;-)
I would be being very short-sighted if I assumed any such thing. The
unfortunate reality is the vast majority of the people authoring
javascript are not programmers, don't really understand what they are
doing and do not apply any standards to what they produce (or even
aspire to). But that is not a good reason for making it difficult for
those that do.
In reality javascript programmers (at least those working with web
browsers) have more reason for being defensive and cautions than C
programmers do. A C program in an equivalent context, interacting with a
DOM, could be written with a fair confidence about the DOM
implementation. At minimum it could expect to only have to work with one
implementation at a time, and that implementation would be known.
Browser scripts are written to be sent out to unknown browser software
where they realistically should expect to find numerous more or less
divergent DOM implementations. Script failure is a consequence so
inevitable in that context that its controlled handling should be
written into the script, and that means checking to see when it can be
predicted, is happening or has happened.
Learning how to do that efficiently is a major part of learning to be
good at browser scripting. for example, if your function returned NaN on
the first call it could reasonably be assumed that it would continue to
do so. There would be no point calling it again. While a non-NaN numeric
return value on the first call would be grounds to assume that all
subsequent calls will also returned non-NaN values (subject to the
element not being removed from the DOM by the script). Testing that you
have a position reporting facility before its use is vital but, once
checked, testing all subsequent returned values would needlessly slow
the script down.
... , was that the functions would be more likely
to fail gracefully this way.
It is really the code employing the functions that should be failing
gracefully, as it is the code that would be written with a knowledge of
what was appropriate as a response to failure in its context.
... . I hesitate to make indiscriminate
changes because many people are using the core functions. ...
<snip>
Yes, you can be certain that if you change the behaviour of an API, even
objectively for the better, then some of the code written to use
previous versions it will promptly fail.
Richard.