The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Validating a number

Last post 12-01-2012 6:00 AM by pjt33. 25 replies.
Page 1 of 1 (26 items)
Sort Posts: Previous Next
  • 11-29-2012 12:26 PM

    Validating a number

    Someone was asked to do validation on a numeric field supplied to us by another system.

    It turns out that they asked to create a db table with all the valid values. Since these were the sorts of things that would never change in our industry, they were told to just hard code the values. There were way too many for an if-else chain or a switch statement, so this individual came up with this:

     

        public enum ValidNumbers {
    _0001,
    _0002,
    ..., // OP: assorted gaps in the range
    _0999,
    _1000;

    public static boolean priceIsValid(int n) {
    try {
    ValidNumbers vn = valueOf("_"+new DecimalFormat("0000").format(n));
    return true;
    } catch (Exception e) {
    return false;
    }
    }
    }

    Maybe someday someone could design an object where you could hash into a map...

    Be brief, no matter how long it takes.
  • 11-29-2012 12:29 PM In reply to

    Re: Validating a number

    snoofle:
    priceIsValid

    I remember that show.

    Signatures are stupid.
  • 11-29-2012 12:46 PM In reply to

    Re: Validating a number

    Am I wrong in thinking that this isn't actually that bad? If the numbers mean something, then they're not numbers, they're just unlabelled enumeration values, and this is a perfectly reasonable way to map between them.

    My only nitpick is using an Exception to handle a non-exceptional situation, but that's Java's fault for not having something like "tryParseValue"

  • 11-29-2012 12:57 PM In reply to

    Re: Validating a number

    pkmnfrk:

    Am I wrong in thinking that this isn't actually that bad? If the numbers mean something, then they're not numbers, they're just unlabelled enumeration values, and this is a perfectly reasonable way to map between them.

    My only nitpick is using an Exception to handle a non-exceptional situation, but that's Java's fault for not having something like "tryParseValue"

    No, it's bad.  Just use Hashset<int> (or equivalent)

    if(validNumbers.Contains(myInt))...

    On a side note, I JUST came upon this code in an internal library I'm removing from our codebase:

        public static Decimal GetDecimal(object value, Decimal defaultValue = 0M)

    {

    Decimal result;

    if (value == null || !Decimal.TryParse(value.ToString(), out result))

    return defaultValue;

    else

    return result;

    }

    What do we pass in? A double.

  • 11-29-2012 1:05 PM In reply to

    Re: Validating a number

    At a high level:  If the field is truly numeric, then the leading zeros can be dropped.  On the other hand, if the leading zeros are important, then these things are just strings.  Yes?  Either way, that random-ass underscore is a huge pain. 

    catching(Exception) to handle normal control flow is like a big piece of WTF carrot-cake,

    but the other small things (like incurring the costs of appending the underscore and creating a DecimalFormat each time) are like the cream-cheese icing.  

     


  • 11-29-2012 1:40 PM In reply to

    Re: Validating a number

    Create a bit mask that contains all valid numbers OR'd together. Goes something like 0001 | 0002 ...

    then it's just return (n & VALID_NUMBERS) == n

  • 11-29-2012 1:44 PM In reply to

    Re: Validating a number

    Maybe if the only valid numbers were powers of 2.

    Signatures are stupid.
  • 11-29-2012 1:53 PM In reply to

    • db2
    • Top 200 Contributor
    • Joined on 06-15-2006
    • Posts 348

    Re: Validating a number

    joe.edwards:
    snoofle:
    priceIsValid

    I remember that show.

    Doesn't it come on just before root:wheel of /bin/fortune?
  • 11-29-2012 1:56 PM In reply to

    Re: Validating a number

    joe.edwards:

    Maybe if the only valid numbers were powers of 2.

    Yeah... well... powers of two are the only good numbers, anyway!

  • 11-29-2012 1:59 PM In reply to

    Re: Validating a number

    _leonardo_:
    At a high level:  If the field is truly numeric, then the leading zeros can be dropped.  On the other hand, if the leading zeros are important, then these things are just strings.  Yes?  Either way, that random-ass underscore is a huge pain. 
    In what way?  The _0001 is the name of an enumeration value.  You can't access it by _1, which is why they have to use 4-digit numbers.  And you can't declare a variable/value that starts with a number, which is why the underscore is there.
  • 11-29-2012 2:11 PM In reply to

    Re: Validating a number

    Evilweasil:
    joe.edwards:

    Maybe if the only valid numbers were powers of 2.

    Yeah... well... powers of two are the only good numbers, anyway!

    validNumbersMask |= ( 1 << 1 );
    validNumbersMask |= ( 1 << 2 );
    validNumbersMask |= ( 1 << 3 );
    validNumbersMask |= ( 1 << 4 );
    // 5 and 6 are not valid numbers
    validNumbersMask |= ( 1 << 7 );
    validNumbersMask |= ( 1 << 8 );
    validNumbersMask |= ( 1 << 9 );
    ...
    

    Of course, to save time/space you would compute the value then replace it with:

    validNumbersMask = BigInteger.valueOf( "548690845690805689054898789769302576894758967340956987439865734068797472760940968056843258237589732895728957982758972983572687439873984678923689478937987234896789367639878071706987432807602976036326873209476732094679862374...");

    Signatures are stupid.
  • 11-29-2012 2:26 PM In reply to

    Re: Validating a number

    db2:
    joe.edwards:
    snoofle:
    priceIsValid

    I remember that show.

    Doesn't it come on just before root:wheel of /bin/fortune?

    No, that's (deal||!deal)

    Signatures are stupid.
  • 11-29-2012 2:27 PM In reply to

    Re: Validating a number

    I'm glad we had this chat. As time progresses, i'm realizing what a horrible idea this was.

  • 11-29-2012 3:02 PM In reply to

    Re: Validating a number

    snoofle:
    Since these were the sorts of things that would never change in our industry

    The worst (and most common) lie told in our industry.


    [mod - to prove a point - PJH]
  • 11-29-2012 3:22 PM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 3,836

    Re: Validating a number

    snoofle:
    It turns out that they asked to create a db table with all the valid values. Since these were the sorts of things that would never change in our industry, they were told to just hard code the values.
    Was that the only reason not to use another table? Or was another reason simply premature optimisation? (i.e. saving a lookup/join on a table that happens infrequently enough to not matter at all in the grand scheme of things.)
    "Because you watched 'The Very Hungry Caterpillar,' we recommend 'The Human Centipede.'"
    --
    UED - Countryside: To kill Piers Morgan
  • 11-29-2012 3:29 PM In reply to

    Re: Validating a number

    db2:
    joe.edwards:
    snoofle:
    priceIsValid

    I remember that show.

    Doesn't it come on just before root:wheel of /bin/fortune?
    The show you're probably thinking of is Win, Lose or FILE_NOT_FOUND.

     

  • 11-29-2012 3:35 PM In reply to

    Re: Validating a number

    PJH:
    snoofle:
    It turns out that they asked to create a db table with all the valid values. Since these were the sorts of things that would never change in our industry, they were told to just hard code the values.
    Was that the only reason not to use another table? Or was another reason simply premature optimisation? (i.e. saving a lookup/join on a table that happens infrequently enough to not matter at all in the grand scheme of things.)
    These constants are the underpinnings of our industry. They've been around for about 40 years, and aren't going to change in any way, short of the American financial system completely collapsing and going away.

    There is a constants file with lots of:

     

      public static int XXX = 1;
    public static int YYY = 2;
    ...

    They put the leading underscore in because you can't start an enum name with a digit. They made them all 4 digits so as to be able to work with a consistent sized number (not necessary as they could have simply done: valueOf("_" + n).

    Of course, putting the constants (mentioned above) into a HashSet would have been the efficient way to go.

    Be brief, no matter how long it takes.
  • 11-29-2012 3:46 PM In reply to

    Re: Validating a number

    snoofle:

                 ValidNumbers vn = valueOf("_"+new DecimalFormat("0000").format(n));

    How is valueOf implemented? Is it an internal function of Java/C#/whatever this is?

    Does this likely use a hashtable or rather a linear search?

     

    DrPepper:
    It will next time the tags are purged.

  • 11-29-2012 4:06 PM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 3,836

    Re: Validating a number

    topspin:

    DrPepper:
    It will next time the tags are purged.

    Doesn't even have to be that long. Look again.
    "Because you watched 'The Very Hungry Caterpillar,' we recommend 'The Human Centipede.'"
    --
    UED - Countryside: To kill Piers Morgan
  • 11-29-2012 4:43 PM In reply to

    • angrysoul
    • Not Ranked
    • Joined on 08-24-2009
    • Speed is 0.0003132 m/s
    • Posts 5

    Re: Validating a number

    snoofle has just produced a 1337 post!
  • 11-30-2012 1:37 AM In reply to

    Re: Validating a number

    snoofle:
    [...], short of the American financial system completely collapsing and going away.
    Wouldn't be the first time.

    Anyway, this could be reasonably solved by making another class with a static HashSet and a static initialiser that iterates over the values() array of the enum, storing the numeric values of the enum (based on the name() method). But it still feels a bit icky.

     

  • 11-30-2012 8:08 AM In reply to

    • Anketam
    • Top 150 Contributor
    • Joined on 10-12-2011
    • Nar Shaddaa
    • Posts 484

    Re: Validating a number

    Sounds like a job for a regex!

    Project Manager: I don't know. I'm not allowed to talk directly to the customer. Apparently my grip on reality is too tight.
    - Weng
  • 11-30-2012 2:40 PM In reply to

    • PJH
    • Top 10 Contributor
    • Joined on 02-14-2007
    • Newcastle, UK
    • Posts 3,836

    Re: Validating a number

    angrysoul:
    snoofle has just produced a 1337 post!
    Yawn. And?
    "Because you watched 'The Very Hungry Caterpillar,' we recommend 'The Human Centipede.'"
    --
    UED - Countryside: To kill Piers Morgan
  • 11-30-2012 7:09 PM In reply to

    • Ben L.
    • Top 10 Contributor
    • Joined on 12-21-2010
    • HELP I'M TRAPPED IN A COMMUNITY SERVER FACTORY
    • Posts 3,424

    Re: Validating a number

    PJH:
    angrysoul:
    snoofle has just produced a 1337 post!
    Yawn. And?
    NUMBERS ARE EXCITING
    mi'e .ben.ly. .i na selju'o mi la lojban
  • 12-01-2012 4:15 AM In reply to

    • ender
    • Top 50 Contributor
    • Joined on 04-27-2006
    • Sunny side of the Alps
    • Posts 1,398

    Re: Validating a number

    PJH:
    Yawn. And?
    You don't get it because you're not 1337!
    Because 10 billion years' time is so fragile, so ephemeral... it arouses such a bittersweet, almost heartbreaking fondness.
  • 12-01-2012 6:00 AM In reply to

    Re: Validating a number

    snoofle:

    Of course, putting the constants (mentioned above) into a HashSet would have been the efficient way to go.

    It would have been a more efficient way, but the efficient way would have been java.util.BitSet. Uses approximately 1/224 of the memory* and less arithmetic per lookup.

    *Or 1/288 on a 64-bit VM. I'm assuming an object header of 8 bytes.

    You'll probably find that the forum experience is improved by going to the "Site Options" tab of "Edit Profile" and turning off "Display User Signatures".
Page 1 of 1 (26 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems