- 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
Help! I can't feel my legs!
I've actually never seen a switch-statement with just a default case before.
Also, aren't functions supposed to have a return value? What does this function return?
Fabian
Admin
Is that the same as:
Admin
Since the try/catch only rethrows the exception it seems to me like it's not needed
Admin
Wow. Just...wow.
From the unused local variable id to the second, unreachable catch block to the completely superfluous switch statement...this is a masterpiece of self-obfuscating code. I am dumber for having read it.
Admin
I'm assuming this is JavaScript, which doesn't complain if your function returns nothing.
Basically, you've got yourself 16 lines of code that do exactly the same thing as
<FONT face="Courier New">eval(sKeyId);</FONT>
Admin
I believe the try catch around eval swallows the exceptions.
Admin
It looks like javascript code, so returns aren't mandatory. or it's been edited for content :)
Admin
if skeyID is null the call to switch (sKeyId) will throw a null exception. So the second catch is "needed" haha.
Admin
It seems that somebody was cutting from this function and pasting it somewhere else. So this is not a WTF, it is left over.
Admin
I disagree. How this WTF came to be doesn't make it less of a WTF. Copying and pasting code without understanding it or cleaning it up for the purpose at hand is not only a WTF in itself, it's extremely irresponsible. This is what 'programmers' who have don't really know how to program write code. They plagarize and tweak the code until it seems to work for a single test case.
Admin
That's the only code that seems to need to be there... as far as I can tell...
I think you can get rid of the last 3 arguments that were passed in as well...
Admin
I do not mean to de-rail this thread, but I have a lot more trouble reading code with curly braces like those above compared to those that are on their own separate lines.
Admin
I am with you all the way... Copying&Pasteing is lazy (which I generally consider to be a positive quality), but the resulting code is YOUR responsibility.
Fabian
Admin
I left those in, just because they were passed in, so while not needed, are necessary if this is already in use...
Admin
Did you read (clean) what you wrote in this post? Your sentence: "This is what 'programmers' who have don't really know how to program write code." is a bigger WTF than the code.
Admin
No, without it you might see a useful call stack. If you're going to write unmaintainable code, why go half-way?
Admin
You are an idiot.
Admin
Actually, in Javascript, there are no data types. Therefore, you cannot state the return type of a function. Hence you cannot state that the return type is void. However, the only way to declare a function is with the "function" keyword (there's no Sub).
In other words, there is no way to say "This is not really a function, but a subroutine which does not return anything".
So, on that one issue, the WTF is with Javascript, not the author.
Admin
Let's take a look at MJD's file of good advice:
#11900 You cannot just paste code with no understanding of what is going on and expect it to work.
#11924 Well, if you don't know what it does, why did you put it in your program?
#11952 In my experience that is a bad strategy, because the people who ask such questions are the ones who paste the answer into their program without understanding it and then complain that it `does not work'.
Admin
Sure. There is nothing wrong with copying some code in order to save time. But just leaving a bunch of bogus crap in the new code shows that the developer doesn't really understand what is going on. I have a co-worker who does this. It's this kind of mentality that leads people to replace a fuse with a rifle cartridge.
Admin
That's astonishing. Really, truly astonishing. I don't think I've ever seen anything that displayed so many fundamental problems per square foot. I suppose one could make an excuse for the signature -- if there were any signs that the "programmer" had any basic understanding of the code. There are none. Nor, I'm sorry to say, can we let the plagiarized coder off the hook. eval()??? Even in a JS sandbox, one would better have a damned good excuse for executing an input.
Admin
I actually find it harder to read with half the lines in the code taken up just to start a block.:S
The end brace is lined up with the opening statement which opens it, so I find it pretty easy to follow this way. Fewer lines of code is fewer lines to debug.
Admin
I'm certainly no javascript programmer (and hope I never am) but am I wrong in saying that you should never use
catch(e) {
throw e;
}
as it doesn't keep the exception stack or something so you can't see where the original exception was thrown? AFAIK
catch(e) {
throw;
}
fixes this. Not that an exception is going to reach that piece of code
Also do not forget the code in the 'finally' as it will be run.
Admin
I would think eliminating the catch entirely would fix it too.
Admin
...except that it would reintroduce the error message that the try-catch was meant to fix. I mean, why fix code when you can merely make the evidence of brokeness go away? The boss'll never know ;-)
Admin
I respectfully disagree. The function can be optimized even more. catch(e){throw(e)} is effectively the same as not having catch at all, so the resulting code is
function hotKeyFired(sKeyId, lKey, iModifier, sKeyTxt) { eval(sKeyId); }
Admin
As someone pointed out before, catch e throw e basically just truncates the stacktrace and would it this case were is reachable code. The other stupid try-catch is the one that is squashing the exception. I think this code is too crappy to think about any longer. It's making my brain hurt.
Admin
I was thinking of the catch(e) {} "correction". Still, you're right. Must stop looking before it becomes viral.....
Admin
Not if you spend your time tracking down the frickin' indentation and where the heck the initial brace was. The R&K brace style is an abomination and its proponents need a good flogging at the hands of the Allman style. I sometimes have to maintain somebody else's code which uses that horrid style, and I wrote a program to FIX that monstrosity automatically. That's the first thing I do.
Admin
You wrote your own? That was redundant.
Admin
I guess if you like looking for braces instead of the constructs of the code they purport to enclose, you could consider K&R an abomination =).
Admin
Admin
Well put :)
Admin
Admin
Here's a question. Do people actually get paid by kLoc? I hear you guys mention it alot but the idea is so rediculous that I can't see it happening. I mean what if this guy came to his senses and changed this 10 line function to a 1 liner, would he then owe the company money for negative lines?
Admin
It took me less time to write that script than to figure out how to use GNU Indent, thanks to my m4d sk|lz with regexps. Plus GNU Indent works for C, while most of my current work is PHP.
Back to topic: I think the biggest WTF in that horrible Javascript is the brace style. [:D]
Admin
Not to get into a bracing style argument but I dont see how a brace is so much easier to line up visually with another brace then a letter. I really like the bracing style, and use it on code i write (unless coding in a group or project that has already defined another style). Most good editors can easily flick between the different styles if needed.
Admin
A long time ago, I heard a proposal -- Instead of bickering over what kind of brace style and how much whitespace to use, why not make your source code control system fix it for you? Just set up your personal options for the "indent" program (or similar for other languages) and check in any old thing you want. No matter what other people are doing, all the code comes out looking just the way you like it.
Except this would screw up the "cvs diff" command something fierce. I guess we have to keep bickering.
Personally I can read either style. What is really awful is code that has everything all over the place, so nothing lines up, anywhere. Euugh.
Admin
Actually, switch (null) { ... } is perfectly legal in javascript. So that try/catch block is not needed. Also, I think that people haven't considered the apparent meaning of this function. It's called hotKeyFired and the parameter it eval's is sKeyId which is almost certainly a number. So the eval will be equivalent to something like eval(65), which will coerce its argument into a string, parse it, and return the value of the expression ... which will be the original number. Which then is not assigned anywhere.
So the function is equivalent to the following code:
Admin
Not in the least. You can call a javascript function with more arguments than are in its formal argument list (they are then available to it through the arguments array), or with less (the rest of the arguments then default to undefined).
Admin
Sheer nonsense. Javascript has types such as number, string, object, and function. Just ask the 'typeof' operator. Javascript simply takes the same attitude to types as Lisp: variables don't have types, values do.
You don't need to declare the return type of a function. (BTW, another way to define a function is with the Function ctor, but that isn't recommeded, as it causes a performance hit.)
If you use a function that doesn't return anything in an rvalue context (e.g., x = hotKeyFired(42)), the value is undefined. Admittedly, that isn't particulary useful.
Admin
I don't see why that would be the case - you just have the diff calculated against the "server-ized" code, and then expand the snippets back out into the format the viewer prefers. Sure, it may start telling you that lines with nothing but brackets on them have been changed, but as long as people know what that means, it's no big deal.
Admin
I love it when somebody plays baseball with an exception, but he doesn't only do that. He puts a switch for no cases, an explicit try catch within another try catch, and a rethrow that comes from nowhere and probably goes nowhere either.
What the Fucking Fuck?
Admin
Actually, in Javascript, they are not necessary.
function bla(x)
{}
bla(1,2,3,4,5,6,7,8,9,0);
works just fine. I guess it just ignores the superfluous arguments to the function call.
Drak
Admin
pree-cisely.
I'm also amused he this cut&paster defined the local var id in the first line, which gets evaluated to Undefined by the JS compiler, then finally assigning Null to that var.
*beats forehead with fists*
This function does absolutely nothing at all, save for the possibility of NOT throwing an error when used with an illegal value of sKeyId.
Admin
They aren't ignored. They're placed in the functions arguments array.
/Mirandir
Admin
Ah, so that you can access them if you made your function with var a= new Function("bla; bla;")
?
Drak
Admin
No.
Within every function, there is the aruments array that contains the arguments. You can access it at any time.
Unless you create a variable called arguments inside the function.
Admin
also in VS.NET:
CTRL-A (:p)
CTRL-K
CTRL-F
(If you have default bindings)
Admin
Well at least that was a complete sentence. Congrats