- 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
Can it be that the code was broken while it was prepared for this site? That doesn't even look remotely runnable (s <-> str, VALID_FOLDERS <-> FOLDERS, etc.).
Admin
Too close to reality for comfort :o
Admin
I sure am glad that this guy optimized it and didn't do somthing foolish like a switch case!
Admin
The real WTF is that the article isn't escaped properly, so the C# comments are parsed by my browser, and breaks the layout of the page.
Admin
In Germany they speak deutsch, shortened to "deu" in the table.
Admin
return s;
What is s? Is it supposed to be str?
Although 3 separate comparisons might be faster than copying the substring, but it would still allow urls like /duelish/foo/bar.html and match it with "deu"
Admin
I'm not sure what the bigger wtf is - the code (if I can geuss what it's actually supposed to look like, as that snippet won't even compile), the fact that the HTML wasn't escaped so it broke the posting, or the fact that someone clearly didn't preview his submission before making it...
Admin
Well, what happens id the URL is just "/" or something like "/x/"? ;)
Captcha: digdug
Admin
To be pedantic, in Germany they speak German. Deutsch is German for German. Another w.t.f. is why the code doesn't consistently follow the convention of using the native name for a country/language. i.e. Spain should be 'Esp'.
Admin
i swear, i lost IQ points just reading this...
Admin
Easy Optimization: Use the 2 letter ISO language codes and save a comparison!
Admin
you mean, in deutschland ? they speak german of course ! and when not speaking, they are driving big fast cars...
worst, they may speak different languages within the SAME country !
Admin
WTF 1: I'm quite sure that the built-in comparison function must do something similar, but without bugs. Why (badly) reinventing the wheel?
WTF 2: Linear complexity. This would probably be faster with a map, especially when adding more languages (although for short collections, lists might outperform maps or hashtables).
The guy who wrote the function (and the comment!) has an odd idea of what optimisation is...
Captcha: Tesla. As tdwtf acts as a magnet on me?
Admin
Yeah and in the netherlands they speak nederlands thats why it is dut(ch)
Admin
It is almost certainly 'deu' because the developers are German. They aren't Dutch or Spanish, thats why those arent 'ned' or 'esp'.
In our system we have uk (means English innit!), french, german, (sanity takes hold after here), es, jp, ko, br, etc
Admin
With 1. such a small list, 2. short comparison (3 chars, one by one), and 3. modern cache size,
it is really hard to determine if using a hashtable would even be faster. There is also no telling if the entries in FOLDER array is arranged from the most frequently requested to the least which would give speed advantage to the OP's code.
So, I would not readily discount the efficiency of the OP's code. There are too many unclear variables.
Admin
What is Europe? And why don't they speak English like the rest of us?
Admin
coughHASHtablecough
Oh, my. Sorry about that, I seem to have a frog in my throat.
Admin
Why not use Resources?
CAPTCHA: tesla, close but... serbia.
Admin
Europe: The Swedish band, best known for "The Final Countdown". And yes, they do sing in English. ;)
Btw. I'm European and don't even own a car... Not even a small one...
Admin
The real WTF is that they used C#.
Admin
Europe is the little place on the far side of the pond where most of your ancestors came from (not counting the few native Americans that were allowed to survive and interbreed). It includes an insignificant island called England - the place English comes from
As for English, we don' speak it like the rest of you because we speak it as it should be spoken!
Admin
Also, notwithstanding cache sizes, etc. this is still sub-optimal. The language codes should be processed by prefix - that is, it sufficient to find an e to know that the language is english, or once one sees a d, then there are only two other options.
Alternatively, for these three-byte codes, they should be packed into ints, to do each comparison in a single machine instruction.
Admin
So whay kind of bicycle do you own?
Admin
Ack! And I thought the scheme used here where each locale has it's own column in a table was back-ass-wards.
Actual (paraphrased) quote from one of the senior-senior devs - "It is easier to just add a new column to the existing table than fix things properly." Nevermind that once the new column is added to the table, the corresponding data object needs a new field and all the code that reads the localized strings from said data object needs to know about the new getter for the field and then to check for some new locale and return the appropriate string.
Really, it's easier. And very WTF-worthy.
CAPTCHA: sanitarium ... scans iPod ... bangs head on table
Admin
I think you've just discovered the binary search algorithm.
Implementation is left as an exercise to the reader. There will be a quiz on Tuesday.
CAPTCHA: vern [RIP Ernest P Worrel]
Admin
And as far as I know, American English is just how British English was spoken 200 years ago ;)
Admin
Generally depends on what kind of bike the person who forgot to use his lock owned. Works pretty well.
Admin
trioBike, if you must know ;)
CAPTCHA: ewww ... Bad code, no twinkie
Admin
I don't understand what the problem is... The code really is pretty close to optimal. If you assume that the URL's validity has already been checked (length >= 4) and that "return s" is really supposed to be "return str" and that "FOLDERS" is really "VALID_FOLDERS", I don't see anything wrong with the code.
In a performance-critical application, it's perfectly reasonable to unroll the inner string comparision, saving a function call plus the math and object creation needed to extract the relevant substring from the URL. And using a hashmap may not be as efficient as a linear search over 11 values, considering that you'd also have to construct a substring, then find its hash in the hashmap.
I assume they've already checked to see whether foreach (creating and destroying an iterator) is more efficient than using a for loop with an index variable. Really, I'm not sure it could be much more efficient without fully unrolling the loop to conditionals or case statements, collapsing common prefixes; the resulting code would be unreadable -- you'd want to write a code generator for it. And even that may be slower, depending on cache effects and branch prediction.
Admin
I don't know about the rest of you, but I for one speak American, where's the option for that?!
</sarcasm>In all reality though, American English is so different these days, in pronunciation, slang, and meaning, that one could certainly consider it a whole language beyond the Queen's native tongue.
Admin
English?? you mean that weird little dialect of American?
How quaint...
Admin
Well, except Dutch (which should be 'nld'), all of these are actually ISO language codes (ISO 639-3 if you care). Which are different from ISO country codes.
Admin
they work less and play more Optimization
they drive tiny little cars. No need for big cars to drive around. And we also use a lot public transport ^^
Apparently, they all speak different languages too. We do this just for the fun when foreigners are around, in fact we all speak alien fluently.
Admin
And no, American English is just English written by lazy people who don't like 'u's.
Admin
Somehow I knew someone would reply with this. Thanks for the tip, Sherlock.
Admin
Admin
FWIW, I'm a Belgian myself and I don't speak Dutch very well :)
Admin
I'm gonna have to concur with the previous poster and say it feels like this is pretty optimal. Assuming the order of the countries is the order of the requests (it seems to be with the most common languages at the top assuming a German company) then unrolling the string comparison loop seems like a good idea.
And given the small size of the collection hashtables seems like a much slower system seeing as how you have to still produce the hash which isn't a zero-cost exercise. What I would suggest to anyone who feels this is sub-optimal is to benchmark it yourself using primarily requests from the top 5 countries and than about 10% combined from the rest. I'll bet you the code presented will be faster.
Admin
Bah. Europa. I prefer scandinavia. ;) Btw, there is a very small and odd language apart from nederlands. ;) dunno what its called in english though. Flamländska in swedish. Cool language.
Admin
Europe: The Swedish band, best known for "The Final Countdown". And yes, they do sing in English. ;)
'Europe' Swedish? You mean Finnish
Admin
or even 'Cas' for Castilian (just to separate it from American Spanish) :-)
Admin
WTF are they doing this in the application? They should just have apache rewrite the url... something like this:
RewriteCond %{REQUEST_URI} ^(/[^/])+)/(.)$ [NC] RewriteRule ^/(.)$ http://www.example.com/$2&lang=$1 [P,QSA]
Now I haven't tested it, but you should probably see what I mean.
Then again, most people running C# are running IIS, which is a WTF in itself.
Admin
Radix search.
CAPTCHA: e_www_. Yup.
Admin
That's really not a binary search. At all. Arguably a [base-26]-ary search, but that's still a stretch.
Admin
The Swiss have four official languages (French, German, Italian and Romanche). Swiss-German isn't an "official" language in Switzerland, because there is no consensus on one version: there are as many different variants of Schwyzerdüütsch as there are valleys. Now don't get me started on the /other/ languages across Europe: Alsacien, Occitan, Piemontese , Catalan, Cornish.... For those interested, do a Google search on the languages that the Asterix comics are printed in. Aah! Internationali[s|z]ation is such fun :-)
Admin
No, the REAL WTF is that the user's web browser already has a preference for what language they want to see the web site in, so the web server should just be using that.
Admin
And in the Netherlands, there's also Frysian as a second official language (though only in the province of Fryslan), and low Saxon is recognized as a regional language by the EU - beside which you've got some people in Limburg speaking in a dialect that borders on being a separate language if it wasn't so much like German.
Argh.
Admin
Rather than questioning whether or not this is the best way to optimise this functionality, this is really yet another example of premature optimisation (y'know, root of all evil...). So this function gets called for EVERY page…and so, what? Lots of things get called once per page in a typical dynamic-content website, including some sort of database access. This will easily overshadow the performance gained here.
Of course, maybe I'm wrong, maybe this app was properly profiled and the i18n WAS found to be the bottleneck. We can't tell. Yet, somehow, I doubt it… =)
Admin