• Fabian (unregistered)

    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

  • chills42 (unregistered)

    Is that the same as:

    function hotKeyFired(sKeyId, lKey, iModifier, sKeyTxt) {
    try {
    eval(sKeyId);
    }
    catch(e) {
    throw (e)
    }
    }

    That's the only code that seems to need to be there... as far as I can tell...


  • 5of10 (unregistered) in reply to chills42

    Since the try/catch only rethrows the exception it seems to me like it's not needed

  • (cs)

    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.

  • (cs) in reply to Fabian

    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>

     

  • CumpsD (unregistered) in reply to chills42
    Anonymous:
    Is that the same as:

    function hotKeyFired(sKeyId, lKey, iModifier, sKeyTxt) {
    try {
    eval(sKeyId);
    }
    catch(e) {
    throw (e)
    }
    }

    That's the only code that seems to need to be there... as far as I can tell...


    I believe the try catch around eval swallows the exceptions.

    function hotKeyFired(sKeyId, lKey, iModifier, sKeyTxt) {
      var id;
      try {
        switch (sKeyId) {
          default:
            try{
              eval(sKeyId);
            } catch(e) {} <-------------------- swallows them
              break;
        }
      } catch(e) {
        throw e; <----------- probably will never occur :) unless 'switch' can crash :p
      } finally {
        id = null;
      }
    }
    It's something strange :)
  • endothermal (unregistered) in reply to Fabian

    It looks like javascript code, so returns aren't mandatory.  or it's been edited for content :)

  • endothermal (unregistered) in reply to CumpsD

    if skeyID is null the call to switch (sKeyId) will throw a null exception.  So the second catch is "needed"  haha. 

     

     

  • Anonymous (unregistered)

    It seems that somebody was cutting from this function and pasting it somewhere else. So this is not  a WTF, it is left over.

     

  • (cs) in reply to Anonymous
    Anonymous:

    It seems that somebody was cutting from this function and pasting it somewhere else. So this is not  a WTF, it is left over.

    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.

  • (cs) in reply to chills42

    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...

  • (cs) in reply to RyGuy

    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.

  • Fabian (unregistered) in reply to dubwai
    dubwai:
    Anonymous:

    It seems that somebody was cutting from this function and pasting it somewhere else. So this is not  a WTF, it is left over.

    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.



    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
  • chills42 (unregistered) in reply to RyGuy

    I left those in, just because they were passed in, so while not needed, are necessary if this is already in use...

  • Anonymous (unregistered) in reply to dubwai
    dubwai:
    Anonymous:

    It seems that somebody was cutting from this function and pasting it somewhere else. So this is not  a WTF, it is left over.

    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.

    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.

  • Craig Stuntz (unregistered) in reply to 5of10
    Anonymous:
    Since the try/catch only rethrows the exception it seems to me like it's not needed

    No, without it you might see a useful call stack. If you're going to write unmaintainable code, why go half-way?
  • (cs) in reply to Anonymous

    Anonymous:
    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.

    You are an idiot.

  • (cs) in reply to endothermal

    Anonymous:
    It looks like javascript code, so returns aren't mandatory.  or it's been edited for content :)

    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.

  • (cs) in reply to Anonymous
    Anonymous:
    It seems that somebody was cutting from this function and pasting it somewhere else. So this is not  a WTF, it is left over.

    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'.


  • (cs) in reply to Fabian

    Anonymous:


    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

    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.

  • (cs) in reply to loneprogrammer

    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.

  • MrDad (unregistered) in reply to RyGuy
    RyGuy:

    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.


    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.
  • Betty (unregistered)

    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.

    function hotKeyFired(sKeyId, lKey, iModifier, sKeyTxt) {
    var id;
    try{
    eval(sKeyId);
    } catch(e) {}
    id = null;
    }

    is what I reduce it to

  • (cs) in reply to Betty

    Anonymous:

    catch(e) {
      throw;
    }

    fixes this. 

    I would think eliminating the catch entirely would fix it too.

  • (cs) in reply to dubwai
    dubwai:

    Anonymous:

    catch(e) {
      throw;
    }

    fixes this. 

    I would think eliminating the catch entirely would fix it too.



    ...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 ;-)
  • (cs) in reply to chills42
    Anonymous:
    Is that the same as:
    function hotKeyFired(sKeyId, lKey, iModifier, sKeyTxt) {
    try {
    eval(sKeyId);
    }
    catch(e) {
    throw (e)
    }
    }

    That's the only code that seems to need to be there... as far as I can tell...

    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); }
  • (cs) in reply to Stan Rogers
    Stan Rogers:
    dubwai:

    Anonymous:

    catch(e) {
      throw;
    }

    fixes this. 

    I would think eliminating the catch entirely would fix it too.



    ...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 ;-)

    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.

  • (cs) in reply to dubwai

    I was thinking of the catch(e) {} "correction". Still, you're right. Must stop looking before it becomes viral.....

  • Anonymous Coward (unregistered) in reply to MrDad
    Anonymous:
    RyGuy:

    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.


    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.

    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.

  • (cs) in reply to Anonymous Coward
    Anonymous:
    I sometimes have to maintain somebody else's code which uses that horrid style, and I wrote a program to FIX that monstrosity automatically.

    You wrote your own?  That was redundant.

  • Code Eye (unregistered) in reply to Anonymous Coward

    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 =).

  • cm (unregistered) in reply to stick109

    Don't overlook the unused parameters: lKey, iModifier, sKeyTxt
    function
    hotKeyFired(sKeyId) { eval(sKeyId); }
    The question "why not replace all calls to hotKeyFired to eval()" is left as an exercise for the reader. I guess this guy gets paid by the kLoc?

  • Cyber (unregistered) in reply to Code Eye

    Well put :)

  • anon (unregistered) in reply to cm

    'The question "why not replace all calls to hotKeyFired to eval()" is left as an exercise for the reader.  I guess this guy gets paid by the kLoc?'

    Replacing kotKeyFired with eval would be confusing interface and implementation. Not that the original coder would really understand that.

    My question is: in what fscked up system does a function called "hotKeyFired" just eval its first argument?

  • Betty (unregistered) in reply to cm

    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?

  • Anonymous Coward (unregistered) in reply to loneprogrammer

    loneprogrammer:
    Anonymous:
    I sometimes have to maintain somebody else's code which uses that horrid style, and I wrote a program to FIX that monstrosity automatically.

    You wrote your own?  That was redundant.

    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]

  • Betty (unregistered) in reply to Anonymous Coward

    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.

  • (cs) in reply to Betty

    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.

  • (cs) in reply to endothermal
    Anonymous:

    if skeyID is null the call to switch (sKeyId) will throw a null exception.  So the second catch is "needed"  haha.


    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:

  • (cs) in reply to chills42
    Anonymous:
    I left those in, just because they were passed in, so while not needed, are necessary if this is already in use...


    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).
  • (cs) in reply to JamesCurran
    JamesCurran:

    Actually, in Javascript, there are no data types.

    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.

    JamesCurran:

    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.



    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.
  • Krenn (unregistered) in reply to loneprogrammer
    loneprogrammer:
    Except this would screw up the "cvs diff" command something fierce.

    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.
  • (cs) in reply to Krenn

    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?

  • (cs) in reply to chills42

    Anonymous:
    I left those in, just because they were passed in, so while not needed, are necessary if this is already in use...

     

    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

  • (cs) in reply to DrCode
    DrCode:
    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:


    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.
  • Mirandir (unregistered) in reply to Drak
    Drak:

    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

    They aren't ignored. They're placed in the functions arguments array.

    /Mirandir

  • (cs) in reply to Mirandir

    Ah, so that you can access them if you made your function with var a= new Function("bla; bla;")

    ?

    Drak

  • (cs) in reply to Drak

    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.

  • CumpsD (unregistered) in reply to loneprogrammer

    loneprogrammer:
    Anonymous:
    I sometimes have to maintain somebody else's code which uses that horrid style, and I wrote a program to FIX that monstrosity automatically.

    You wrote your own?  That was redundant.

    also in VS.NET:

    CTRL-A (:p)

    CTRL-K

    CTRL-F

    (If you have default bindings)

  • jackass (unregistered) in reply to dubwai

    Well at least that was a complete sentence.  Congrats

Leave a comment on “Write Only Coding”

Log In or post as a guest

Replying to comment #:

« Return to Article