|
|
|
| Non-WTF Job: Junior .NET Developer at West Monroe Partners (Chicago, IL) |
| « Prev | Page 1 | Page 2 | Page 3 | Next » |
|
Oh! an unfinished sentence! I wish there were more
|
|
TRWTF is that readers have to finish the code in the original post, making it as WTFy as possible.
The code was eaten by our occasionally dysfunctional richtext editor, then the mistake was missed by our occasionally dysfunctional proofreading department. Fixed! -ed. |
Re: The Long Road to Uppercase
2008-05-14 08:08
•
by
real_aardvark
|
Well, it obviously doesn't use regexps -- that would be too simple. My guess is loops within loops within loops, probably using loop counters named ii, iii, iv, not_i, and paula_bean. An interesting alternative would be to pretend that you're programming in a functional language, and to transform the original string by mapping it to a series of new strings, one character at a time. |
|
I wonder what would happen if you entered some exotic characters like é, ü or å. But then, who needs applicants with French, German or -- God forbid -- Swedish names :)
|
Try writing your name in Ogham, Russian, Greek, Hebrew, Arabic, Japanese, Korean or Chinese. Or any of the other alphabets that any modern OS worth the name can support... |
Re: The Long Road to Uppercase
2008-05-14 08:17
•
by
real_aardvark
|
My vote goes for Klingon. "Yes, perhaps today is a good day to die. Release the Javascript!" ... although my favourite is still: "My program has just dumped Stova Core!" |
It still puzzles me that in this day and age of unicode é is seen as exotic a character as Jack Sparrow.. |
|
"In response to your interview question on how I feel I could contribute to the company I'd like to point out that I could start by updating, sorry, rewriting your online résumé form. I realize that this isn't saying much seeing as a 3 year old child with severe down syndrome could make a better form than this; it is however necessary to start with the basics if you'll have any hope of lifting yourself out of this pile of rancid manure you call code.
If I may offer you some advice, I'd also propose to stop hiring high school kids to build forms. I understand that you can pay them in coke cans, but their "code" tends to suffer." |
|
This comment goes something like this:
The real WTF is |
|
Heey, the post wasn't like this!
Here is the code as I posted it. function validate(field) { var A = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.:!?@#$%¨&*()_+=[]^`Çç{}`^<>\\/|"; var B = " ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.:_____________________________"; var oldval = field.value; var newval = ""; for (var i=0; i<oldval.length; i++) { var char = A.indexOf(oldval.substr(i, 1)); newval = newval + B.substr(char, 1); } field.value = newval; } o.O Sorry for a WTF inside a WTF =P |
Re: The Long Road to Uppercase
2008-05-14 08:30
•
by
real_aardvark
|
Well, you've just sucked all the fun out of today, then, haven't you? |
|
whoever posted the code forgot to escape the <
it's not rocket science people... |
|
I am too humble to call myself a "star programmer", but the code was quite obvious by what was displayed.
Two variables, one is the entire alphabet, the other is a matching alphabet in all UPPERCASE. Grab the user input (field.value), then you start with a 'for' loop. It was quite obvious what was going to happen. |
Or maybe it is and THAT is why half of the code is missing? I think Gustavo is lying about how the code actually looked... the original OBVIOUSLY used a database to store xml data to determine which characters to change. It's just common sense! |
|
This could've led to some brilliant support calls.
"Christopher Konstantopoulos says the site's running very slowly? But I just checked with Bob Jones and he says it's fine!" |
|
Not to mention that if you use a pipe ('|'), it will probably fail as well, seeing that the result string (var B) is 1 character shorter then the input string (var A).
But this code might be valid. I'm not sure if Netscape 2.0 supported fancy functions like 'toUpperCase()' |
|
Not that I'm saying that code is any good, but that method shouldn't be noticeably slow. It's 0(n) so it shouldn't deteriorate too much with larger strings. If you unroll the loop, you get this for "John" as a first name:
var char = A.indexOf(oldval.substr("J", 1)); newval = newval + B.substr(char, 1); char = A.indexOf(oldval.substr("O", 1)); newval = newval + B.substr(char, 1); char = A.indexOf(oldval.substr("H", 1)); newval = newval + B.substr(char, 1); char = A.indexOf(oldval.substr("N", 1)); newval = newval + B.substr(char, 1); That should return in under a millisecond with todays computers... maybe I'm not appreciating how slow computers were when this was discovered. Again, not advocating bad code, just surprised that this was even noticeable. |
Re: The Long Road to Uppercase
2008-05-14 09:04
•
by
Frunobulax
(unregistered)
|
That's easy to say after he already posted the rest of the code ;) |
Re: The Long Road to Uppercase
2008-05-14 09:10
•
by
ais523
(unregistered)
|
No, O(n^2) on many interpreters. The string concatentation can require a string copy each time, which is O(n) in the length of the string. Of course, it would be possible to optimise such a loop, but many interpreters won't do that. BTW: The real WTF is the way that when I click Preview, I get a new Captcha with the same word but written differently, so I can get lots of examples to feed to my OCR. Oh, and does it use words randomly taken from Lorem Ipsum? |
Re: The Long Road to Uppercase
2008-05-14 09:17
•
by
Anonymous
(unregistered)
|
I agree. For one line thats a total of 160 calls to substr, 80 calls to indexOf and 80 string concatenations, which should not be noticeable even in an interpreted language and on old hardware.
It's at least O(n^2). substr may be O(1), but indexOf definitely is O(n), as is string concatenation; and all looped n times. Still, this would require an abysmal implementation of substr and string concatenation to get that slow in a few characters... |
Re: The Long Road to Uppercase
2008-05-14 09:19
•
by
Emphyrio
(unregistered)
|
Also String.indexOf is O(n)... |
Re: The Long Road to Uppercase
2008-05-14 09:20
•
by
hvm
(unregistered)
|
|
No, it's actually O(n^2) because it actually searches for the character each time: A.indexOf(oldval.substr(i,1)). The 'correct' way would have been B=B+A.substr(i,1). I don't know why he didn't do that, it's more obvious IMO. If you want to change é, ă, etc. in simple English characters I think that this method is the simplest way (if you are careful about the complexity)
|
Re: The Long Road to Uppercase
2008-05-14 09:20
•
by
Old Coder
(unregistered)
|
Nope. The backslash is escaped (with another backslash). An additional WTF is that cedillas are included in the trap string but no other characters with accents or other funny non-english wiggly bits. |
|
var myComment = "If there were only an easier way to make my comment all upper case I'd do it."
this.comment.innerHTML = myComment.toUpperCase(); |
Re: The Long Road to Uppercase
2008-05-14 09:27
•
by
rumpelstiltskin
(unregistered)
|
|
What's with all these ancient WTFs?
Everything must be working perfectly now, I guess. |
Re: The Long Road to Uppercase
2008-05-14 09:29
•
by
Jay
(unregistered)
|
Hey, sounds like a clever way to weed out the Swedes from your job applicants without having to admit to discrimination. When they try to submit the resume, just reject it with "Invalid character in name". (Need I explain that I'm of Norwegian descent?) |
Re: The Long Road to Uppercase
2008-05-14 09:29
•
by
Captain Obvious
(unregistered)
|
I agree - far too much of the content on this site is obvious - the stories are far too slow at getting to the point. For example, I remember reading the first Paula Bean story and thinking (halfway through), "oh she's probably not written more than a HelloWorld app that prints "Paula is Brilliant". Make it snappy people! And stop boring us with these *obvious* code snippets. |
Re: The Long Road to Uppercase
2008-05-14 09:32
•
by
ex-Pizza delivery man
(unregistered)
|
oldval.substr(1, 1) = "J" oldval.substr("J", 1) = unpredictable javascript behaviour I agree with you that I would not expect it to be noticably slow on todays machines. |
|
Please wait while we uppercase this comment...
|
You'd get a _ in its place. Or at least, that's what you'd get if your JS interpreter is standards compliant. indexOf would return -1, as it can't find the character, and substr would then return the last character of the string, as per the ECMAScript standard for String.substr(start, length): "If start is negative, it is treated as (sourceLength+start) where sourceLength is the length of the string.". Sourcelength-1 will start you at the last character in the string (since String positions are 0-based in Java/ECMAscript), and you've asked for 1 character - so you'll get the last one in the string. God, I'm such a crushing bore sometimes. My Apologies... |
|
Each string concat creates a new string object to add the previous two into. It does get tend to get slow.
You're better off pushing the letters to an array and joining that together afterwards. |
|
Why insist on uppercasing everything in the first place?
|
|
Offtopic:
I really like these MUMPS programmer Google ads, considering this site's attitude towards MUMPS |
That would've been the correct way if all we'd wanted to do was copy the first x digits of A to B, while wasting resources by doing string contatenation as we go. I'm not even sure what you're trying to acheive with the code you suggest, but it certainly doesn't provide the same functionality as the (rather poor) code in the snippet. Perhaps you meant result += B.charAt(A.IndexOf(value.charAt(i)));That'd work ;^) |
Re: The Long Road to Uppercase
2008-05-14 10:11
•
by
Loren Pechtel
(unregistered)
|
|
Another vote for being puzzled at what's so dreadfully slow about this code? The stated times seem a bit slow for the first machine I ever worked with that I could time: TRS-80 Model 1. A CPU speed three orders of magnitude below current chips and it did a *LOT* less per cycle as well. Interpreted basic--shouldn't be any worse than javascript.
|
|
O(n) vs O(n^2)
I'm pretty sure it's 0(n). Look at the loop unrolling (fixed bug from last unrolling): var char = A.indexOf(oldval.substr(1, 1)); newval = newval + B.substr(char, 1); char = A.indexOf(oldval.substr(2, 1)); newval = newval + B.substr(char, 1); char = A.indexOf(oldval.substr(3, 1)); newval = newval + B.substr(char, 1); char = A.indexOf(oldval.substr(4, 1)); newval = newval + B.substr(char, 1); The indexof is being done on a constant string, not the input string so is O(1). Substr is o(1). It seems to me that this algorithm is a perfect example of O(n). Can one of you O(n^2) advocates go into a little more detail please? Thanks F |
|
Why
a) convert to upper case and b) convert to upper case on the client side? |
Re: The Long Road to Uppercase
2008-05-14 10:14
•
by
akatherder
|
We force all of our data to upper case for some unknown reason. I think it's because we used to have a paper form that people would handwrite their info on. Someone decided it's more accurate to OCR capital letters so we told them to write capital letters. However, the important thing is that we do it server-side and don't bother users with it. |
Re: The Long Road to Uppercase
2008-05-14 10:15
•
by
WhiskeyJack
|
I WAS JUST GOING TO SAY. THAT IS THE REAL WTF, IN MY OPINION. |
Re: The Long Road to Uppercase
2008-05-14 10:18
•
by
anonymous_coder()
(unregistered)
|
We've actually had a VistA programmer come by our LUG meetings - he's trying to adapt the code to private practice. Yeech. Although I'd probably rather use MUMPS than javascript... |
Re: The Long Road to Uppercase
2008-05-14 10:20
•
by
real_aardvark
|
Nå... |
Re: The Long Road to Uppercase
2008-05-14 10:22
•
by
real_aardvark
|
Hey, if you're not registered, we can't track you down and mutilate your cattle. So quityerwhining. Also, you've mis-spelt "brillant." Is nothing sacred? |
Re: The Long Road to Uppercase
2008-05-14 10:26
•
by
Crabs
(unregistered)
|
|
indexOf is O(n), so this is O(n^2).
|
Re: The Long Road to Uppercase
2008-05-14 10:31
•
by
Anonymous
(unregistered)
|
Granted, I missed that. What remains is the string concatenation, which, if strings are implemented as arrays, requires at least occassional copies and is thus O(n). |
Re: The Long Road to Uppercase
2008-05-14 10:40
•
by
real_aardvark
|
Does any of this matter? Ignoring the text boxes at the end, I find it hard to believe that anybody would enter more than ~50 characters for their name (unless they're Mr Gambolputty...etc). O(1), O(n) or O(n^2) ... I think we need to delve more deeply into the problem here. One second? |
|
I've seen this before....I think these are automatically generated validation routines that MS Frontpage puts in.
I saw something similar to this years ago when our "marketing" department (one young kid that discovered web development with frontpage) made some sort of customer contact information gathering page. My initial reaction was WTF too until I realized it was auto-generated by an MS tool....then it really didn't seem much like a WTF anymore since, you know, it was one of those common moronic things MS does from time to time. But, hey, remember they only hire the best and the brightest! Every single applicant gets to answer questions about how to make dwarves wearing different colored hats cross a bridge in the most efficient way possible while smuggling tomatoes.... |
Re: The Long Road to Uppercase
2008-05-14 10:44
•
by
Crabs
(unregistered)
|
|
Just read this one. Technically, indexOf is O(m), m being the length of the character string, so it's really O(m*n). m is consistent in this case, but it would be naive to leave it out of the O notation, as it's the process that takes the longest.
also, I imagine the code looking more like this: for(int i=0; i<field.value.length; i++){ for(int j=0; j<A.length; j++){ if(field.value[i] == A[j]){ newVal = newVal + B[j]; (heh...Bj) } } } To cap it all, I don't know how "+" works with strings/characters in javascript. It could be doing this: temp[] = new char[newVal.length + 1]; for(int i=0; i<newVal.length; i++) temp[i]=newVal[i]; temp[temp.length-1] = B[j]; newVal = temp; this has obvious consequences. |
|
The thing I really enjoyed about this WTF was th
|
Re: The Long Road to Uppercase
2008-05-14 10:54
•
by
A. Cube
(unregistered)
|
JavaScript/ECMAScript IS a functional programming language, with support for first class functions and closures. Due to its C-like syntax and the fact that most people only use it to do a bit of DOM and maybe some asynchronous callbacks, most remain blissfully unaware what the LANGUAGE is capable of (most of the LIBRARIES on the other hand, are hideous trash). |
It just occurred to me that maybe the problem is that every field is being "validated" each time. If that form's a long one (and the JS implementation a piece of rubbish) then that could lead to a proper WTF?! moment... |
| « Prev | Page 1 | Page 2 | Page 3 | Next » |