• (cs)
    Private Sub ImageList_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ImageList.DataBound
        Try
    
        Catch ex As Exception
            If TypeOf ex Is ArgumentOutOfRangeException Then
                Throw New Exception("item not found in the list...")
            End If
        End Try
    End Sub
    

    Did the original code have an empty Try block? Well, that would be safe, because nothing would be executed. Personally, I would build the Catch block as:

    Catch ex as ArgumentOutOfRangeException
        Throw New ApplicationException("Item is not found in the list.", ex)
    Catch ex as Exception
        ' Throw it away, or re-throw and let caller take care of it, or just not catch it in the first place and let the caller take care of it?
    End Try
    
  • the way Alex has a baby dick (unregistered)

    Ran the following code against today's article, glad I put the try catch in here because it could have crashed without it!

    Try MakeFunnyArticle(joke,humor) Catch Ex as Exception Msgbox("UNFUNNY ARTICLE!") End Try

  • Rodrigo (unregistered)
    The Fury:
    The count method I can possibly understand unlike the others. It is possible that it used to do more, i.e. check for some condition on the document before incrementing the count.

    At some point this requirement went away, but the developer didn't bother to do a proper job of refactoring.

    function countDocuments() {
        var count = 0;
    
        for ( var i = 0; i < user.documents.length; i++) {
            if (i % 3 == 0) {
                print "Fizz";
            } else if (i % 5 == 0) {
                print "Buzz";
            } else {
                count++;
            }
        }
    
        return count;
    }

    The business logic was temporarily removed because it was not handling properly the FizzBuzz case

  • (cs)
    String timeStampLength = "                          ";
    int lengthOfTimeStamp = timeStampLength.length();

    This is the code equivalent of "I once caught a TimeStamp this big!"

  • Jay (unregistered) in reply to Evan

    Sure. That's self-documenting code. I mean:

    dateLength="YYYY-MM-DD".length()
    

    A reader knows exactly what the length means. While:

    dateLength=10
    

    Who knows?

    I probably would have written:

    dateLength=10
    // date string is YYYY-MM-DD
    

    but arguably that's inferior, because a later programmer could come along and change the length of the string -- maybe add the time of day or add an AD/BC indicator or whatever -- and change the assignment statement but not change the comment. So now it says

    dateLength=14
    // date string is YYYY-MM-DD
    

    Yeah, that would be dumb, but programmers do it all the time.

    But of course taking the length of a string of spaces tells me less than dateLength=10. Now I have to count the spaces. What did that gain?

  • Jay (unregistered) in reply to Zacrath
    Zacrath:
    RFox:
    Because this method was decommissioned and they weren't quite sure they got all the calls to it yanked out of all the client code...so they made sure any code that still called it would fail dramatically.

    Captcha: plaga - plaga of bad code.

    public DataModel getEditionModel() { if ( true ) throw new IllegalArgumentException( "You shouldn't be here" ); return editionModel; }

    And, of course, using @Deprecated won't work because everyone ignores warnings.

    Or this code was written before the @deprecated tag was added to Java. Or, let's be fair, maybe the programmer isn't familiar with @deprecated.

    RE just delete the function and get the compiler errors: But what if this function is in a library that might be called by many programs? The programmer wouldn't necessarily know all the programs that use this library. So if he just deleted the function, then six months later somebody recompiles and gets this mysterious not-found error, and he has to figure it out. If that's the case, sure, it would be better to put in a comment explaining why the function is obsolete, but I don't think this solution is totally idiotic. Not the best thing to do, but not totally idiotic.

  • Jay (unregistered)
     for ( var i = 0; i < user.documents.length; i++) {
    

    What gets me about this one is that he used the length member right there. I understand if someone writes his own function to do something even though there's a built-in function in the standard library. So okay, he didn't know it was there. Ignorant, but not stupid. But this ...

    Ok, maybe everyone else is saying, Duh, that's why it's on thedailywtf and not on "the daily oh by the way did you know there's a better way to do that".

  • noland (unregistered) in reply to Jay
    Jay:
     for ( var i = 0; i < user.documents.length; i++) {
    
    What gets me about this one is that he used the length member right there. I understand if someone writes his own function to do something even though there's a built-in function in the standard library. So okay, he didn't know it was there. Ignorant, but not stupid. But this ...

    Ok, maybe everyone else is saying, Duh, that's why it's on thedailywtf and not on "the daily oh by the way did you know there's a better way to do that".

    This is an attempt to get the most out of the expensive machine. Please note that with "count = user.documents.length" the length would be evaluated only once. But here, it's evaluated user.documents.length times and this with only a minimal overhead in code. This is a drastic improvement in terms of productivity!

  • qbolec (unregistered)

    Not all langauges have compilers/statick analysis tools. It may be that the class has to meet requirements of some interface, which are actually too restrictive, and the programmer is 99% sure, that a given function will not be called, but still prefers to have a nice stacktrace in case it happen.

    As to the string of spaces, perhaps it is a buffer used to compose the characters of the date one by one, in which case initializing it with spaces makes some sense.

    Using if(true) is either a way to make compiler happy (which in turn indicates existence of some static analysis tools...) or generated by some makro preprocessor which replaces some placeholders with boolean literals, or just a way to make it obvious to the reader that this line is "special".

    Counting using loops is very common among people who just started to learn programming and try to encode what they would do if they were tasked with the same problem the computer is. Some of them are not smart enough to realize that the "for mantra" actually contains subexpression which already is the answer to their problem. Also, there might be people for which corner cases of empty array seem to be "better" handled this way. But, the most probable explanation is that the loop used to contain some more conditions/rules.

  • noland (unregistered) in reply to noland

    *) An attempt to explain the for-loop construct: With AI on the rise, there is less and less reason to treat structures of silicon other than the human work force. So the developer imagined him/herself rather a manager of the silicon work force. This is not simple code, it's the epitome of wise management as lived for decades!

  • Shill (unregistered) in reply to Jay
    Jay:
     for ( var i = 0; i < user.documents.length; i++) {
    

    What gets me about this one is that he used the length member right there. I understand if someone writes his own function to do something even though there's a built-in function in the standard library. So okay, he didn't know it was there. Ignorant, but not stupid. But this ...

    Well, to be fair , he tried this first:

    for ( var i = 0; i < countDocuments(); i++)

    and when he realized that wouldn't work, consulted the documentation and realized he could replace countDocuments() in his code with user.documents.length and Bob's-yer-uncle.

  • (cs) in reply to RFox
    RFox:
    Because this method was decommissioned and they weren't quite sure they got all the calls to it yanked out of all the client code...so they made sure any code that still called it would fail dramatically.

    Captcha: plaga - plaga of bad code.

    public DataModel getEditionModel() { if ( true ) throw new IllegalArgumentException( "You shouldn't be here" ); return editionModel; }

    I disagree. While I think you're on the right track I think it's more about finding use cases that actually trigger the code. If you just want to find the calls remove it and see what fails to compile.

  • (cs) in reply to Walky_one
    Walky_one:
    Note: accessing a constant will always be faster than accessing Array.Length

    And neither will be as slow as using both.

  • (cs) in reply to Bobby Tables
    Bobby Tables:
    Trebla:
    ANON:
    Or deleting it, so that it just wouldn't compile anymore.
    That's no excuse for not deprecating the method and noting that it should be removed in future releases... or for checking if(true).

    Deprecating - perhaps it's possible, perhaps the architecture and/or methodology would make it hard or impossible. Which would imply they have the wrong architecture and/or methodology but hacking this and then patching the resulted faults would be simpler than swapping the entire way how they build whatever system this is.

    As for if (true) - some compilers would complain or actually refuse to compile this method if you just have it as

    public DataModel getEditionModel() {
       throw new IllegalArgumentException( "You shouldn't be here" );
       return editionModel;
    }

    for the return statement is unreachable. Similarly, doing a straight

    public DataModel getEditionModel() {
       throw new IllegalArgumentException( "You shouldn't be here" );
    }

    Would cause the compiler to tell you there is no return statement. The only way to both always throw an exception and have it compile is to have it in a if (true). Now, of course this is not a good way of doing it, but as pointed above may be the best way in the situation which would make it's the...well, correct incorrect way of doing it.

    
    public DataModel getEditionModel() {
       throw new UnsupportedOperationException( "You shouldn't be here" );
       return null; // just in case!
    }
    
  • (cs) in reply to Jay
    Jay:
    Sure. That's self-documenting code. I mean:
    dateLength="YYYY-MM-DD".length()
    

    A reader knows exactly what the length means. While:

    dateLength=10
    

    Who knows?

    I probably would have written:

    dateLength=10
    // date string is YYYY-MM-DD
    

    but arguably that's inferior, because a later programmer could come along and change the length of the string -- maybe add the time of day or add an AD/BC indicator or whatever -- and change the assignment statement but not change the comment. So now it says

    dateLength=14
    // date string is YYYY-MM-DD
    

    Yeah, that would be dumb, but programmers do it all the time.

    But of course taking the length of a string of spaces tells me less than dateLength=10. Now I have to count the spaces. What did that gain?

    int characterLengthOfProperlyFormattedDateString = 10;

  • Ninny (unregistered)

    unction countDocuments() { return
    user.documents.length == 0 ? 0 : user.documents.length == 1 ? 1 : user.documents.length == 2 ? 2 : user.documents.length == 3 ? 3 : user.documents.length == 4 ? 4 : user.documents.length == 5 ? 5 : user.documents.length == 6 ? 6 : user.documents.length == 7 ? 7 : user.documents.length == 8 ? 8 : user.documents.length == 9 ? 9 : user.documents.length == 10 ? 10 : user.documents.length == 11 ? 11 : user.documents.length == 12 ? 12 : user.documents.length == 13 ? 13 : user.documents.length == 14 ? 14 : user.documents.length == 15 ? 15 : user.documents.length == 16 ? 16 : user.documents.length == 17 ? 17 : user.documents.length == 18 ? 18 : user.documents.length == 19 ? 19 : user.documents.length == 20 ? 20 : user.documents.length == 21 ? 21 : user.documents.length == 22 ? 22 : user.documents.length == 23 ? 23 : user.documents.length == 24 ? 24 : user.documents.length == 25 ? 25 : user.documents.length == 26 ? 26 : user.documents.length == 27 ? 27 : user.documents.length == 28 ? 28 : user.documents.length == 29 ? 29 : user.documents.length == 30 ? 30 : user.documents.length == 31 ? 31 : user.documents.length == 32 ? 32 : user.documents.length == 33 ? 33 : user.documents.length == 34 ? 34 : user.documents.length == 35 ? 35 : user.documents.length == 36 ? 36 : user.documents.length == 37 ? 37 : user.documents.length == 38 ? 38 : user.documents.length == 39 ? 39 : user.documents.length == 40 ? 40 : user.documents.length == 41 ? 41 : user.documents.length == 42 ? 42 : user.documents.length == 43 ? 43 : user.documents.length == 44 ? 44 : user.documents.length == 45 ? 45 : user.documents.length == 46 ? 46 : user.documents.length == 47 ? 47 : user.documents.length == 48 ? 48 : user.documents.length == 49 ? 49 : 50; }

  • (cs) in reply to Herr Otto Flick
    Herr Otto Flick:
    Balu:
    The physicist takes is ever present chainsaw and after the tree has fallen he takes a folding meter stick, measures the tree, tells the other two: "45 ft! Have a nice day!" and walks on

    TRWTF

    Oh come on, wake up, stupid. My folding meter stick also has imperial measurements on it in case I bump into American mathematicians arguing over the height of a tree. You don't need it for European ones because they're clever enough to determine tree height without needing a physicist to chop it down for them.

  • (cs) in reply to faoileag
    faoileag:
    dkf:
    faoileag:
    Count Documents.
    Ah yes, the lesser known (and rather less scary) younger brother of Count Dracula.
    Nope, the lesser known (and more prone to bureaucracy) older brother of Count von Count.
    Any relation to Count von Toothree?
  • (cs) in reply to Jay
    Jay:
    Zacrath:
    RFox:
    Because this method was decommissioned and they weren't quite sure they got all the calls to it yanked out of all the client code...so they made sure any code that still called it would fail dramatically.

    Captcha: plaga - plaga of bad code.

    public DataModel getEditionModel() { if ( true ) throw new IllegalArgumentException( "You shouldn't be here" ); return editionModel; }

    And, of course, using @Deprecated won't work because everyone ignores warnings.

    Or this code was written before the @deprecated tag was added to Java. Or, let's be fair, maybe the programmer isn't familiar with @deprecated.

    RE just delete the function and get the compiler errors: But what if this function is in a library that might be called by many programs? The programmer wouldn't necessarily know all the programs that use this library. So if he just deleted the function, then six months later somebody recompiles and gets this mysterious not-found error, and he has to figure it out. If that's the case, sure, it would be better to put in a comment explaining why the function is obsolete, but I don't think this solution is totally idiotic. Not the best thing to do, but not totally idiotic.

    So how is "You shouldn't be here" any more enlightening?

  • (cs) in reply to Matt Westwood
    Matt Westwood:
    Herr Otto Flick:
    Balu:
    The physicist takes is ever present chainsaw and after the tree has fallen he takes a folding meter stick, measures the tree, tells the other two: "45 ft! Have a nice day!" and walks on

    TRWTF

    Oh come on, wake up, stupid. My folding meter stick also has imperial measurements on it in case I bump into American mathematicians arguing over the height of a tree. You don't need it for European ones because they're clever enough to determine tree height without needing a physicist to chop it down for them.

    It's obviously 135 hands!

  • Rick (unregistered) in reply to Bobby Tables
    Bobby Tables:
    Trebla:
    ANON:
    Or deleting it, so that it just wouldn't compile anymore.
    That's no excuse for not deprecating the method and noting that it should be removed in future releases... or for checking if(true).

    Deprecating - perhaps it's possible, perhaps the architecture and/or methodology would make it hard or impossible. Which would imply they have the wrong architecture and/or methodology but hacking this and then patching the resulted faults would be simpler than swapping the entire way how they build whatever system this is.

    I can easily think of some valid reasons, where deprecating isn't possible and returning an exception is the correct implementation. Implementing interfaces or subclassing come to mind. For example, an ImmutableMap that implements the Map interface. The Map interface lets you put() new entries, but since your object is immutable you want to return an exception if anyone accidentally tries that with an instance of your class. Similarly for subclassing - an ImmutableHashMap could subclass HashMap, and just override the put() methods so they return an exception.
  • not an anon (again) (unregistered) in reply to Rick
    Rick:
    Bobby Tables:
    Trebla:
    ANON:
    Or deleting it, so that it just wouldn't compile anymore.
    That's no excuse for not deprecating the method and noting that it should be removed in future releases... or for checking if(true).

    Deprecating - perhaps it's possible, perhaps the architecture and/or methodology would make it hard or impossible. Which would imply they have the wrong architecture and/or methodology but hacking this and then patching the resulted faults would be simpler than swapping the entire way how they build whatever system this is.

    I can easily think of some valid reasons, where deprecating isn't possible and returning an exception is the correct implementation. Implementing interfaces or subclassing come to mind. For example, an ImmutableMap that implements the Map interface. The Map interface lets you put() new entries, but since your object is immutable you want to return an exception if anyone accidentally tries that with an instance of your class. Similarly for subclassing - an ImmutableHashMap could subclass HashMap, and just override the put() methods so they return an exception.
    Doesn't this violate the Liskov Substitution Principle though?
  • (cs) in reply to not an anon (again)
    not an anon (again):
    Rick:
    Bobby Tables:
    Trebla:
    ANON:
    Or deleting it, so that it just wouldn't compile anymore.
    That's no excuse for not deprecating the method and noting that it should be removed in future releases... or for checking if(true).

    Deprecating - perhaps it's possible, perhaps the architecture and/or methodology would make it hard or impossible. Which would imply they have the wrong architecture and/or methodology but hacking this and then patching the resulted faults would be simpler than swapping the entire way how they build whatever system this is.

    I can easily think of some valid reasons, where deprecating isn't possible and returning an exception is the correct implementation. Implementing interfaces or subclassing come to mind. For example, an ImmutableMap that implements the Map interface. The Map interface lets you put() new entries, but since your object is immutable you want to return an exception if anyone accidentally tries that with an instance of your class. Similarly for subclassing - an ImmutableHashMap could subclass HashMap, and just override the put() methods so they return an exception.
    Doesn't this violate the Liskov Substitution Principle though?

    You say that like it was a bad thing.

  • (cs) in reply to not an anon (again)
    not an anon (again):
    Rick:
    For example, an ImmutableMap that implements the Map interface. The Map interface lets you put() new entries, but since your object is immutable you want to return an exception if anyone accidentally tries that with an instance of your class. Similarly for subclassing - an ImmutableHashMap could subclass HashMap, and just override the put() methods so they return an exception.
    Doesn't this violate the Liskov Substitution Principle though?
    Since the exception is a documented part of the interface, it's A-OK with the LSP. Strictly assuming that put() never throws an exception is not warranted by the interface description. (No, just looking at the types doesn't tell you everything about the interface; you've got to read it yourself too to understand the semantics. It's technically possible to encode the semantics in computer-readable form, but You Don't Want That. Computer-readable semantics is usually horrendous!)
  • (cs) in reply to RFox
    RFox:
    Because this method was decommissioned and they weren't quite sure they got all the calls to it yanked out of all the client code...so they made sure any code that still called it would fail dramatically.

    Captcha: plaga - plaga of bad code.

    public DataModel getEditionModel() { if ( true ) throw new IllegalArgumentException( "You shouldn't be here" ); return editionModel; }

    Okay, I'll buy that. There's just one question that remains: Why does it still return editionModel? I mean, do we still need to return that decommissioned value as opposed to, say, null?

  • Irregular programming (unregistered) in reply to Maurizio
    Maurizio:
    function countDocuments() { var count = 0;
    for ( var i = 0; i < user.documents.length; i++) {
        count++;
    }
    
    return count;
    

    }

    Can't you people read ? The name of the method is "Count Documents", it is not "Tell me how many documents are there". So the code do that, counts documents. What else it should do ?

    Captcha: ullamcorper: ancient exotic sexual practice: "last time we did an ullamcorper, i got back pain for a week".

    Why does it return them though?

  • Balu (unregistered) in reply to Matt Westwood
    Matt Westwood:
    faoileag:
    dkf:
    faoileag:
    Count Documents.
    Ah yes, the lesser known (and rather less scary) younger brother of Count Dracula.
    Nope, the lesser known (and more prone to bureaucracy) older brother of Count von Count.
    Any relation to Count von Toothree?
    Yess, sere is.
  • S (unregistered) in reply to Bobby Tables
    Bobby Tables:
    As for if (true) - some compilers would complain or actually refuse to compile this method if you just have it as
    public DataModel getEditionModel() {
       throw new IllegalArgumentException( "You shouldn't be here" );
       return editionModel;
    }

    for the return statement is unreachable.

    You're right on that one... the version "if(true)" will cause a warning from an IDE, but isn't actually a compiler error as the version above would.

    Bobby Tables:
    Similarly, doing a straight
    public DataModel getEditionModel() {
       throw new IllegalArgumentException( "You shouldn't be here" );
    }

    Would cause the compiler to tell you there is no return statement.

    But you're wrong on that one... this is perfectly valid Java code, though normally seen with UnsupportedOperationException instead. A function must return a value only if it doesn't throw an exception...

  • S (unregistered) in reply to Loren Pechtel
    Loren Pechtel:
    If you just want to find the calls remove it and see what fails to compile.

    A good start, but no guarantee. It's a "getter" method, so there's a very good chance it's called via reflection, from some reference to an "editionModel" property in an XML file or something...

  • Evan (unregistered) in reply to Jay
    Jay:
    What gets me about this one is that he used the length member right there. I understand if someone writes his own function to do something even though there's a built-in function in the standard library. So okay, he didn't know it was there. Ignorant, but not stupid. But this ...
    Let me introduce you to TheDailyWTF that, to this day, has caused me to laugh longer and harder than any other: A False Detector.

    That WTF is like an Ogre... there're just WTFs layered on top of WTFs. And one of them is what you say: which is that even if you just look at that code and don't use any language features not used there, there are still much better ways to write it.

  • (cs) in reply to S
    S:
    But you're wrong on that one... this is perfectly valid Java code, though normally seen with UnsupportedOperationException instead. A function must return a value only if it doesn't throw an exception...

    I've never really been fond of the throws keyword.

    public DataModel getEditionModel() throws IllegalArgumentException {
       throw new IllegalArgumentException( "You shouldn't be here" );
    }
    
  • bar (unregistered) in reply to foo

    You introduced a bug there...

  • bar (unregistered) in reply to foo
    foo:
    String timeStampLength = "                          ";
    int lengthOfTimeStamp = timeStampLength.length();

    How confusing! I suggest:

    String sTimeStampLength = "                          ";
    int iLengthOfTimeStamp = timeStampLength.length();
    You introduced a bug there...
  • Concerned Programmer (unregistered) in reply to Hannes
    Hannes:
    I've seen similar code to the one Dennis sent in.

    int newPage = 0; for (int i1 = 0; i1 < (dataGridView1.Rows.Count - 1); i1++) { newPage = i1; }

    Yes. The programmer tried to determine the rows count of the DataGridView. So, to do this, he just wrote a for-loop and used the count of rows in the DataGridView as it's terminator. :(

    I think I know what is going on here.

    Codemonkey programmer only seen use of the array.size / rows.count propperties used inside a for loop, never in a direct assignment. So he/she thinks to be clever and does the for-loop "trick". Wow....

    Complete lack of understanding of the fundamentals I reckon.

    Do you see the pattern, too?

  • (cs) in reply to the way Alex has a baby dick
    the way Alex has a baby dick:
    Ran the following code against today's article, glad I put the try catch in here because it could have crashed without it!
    Try
      MakeFunnyArticle(joke,humor)
    Catch Ex as Exception
      Msgbox("UNFUNNY ARTICLE!")
    Finally
      WriteComment(lame,unfunny)
    End Try
    

    FTFY

  • Ben (unregistered) in reply to ComputerForumUser

    For the last WTF, the joke isn't that they're throwing an exception right after catching one, but that the try block is empty. I assume they either deleted whatever was in the try block, or they wanted to put the try block around every call to the method but couldn't figure out how.

    Side-note: If throwing an exception right after catching one (to change it to a more descriptive message), you should reference the first exception in the constructor of the second, so that its stack-trace information of the first is kept (otherwise when the exception comes up, it only points to where you threw the 2nd exception, and you can't check where the original error came from). Here's an example: http://blog.codinghorror.com/rethrowing-exceptions/

  • Neil (unregistered) in reply to ComputerForumUser
    ComputerForumUser:
    for ( var i = 0; i < user.documents.length; i++) {
    This probably started out as
    foreach (var document in documents) {
    and then somebody said "You should use documents.length instead of the foreach."
    Or maybe someone changed user.documents.constructor.prototype and as a result the new properties showed up in the in loop.
  • (cs)

    Because no one did it: "Ohhhhhh, sweet mystery of life at last I found you!"

  • Jen (unregistered)

    Haha! WTF!

  • smf (unregistered) in reply to ComputerForumUser
    ComputerForumUser:
    This probably started out as
    foreach (var document in documents) {
    and then somebody said "You should use documents.length instead of the foreach."

    Which would be wrong, because when multi-threading those aren't necessarily equivalent.

    In C# length could be a property which returns a different value each time even when single threading.

  • NanoGator (unregistered) in reply to Black Bart

    He also may have anticipated the need to filter out some of the items in that array and left that function as an easy-to-find hook to install the feature when the time comes. Frankly, I've written code like that, and at times it has been rewarding. I think the fact that he used the length command indicates it was intentional.

Leave a comment on “Sweet Mysteries of Life”

Log In or post as a guest

Replying to comment #:

« Return to Article