Matt said:
I think most people understand that feature detection can
only decide if features are available.
What most people understand often significantly diverges from reality.
Feature detection is an environment testing strategy that certainly does
extend to the actual behaviour/characteristics of features in addition
to their mere existence.
There is, of course, no way to determine if a feature
behaves according to standards or expectations.
In many cases it is entirely possible to verify that a feature behaves
in accordance to the standards. It is rarely necessary to do so, and
such testing is likely to be impractical much of the time. For example,
having verified the precise behaviour of the mathematical functions and
operators involved it is possible to implement the toFixed algorithm and
test all of the possible parameters against all of the representable
numeric values looking for differences between its results and those of
Number.toFiexed, but that test would take lifetimes to run and so is
certainly not practical.
However, it is a mistake to think that verifying correct behaviour is
the problem here. It would be sufficient to identify aberrant behaviour.
And for that task a small number of tests on sample numbers to see if
they return the expected string values should be satisfactory.
This is particularly true in the case of toFixed as the observed issues
relate to the faulty behaviour of JScript's current implementation.
Specifically the way in which JScript's toFixed rounds down if the digit
after the last that is to be output is 5 to 9 while the digits above the
are all zero, a single test can identify that fault reliably. E,G.:-
if(0.0055.toFixed(2) != '0.01'){
... // erroneous toFixed implementation.
}
And that one test to identify known faults in JScript could be
meaningfully extended with a small number of samples of other boundary
conditions, such as a number on each side of rounding up and rounding
down, and each side of the transition across a whole number (with
precision appropriate to the application, i.e. toFixed(2) if
presenting currency amounts).
This is definitely a limitation of the "feature detection"
strategy.
If feature detection could only verify the existence of a feature then
that would be a limitation. As it can be applied to the behaviour of a
feature as well then this is not an example of a limitation. In this
case the impracticality of verifying 100% correct behaviour of toFixed
is significantly mitigated by being able to identify known incorrect
behaviour, and the alternative of using a function in its place that
does have known behaviour.
In some cases, in addition to using feature detection, it
can be required to do browser detection to sort through quirks
in implementations of features in specific browsers.
That the feature detection strategy does have limitations is undoubtedly
true. The main reason for its recommendation as a strategy is that it
has fewer limitations than all other available strategies. It is
certainly superior to browser detection in every sense. Mostly because
there is no browser detection strategy that can accurately and
discriminatingly identify all browsers.
In this particular case identifying the browsers that are known to be
problematic, IE versions, would be significantly less effective than the
feature detection test proposed above. That test will identify toFixed
implementations that exhibit the problem known to be exhibited by
current (and past) JScript versions. While even if you could identify
all IE browsers that have used those JScript versions (and I am yet to
see a browser detection test that could deliver that information) the
results of such a test would still be wrong if Microsoft release a
JScript 7 version with the issue fixed, as JScript can be retro-fitted
to older browsers. And again, even if you could identify all IE browsers
that exhibit the problem you will not be detecting other browsers that
have the same issue in their ECMAScript implementations.
In the past, and generally, when assertions that feature detection is an
inadequate strategy are rendered concrete it becomes apparent that
feature detection tests can be discriminatingly applied, and in the rare
cases where they cannot it has never been demonstrated that browser
detection could be applied to produce more discriminating results.
IMO, though, if a browser implements a feature but does so
incorrectly, and the user sees an error, that doesn't bother me.
<snip>
If you are rounding money presenting inaccurate information has
potentially serious legal consequences.
Richard.