• Miriam (unregistered)

    Smaller-than and greater-than operators? Who needs those?!

    TRWTF is only half-working syntax highlighting.

  • cyborg (unregistered) in reply to Miriam
    Miriam:
    Smaller-than and greater-than operators? Who needs those?!

    Or nots. I assume this is Javascript - I wonder what would happen to the rest of the code if I used a five digit year?

  • (disco)

    Sometimes, when we’re validating input, we like to provide feedback to the users that are doing it wrong by doing things with colors, like changing the background color.

    FTFA

  • (disco)

    I wonder how an exception called CodeUnreachableException is supposed to be used...

  • (disco) in reply to VinDuv
    VinDuv:
    I wonder how an exception called ```CodeUnreachableException``` is supposed to be used...
    The obvious answer is "not like that". Unless there is strong validation somewhere else, that code will be reachable if someone uses "123" as a key.

    I'd normally describe throwing CodeUnreachableException as a form of assertion failure.

    Discourse Bug: Here I am, looking at the vertical-position misindicator and the text above the reply box. I am replying to post 5, you know, with the vertical position misindicated as 3 of 3.

    Discourse Bug: What's with one star around a chunk of text being italic? Who the fuck thought that was a good idea? Can I have him shot?

  • (disco) in reply to VinDuv
    VinDuv:
    I wonder how an exception called ```CodeUnreachableException``` is supposed to be used...
    function php_real_all_equal($a, $b, $c)
    {
        if ($a == $b && $b == $c)
        {
            if ($a != $c)
            {
                // Sanity check. This can never happen.
                throw new CodeUnreachableException('Boolean logic is broken =(');
            }
    
            return true;
        }
    
        return false;
    }
    
    $equal = php_real_all_equal('0', 0, '');
    
  • (disco) in reply to Keith

    You're making my head hurt. Or maybe that's just the PHP talking. I've seen placement students do much, much worse though.

  • (disco) in reply to Keith
    Keith:
    function php_real_all_equal($a, $b, $c)
        {
            if ($a == $b && $b == $c)
            {
                if ($a != $c)
                {
                    // Sanity check. This can never happen.
                    throw new CodeUnreachableException('You forgot to use === and some asshole will find a way to break it!');
                }
    
                return true;
            }
    
            return false;
        }
    
        $equal = php_real_all_equal('0', 0, '');
    

    Of course, you could use type hinting since version 5.<CBA to google it> in which case it should be fine.

    Then again... Unicode...


    Filed under: Look ma, I broke Discurse again!

  • (disco) in reply to Onyx

    What's the world coming to when a guy can't craft a contrived example of PHP's lack of transitivity in its non-typesafe equality operator without facing the Spanish inquisition?

  • (disco) in reply to Keith
    Keith:
    What's the world coming to when a guy can't craft a contrived example of PHP's lack of transitivity in its non-typesafe equality operator without facing the Spanish inquisition?

    This is TDWTF. Welcome to die.

  • Paul Neumann (unregistered)

    Simple:

    // because the greater/smaller signs are being escaped
    //if(this.value.length<>4)
    if(this.value.length!=4)
    {
        alert('The year must have 4 digits.');
        this.focus();
    }
  • (disco) in reply to Onyx
    php_real_all_equal shaqo ($ a, b $, $ c) 
         {
             haddii ($ a && $ b $ c == == $ b) 
             {
                 haddii ($ a! = $ c) 
                 {
                     / / Jeeg suulo. Tani marnaba dhici karin. 
                     tuurin CodeUnreachableException cusub ('Waxaad illowday in ay isticmaalaan === iyo Daawa qaar ka mid ah ka heli doonaan jid ay si u burburin!'); 
                 } 
    
                 soo celi run ah; 
             } 
    
             soo celi been ah; 
         } 
    
         $ siman = php_real_all_equal ('0 ', 0,'');
    

    Filed under: oops wrong thread

  • (disco) in reply to sam

    That gives me an idea: We could translate php to lojban!

    Nah, actually, just do the British version: http://www.addedbytes.com/blog/if-php-were-british/

    Hey! Translate Go to lojban. Maybe @ben_lubar could start the project.

  • (disco)
    function checkComment( String comment ) {
     try {
        int i = 1/"frist".compareTo( comment );
     } catch ( ArithmeticException e ) {
        return;
     }
     throw new CodeUnreachableException( "Comments may not solely consist of the word 'frist': " + comment );
    }
  • (disco)

    What do you suppose CheckKey() would do with 0xAC?

    And actually that's an interesting consideration for programming language specific syntax, since different languages do non-decimal numbers differently. See http://en.wikipedia.org/wiki/Hexadecimal

    dave (who used to tell people his age in Hex, until earlier this year when he turned 3A...)

  • (disco) in reply to deemery
    deemery:
    dave (who used to tell people his age in Hex, until earlier this year when he turned 3A...)

    Did the same until 1A hit. Sigh.

  • (disco) in reply to Onyx
    Onyx:
    Did the same until 1A hit. Sigh.
    I gave up all hope on that when I was 0x15 and an American cinema ticket-seller asked me if I was old enough to be allowed into an R-rated film on my own. Needless to say I wasn't pleased, but I restrained my urge to make a big scene because I figured if I did they wouldn't let me into the film, or, indeed, into any other film they might show in the future.

    No, I don't remember which film it was, but I do remember the cinema - the one that (in 1987!) was in Shopper's World in Framingham, Mass. (The alert among you will note that I remember where it was but not which chain it was...)

    EDIT: It was Shoppers' World, not Shopper's World, although the latter is on the site formerly occupied by the former.

  • (disco) in reply to Onyx

    I can work it out just fine. On my last birthday I turned 40. Now ten more years till 4A. Maybe I can say I turned '@'.

  • (disco) in reply to herby
    Steve_The_Cynic:
    I gave up all hope on that when I was 0x15 and an American cinema ticket-seller asked me if I was old enough to be allowed into an R-rated film on my own.

    Used to get that all the time while trying to buy alcohol or cigarettes. One time even as I clearly just walked out of the car, from the driver's seat, with keys in hand (Note: Legal age for everything is 18 here). These days I have a goatee, but if I shaved I'm pretty sure they'd ask me again.

    herby:
    I can work it out just fine. On my last birthday I turned 40. Now ten more years till 4A. Maybe I can say I turned '@'.

    It's not the working out bit, it's messing with people by saying a seemingly lower number. But the letter mess it up, so they either figure it out immediately, or, more frequently, look at you with a blank stare.

  • (disco)

    Not that age could reach the number, but say something is 9F years old, it would be 'ninety eff'. What do you call it next year, without confusing it with a number half as much?

  • (disco) in reply to Onyx

    I used to work in a store that sold alcohol and I had someone claim that they'd just turned up to the store in their car, but had no car keys nor any form of driving licence (if they had done so, they would at least have the credit-card sized driving licence in Europe which they should have had on them)

  • hanke (unregistered)

    I am sad that fewer people are posting here and everyone must be discoursing.

  • (disco) in reply to Steve_The_Cynic
    Steve_The_Cynic:
    I gave up all hope on that when I was 0x15 and an American cinema ticket-seller asked me if I was old enough to be allowed into an R-rated film on my own. Needless to say I wasn't pleased, but I restrained my urge to make a big scene because I figured if I did they wouldn't let me into the film, or, indeed, into any other film they might show in the future.

    No, I don't remember which film it was, but I do remember the cinema - the one that (in 1987!) was in Shopper's World in Framingham, Mass. (The alert among you will note that I remember where it was but not which chain it was...)

    EDIT: It was Shoppers' World, not Shopper's World, although the latter is on the site formerly occupied by the former.

    I've been to Framingham many times, don't think it's ever been that boring.

  • (disco) in reply to Arantor
    Arantor:
    I used to work in a store that sold alcohol and I had someone claim that they'd just turned up to the store in their car, but had no car keys nor any form of driving licence (if they had done so, they would at least have the credit-card sized driving licence in Europe which they should have had on them)

    I had the licence with me. And other forms of ID. The point is I stopped and exited my car in plain sight of the seller (it was a news stand on a parking lot basically).

    That, and I bought stuff there almost daily.

  • (disco) in reply to Onyx
    Onyx:
    http://www.addedbytes.com/blog/if-php-were-british/
    Thanks for the link - made my day!
  • (disco) in reply to Onyx

    Yeah, that's not smart, unless they didn't see you or know you...

  • (disco) in reply to Onyx

    I had a friend who got carded well after 0X28. One day a bouncer said, "If you're 40, I'm buying!". He flipped his government (Civil Service) ID on the counter, and started ordering top shelf stuff.

    And I remember Shoppers World when it had that kinda modernist/flying saucer look to it.

    dave

  • (disco) in reply to Keith

    No-one expects the Spanish inquisition. Or something along those lines ;)

  • (disco) in reply to SG_01

    Suck it, @blakeyrat.

  • (disco) in reply to herby
    herby:
    I can work it out just fine. On my last birthday I turned 40. Now ten more years till 4A. Maybe I can say I turned '@'.

    When people ask my age I tell them I recently celebrated the 22nd anniversary of my 21st birthday.

  • (disco) in reply to redwizard

    21 with lots of experience?

  • (disco) in reply to DrakeSmith
    DrakeSmith:
    Not that age could reach the number, but say something is 9F years old, it would be 'ninety eff'. What do you call it next year, without confusing it with a number half as much?

    AAAAAA oh. AAA Aaa aaa oh. ;-)

  • (disco) in reply to redwizard
    redwizard:
    22nd anniversary of my 21st birthday

    Hmm, 34th anniversary of my 21st birthday, 30th anniversary of my 25th birthday, 25th anniversary of my 30th birthday. No, none of them sound good.

    Filed under: I'm old.

  • (disco) in reply to HardwareGeek
    HardwareGeek:
    Hmm, 34th anniversary of my 21st birthday, 30th anniversary of my 25th birthday, 25th anniversary of my 30th birthday. No, none of them sound good.

    0x16th anniversary of your 0x21st birthday.

    FTFY

  • (disco)

    That if statement in the last piece of code made my head implode. In future, no TDWTF before coffee in the morning.

  • (disco) in reply to Onyx
    Onyx:
    Nah, actually, just do the British version: http://www.addedbytes.com/blog/if-php-were-british/

    This would bring me back to PHP after all these years. Quick, someone create an Imperial C++ Preprocessor to counteract!

  • (disco)

    "The Long Now Foundation doesn’t appreciate his date conventions."

    I don't think so. The code does not show an alert for year 02014. Only people who lived in early mediaeval era might object to it.

  • the beholder (unregistered) in reply to Paul Neumann
    Paul Neumann:
    Simple:
    // because the greater/smaller signs are being escaped
    //if(this.value.length<>4)
    if(this.value.length!=4)
    {
        alert('The year must have 4 digits.');
        this.focus();
    }
    Don't forget to check that every digit must be purely numeric, too.

    You could use the third sample in today's article.

  • (disco)

    I was curious about what exactly the condition was here:

    if (!(!(i == detailsList.length() - 1) || detailsList.length() < 3))
         textViewLineSeperater.setBackgroundColor(Color.TRANSPARENT);
    

    If ! takes precedence over - which takes precedence over == and < which take precedence over || (these rules are used in JS, not sure about what language this was) -- then this simplifies to:

    !(i != d.length() - 1 || d.length() < 3)
    

    And with de Morgan's law:

    i == d.length() - 1 && d.length() >= 3
    

    So its testing if an index has looped all the way through an array which is 3 or more elements. Basically "if it's the last one and the list is long enough, clear the background color." Is that what was intended? I have no idea.

  • (cs) in reply to hanke
    hanke:
    I am sad that fewer people are posting here and everyone must be discoursing.
    People are so distressed by discurse that the number of commenters went down a lot.

    Check for example April May June (which in this case is not the name of a person!).

    More than 50% of the articles had about 100 comments or more. In June all the articles plagued with disaster had only half of that, most of them only 30 comments or less.

    In July the disdain caused readers to flee the site.

    Did nobody tell Jeff Atwood that dis has negative connotations?

    Which, in the case of discourse, means it's aptly named.

  • (disco) in reply to chubertdev
    chubertdev:
    I've been to Framingham many times, don't think it's ever been that boring.
    You overlooked the point that I forgot the name of the cinema because it was nearly 30 years ago... And the Shoppers' / Shopper's World thing is genuine. They demolished the s-apostrophe version in 1994 and replaced it on the same site with the apostrophe-s version.
  • (disco) in reply to deemery
    deemery:
    And I remember Shoppers World when it had that kinda modernist/flying saucer look to it.
    Yeah, I remember that version, too, with the shops on the inside and the parking on the outside. The last time I was there was in about 1989, five years or so before they demolished that version to replace it with a different version... It used to be the World of Shoppers, now it's a World for a Shopper. Not sure that's an improvement.
  • (disco) in reply to BernieTheBernie
    BernieTheBernie:
    > "The Long Now Foundation doesn’t appreciate his date conventions."

    I don't think so. The code does not show an alert for year 02014. Only people who lived in early mediaeval era might object to it.

    02014, being 1036 in decimal, would correspond not to the early mediaeval era (which begins not long after 450AD) but the middle part, commonly the "High Middle Ages", which began around 1000AD.
  • anonymous (unregistered)
    Validation-through-exceptions is one of those bizarrely prevalent anti-patterns. Kids: exceptions should be used to model *exceptional* situations, things that *shouldn't* ever happen. "My user was an idiot and entered bad input," isn't an exceptional event. That's *normal*.

    No. That's stupid. "Things that shouldn't ever happen" are fatal errors: things that result in your code blowing up and failing miserably. There is nothing wrong with taking exception to bad data that a user has input. That is what exceptions are designed for: things that shouldn't happen if everything is done correctly, but which can be caught and recovered from without causing the user any undue stress.

    TRWTF would be if your "my user is an idiot and entered bad input" exception causes your program to crash and burn (or display some obscure error message that doesn't tell the user what he did wrong).

  • (disco) in reply to Steve_The_Cynic
    Steve_The_Cynic:
    You overlooked the point that I forgot the name of the cinema because it was nearly 30 years ago... And the Shoppers' / Shopper's World thing is genuine. They demolished the s-apostrophe version in 1994 and replaced it on the same site with the apostrophe-s version.

    True, although I don't think Framingham has changed much in 30 years. I tended to spend more time next door in Natick.

  • (disco)

    Re that numeric-format-validation code -- to be perfectly honest, that's almost exactly how I'd do it. Every time I've tried to "roll my own," I've ended up in a hideous tangle of trying to anticipate all the possible ways of writing a "number" -- "d" or "d.dd" or "0.dd" or ".dd" or "{any of those}{e or E}dd" or who knows what-all else. Why go through all that head-and-heartache when the OS or RTL writers already did all that, years ago? The only tricks then are to find the right function, and find out how to distinguish success from failure. Since most of the world seems still to derive from C/Unix conventions established in the 1960s, it's not uncommon for a string-to-number conversion function to return 0 as its error value; I guess you're supposed to be able to just know that the string you passed "wasn't zero, at least," even if you don't know what else it might have been. Clearly, the only reliable way for the conversion function to return "failure" is by taking action that can't possibly be misinterpreted as a valid number. One way would be to make the function take a second argument, by reference (hence writable-for-return), that acted as a flag for whatever error conditions were discovered in the bowels of the conversion algorithm -- but when has there ever existed a string-to-integer conversion function with such a forethoughtful signature? None that I've ever seen! The best behavior I have seen is -- you guessed it -- for the conversion function to throw an exception when conversion fails. Then the code that calls the conversion function can handle the exception and take appropriate action (such as coloring the input field, at the cost only of calling the conversion function every time the field content changes, which used to be considered expensive back in the good ol' days) based on clearly unambiguous knowledge of whether the string was, or was not, "a number."

    As to the nature of the exception, I can even understand using "UnreachableException." You would want something that couldn't easily be confused with any other exception, and maybe you don't know about (or there aren't, in your framework/platform of enforced choice) numeric-conversion-specific exceptions. I personally have had a HELL of a time getting OS/RTL-provided exceptions to work at all; rarely is there any documentation of what header file has to be included, or how to throw or catch them, or what arguments their constructors take, or what information can be extracted from them once caught, etc. etc. etc. About the only thing you CAN do is throw something as bland and generic, but unique, as you possibly can, and do your best to recognize it when it comes.

    In short, I see absolutely nothing wrong with the conversion function that's being ridiculed here.

Leave a comment on “Everyone Wants Validation”

Log In or post as a guest

Replying to comment #:

« Return to Article