- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Isn't it possible that this is just stale code that is still there because that portion of the code base didn't need to be touched in a whole bunch of years? As soon as you start mucking with things that aren't broken you risk introducing new bugs. Hardly makes this WTF worthy.
Admin
This is the WTF. There are two pieces of information to return. One is conditional depending on the other, but the function fuses them into a single return of just one value, rather than returning a tuple or similar, either (Python-ish syntax for the tuple, sorry):
or
Admin
The solution is of course return 12 instead of false.
Admin
"there's simply no excuse for this user-agent sniffing game."
Yep, just test in Chrome, and if it works, job done.
Admin
Ah, that old chestnut. But really, we should be asking ourselves... What Is Truth?
if (isIE() != 'FileNotFound' && isIE() <= 10) { alert("The browser you are using is too old and not supported anymore. Please get a newer one."); }
Admin
No no no. Return (uint.Max / 2) - 1234, or equivalent.
This guards against almost all future agent numbering systems, avoids the possibility that some loon compiler has coalesced the range of unsigned integers with the range of signed integers, and also guards against the possibility of the agent string being defaulted to uint.Max.
I mean, obviously, there's a small keyhole propensity to error here, but I would claim that it is unlikely.
Admin
The other WTF thing here is the fact that ECMA5 (AKA Javasript in 2009, which I devoutly hope is the minimum here) coerces a boolean into an integer.
I realise that Javascript was knocked together in about two weeks, and frankly it was a phenomenal achievement. But if you're going to create a language that leans heavily towards FP, you really don't want to rely on crusty ole C style coercions.
Admin
Jokes aside, I would have gone with a function name of "GetIEVersion" and it can return null, if not IE, or a number, if it is. Then you can do:
if(GetIEVersion() !== null && GetIEVersion() <= 10) { ... }
Admin
: 0; // Fixed
Admin
JavaScript isn't the only language that shows this behaviour. PHP, a language with a library set notorious for its many, many "return a proper value or otherwise false" functions, also regards false as <= 10.
Admin
Classic IE exceptionalism. It looks like when they finally began to comply with RFC7231 5.5.3 in IE11, "msie" was replaced with Trident, so
if (isIE() && isIE() <= 10)
in particular could very well be replaced with justif (isIE())
.Admin
There's still a valid case for user agent sniffing... When you want to show instructions on how to "install" a PWA. Each browser does their own thing, you either sniff and give the user the correct instructions or you list them all and let the user figure it out (the user won't).
Admin
Or you could argue there's one piece of information to return: the IE version number. And if there's no version number, you shouldn't return a number. So return something else. Nowadays, that's null, but in a lot of cases, false is semantically equivalent.
I would argue that the WTF is that a function named is* returns anything other than true or false. If it were named something like getIEVersion(), it's a perfectly fine function, though nowadays we probably would return null instead of false.
I think people tend to forget that in the environment for which JavaScript and PHP were developed — handling HTML form input fields — ALL data starts as a string, and enforcing a strict type structure in such an environment just adds tedium.
Admin
Much simpler approach: Return -1 for the version if it's not there. Version numbers are always positive.
Admin
Wouldn't work properly: it would detect Firefox as a "too old Explorer".
Admin
Makes more sense to just take a test version as an argument and return true if the version matches or false otherwise.
Admin
As others have alluded, the issue is not really about JavaScript's type conversion; it's with trying to return 2 things from the same function.
In a strongly typed language you would have probably chosen to return to return either 0 or -1 if the browser was not IE (or if you were a real pedant you could return a tuple or a union type) but whatever you did and would have led to very similar logic required of the caller, because you need to make 2 separate tests of the result
Admin
It's a bit unfair to criticise javascript for doing exactly the same thing as C, C++, .... do
there are times when I feel fortran is an improvement on C because it doesn't let you do stupid things like that.
Admin
Challenge accepted