|
Validating a number
Last post 12-01-2012 6:00 AM by pjt33. 25 replies.
-
11-29-2012 12:26 PM
|
|
-
snoofle


- Joined on 06-22-2006
- Posts 1,423
|
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...
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,139
|
snoofle:priceIsValid
I remember that show.
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
pkmnfrk


- Joined on 05-10-2010
- Posts 366
|
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"
|
|
-
-
Sutherlands


- Joined on 07-24-2008
- Posts 1,420
|
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.
|
|
-
-
_leonardo_


- Joined on 11-09-2012
- Posts 11
|
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.
|
|
-
-
Evilweasil


- Joined on 05-30-2012
- Posts 38
|
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
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,139
|
Maybe if the only valid numbers were powers of 2.
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
-
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,139
|
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...");
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
joe.edwards


- Joined on 08-14-2006
- Dallas, TX
- Posts 1,139
|
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)
I spend most of my life pressing buttons to make the pattern of lights change however I want.
|
|
-
-
-
DrPepper


- Joined on 10-26-2012
- Posts 61
|
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]
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,140
|
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.)
3 logicians go into a bar.; the barman says ‘Would you all like a drink?’. The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
|
|
-
-
da Doctah


- Joined on 02-20-2010
- Posts 1,072
|
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.
|
|
-
-
snoofle


- Joined on 06-22-2006
- Posts 1,423
|
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.
|
|
-
-
topspin


- Joined on 03-07-2008
- Posts 304
|
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.
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,140
|
topspin:
DrPepper:
It will next time
the tags are purged.
Doesn't even have to be that long. Look again.
3 logicians go into a bar.; the barman says ‘Would you all like a drink?’. The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
|
|
-
-
-
Severity One


- Joined on 12-04-2008
- Malta
- Posts 346
|
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.
|
|
-
-
Anketam


- Joined on 10-12-2011
- Nar Shaddaa
- Posts 484
|
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
|
|
-
-
PJH


- Joined on 02-14-2007
- Newcastle, UK
- Posts 3,140
|
angrysoul:snoofle has just produced a 1337 post!
Yawn. And?
3 logicians go into a bar.; the barman says ‘Would you all like a drink?’. The first says 'I’m not sure', the second says 'I’m not sure', and the third says 'Yes'.
|
|
-
-
-
ender


- Joined on 04-27-2006
- Sunny side of the Alps
- Posts 1,139
|
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.
|
|
-
-
pjt33


- Joined on 12-06-2005
- Posts 479
|
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.
|
|
Page 1 of 1 (26 items)
|
|
|