• Joseph Newton (unregistered) in reply to Troy Mclure
    Troy Mclure:
    GoldenRatio:
    Troy Mclure:
    Who gives a fuck? How often do you actually spell it out? You must be a hit with the ladies
    Because it makes me uneasy knowing there are people walking around that don't know this kind of stuff.

    If you pronounce "queue" as "kway", people need to know so they can stay the fuck away from you.

    Yes but spelling and pronouncing are not the same thing. Baited and Bated are pronounced the exact same way - hence the reason I didn't spell it right. Queue and Kway are not pronounced the same.

    If it's any consolation, Troy, all of the people who have corrected you are wrong, also. The correct spelling is "'bated" as in "abated" with the initial "a" elided.

  • (cs) in reply to real_aardvark
    real_aardvark:
    bstorer:
    real_aardvark:
    On the other hand, it's possible to be personally insulting, first time on the thread:
    bstorer:
    Are you being intentionally dense, or does it just come naturally? A ZIP code is defined for use in the US (with some caveats), and is defined as a series of 5 digits. To put in what looks to be a Canadian postal code is nonsensical. Remember, we're talking ZIP codes, here, not postal codes used elsewhere.
    I will try to defend webhamster, chesta and zip.
    zip's question was about representing a ZIP code internally as an integer.
    Yup, de ma faute. I meant to defend "anon," not "zip." Got a little bit of indentationitis there.
    bstorer:
    Yes, yes, ZIP is a class, and I address that point by suggesting a possible solution using classes. But like it or not, eventually we have to talk implementation. zip's suggestion is ridiculed because it doesn't take into account things it doesn't need to take into account.
    Frankly, your implementation still looks like bollocks to me. But you're right, it does look "possible."
    bstorer:
    It's simply mind-numbing to complain because an implementation of a ZIP code can't handle things that aren't ZIP codes. I wouldn't beat down somebody's implementation of BigInteger because it doesn't handle complex numbers.
    I, of course, would insist, in true straw-man style -- nobody even mentioned BigInteger, but what the heck, you're welcome to make this stuff up as you go along -- on beating down such an implementation. Complex numbers are obviously a superclass of BigInteger. Whereas "Postal Code" is clearly not a superclass of "ZIP code." Oh, those naughty implementers of "BigInteger."
    bstorer:
    real_aardvark:
    Thinking of it as an object leads to good design practice. bstorer's design isn't quite right in C++: sub-classing from a concrete base is a no-no. (I'm happy to be told that it is a Good Thing in other languages.)
    It's a Good Thing in many modern OOP languages. C++'s behavior when methods aren't virtual is just silly.
    ... and I said I was happy to be told that. Jeez, how emollient do you want me to be? I may very well be dense, but C++'s behaviour when methods aren't virtual makes perfect sense to me. They ain't virtual, dude. What you see is what you get.

    However, if someone with less of a hair up their ass can offer a concrete example -- I suspect from Python, or from CLisp, or maybe from Smalltalk, then I would still be interested. This isn't sarcastic. I really would be interested. I just need a concrete example. The C++ idiom is just an example to help me formulate the question.

    bstorer:
    And that's fine and dandy. But we're discussing the internal representation of a ZIP code. I don't care how you'd implement the Swedish, Welsh, Mexican, or any other postal code, because they don't solve the problem of implementing a ZIP. Wanna subclass everything from an abstract postal code class? Hey, that's super. But save it for when you start writing a second potential subclass. Either way, I'm probably implementing ZIP with an int (actually, I'd probably go with char[5]).
    No, you're discussing the internal representation of a ZIP code. And a damn good job you're making of it too (even if I would argue that char[6] with a terminating '\0' would make it easier to implement ToString(), for example. But that's just a quibble. Or in fact two nibbles).

    And yes, an abstract postal code class would indeed be super. You have correctly hit the nail on the head.

    This back and forth quoting stuff is getting old, so I'll just do it this way:

    1. The BigInteger point is an analogy. A BigInteger class is used to store arbitrarily long integers. It shouldn't care about complex numbers. Just as a ZIP should care about Canadian postal codes. This is in direct reference to webhampster and chesta. Not anything you said.

    2. My comment about C++'s virtual functions was simply in agreement with your comment, and an explanation as to why you might find my example classes repellant. In Ruby, for example, it's all kosher, though. A big difference between C++'s inheritance and Ruby's is that C++ starts at the pointer's type and works down the subclassing hierarchy. Ruby, though, starts with the type of the object itself, and goes back through the ancestor list until it finds a class that defines that method. In short, you always get virtual methods in Ruby.

    3. I'm talking the internals of ZIP code because the comment I initially replied to was, too. Though I said I wouldn't, here's a quote:

    zip:
    Troy Mclure:
    anon:
     if(zipcode>=00000)
         ...
            if(zipcode>=00)
    
    Umm, is this another WTF - treating numbers as strings?

    Zip codes are NOT numbers. They are strings. Numbers trim leading zeros

    So what's wrong with representing the zip code 00012 as 12 internally?

    (Emphasis mine)

    1. I'd do it as a char[5] because whenever I do ToString (), I'm going to want to make a copy of the data anyway, so that nobody messes with my data. I can easily append a 0 to the end of that without trouble. I'd just create the return variable as a char[6] and zero it out before copying the data.

    2. The comments about an abstract postal code are, again, based upon chesta's comment that a ZIP code implemented as an integer won't be able to handle a Canadian postal code.

    3. You seem to think I have some problem with you, but I really don't. Contrary to what you seem to believe, I didn't call you dense. I did, however, call webhampster and chesta dense. Anyway, this was fun. Let's do it again.

  • (cs)

    'quay' is the word that isn't pronounced 'kway' . . . um, is that a word in the US, or is it used only here in the UK? (off topic, but I'm curious)

  • (cs) in reply to Troy Mclure
    Troy Mclure:

    Nothing really. But hopefully they never go to a 6 digit zip or 12 could be 00012 or 000012. Since its a fixed-length, its really just a preference. Personally since it doesnt make sense to use math operations on it (add, subtract..etc) and leading zeros need to be present I think of it as a string

    I'm pretty sure they will NEVER go to 9 digits. And I doubt there is a country that uses letters in their zipcode. That would be crazy.

  • (cs) in reply to MX5Ringer
    MX5Ringer:
    (On the plus side, it looks like boobs if you squint!)

    To paraphrase Family Guy:

    Programmer 1: Fine, I'm going to go write conditional boobs! Programmer 2: Go ahead, they always come out pointy.

  • (cs) in reply to Andrew
    Andrew:
    In C, this is easy and takes very little memory. Treat the digits as byte array of intgers, each in the range 0-9. For an ASCII digit-string, subtract 48 (ASCII "0") to get numbers. zip[k] -= 48;
    Why oh why do you write 48 instead of '0' ?

    '0' is always the character for zero! 48 is correct if the character set is ASCII but wrong if it is something else like EBCDIC.

    Furthermore, 48 is harder to read than '0'.

    This is not the IOCCC. Or do you have a pain fetish? BTW your whole code snippet is awful.

  • BillyBob (unregistered) in reply to Troy Mclure
    Troy Mclure:
    Baited and Bated are pronounced the exact same way - hence the reason I didn't spell it right.

    correctly :-p

  • BillyBob (unregistered) in reply to Andy
    Andy:
    1) It can make perfect sense to compare two zip-codes lexically - indexing is one immediate application that springs to mind.

    Not sure that this is valid, you'd have a table of zipcodes surely to reduce errors and replication - the indexing can then happen on the foreign key if we are talking about databases or you can make your own indexing rules if we are talking about in memory data.

    2) You've got two sides to this argument. Of course it is desirable for your APPLICATION to treat zip-codes as zip-codes. No-one is suggesting that you shouldn't write a zip-code class to manage the particular behaviours of zip-codes. However, within that class you are going to have to choose an INTERNAL REPRESENTATION for your zip-code data. Here the question of whether it is a string or number is not actually arbitrary, as it will affect the performance of the internal algorithms

    The internal representation is not something you are going to think about upfront. You don't know the problem but have already assigned it to a string or an integer....

    How long before you see a zipcode as "John Doe" if you accept it as a string or your code base littered with protections around the zipcode? :-)

  • (cs)

    The checkdigit is chosen so that all the digits, including the checkdigit, add to a multiple of ten. (One of the things I learned working for the Eagle.) This whole function only makes sense for USA-based zip codes anyway, as the checkdigit rule isn't applied in, say, Canada.

    Here's what my mind comes up with:

    Function check_digit(zipcode as string) as integer
     Dim counter as integer
     Dim s, digits() as string
     digits = zipcode.Split("")
     For each s in digits
      counter = counter + val(s)
     Next
     Return (200-counter) mod 10
    End Function
    

    Oh, and using the ASCII codes is dangerous too, if the value might be stored with a hyphen in it.

  • Edwin (unregistered) in reply to chrismcb
    chrismcb:
    I'm pretty sure they will NEVER go to 9 digits. And I doubt there is a country that uses letters in their zipcode. That would be crazy.

    Assumptions... In the Netherlands we use letters in zipcodes (I am at 3584 BH now).

  • Shim (unregistered) in reply to chimaera
    chimaera:
    'quay' is the word that isn't pronounced 'kway' . . . um, is that a word in the US, or is it used only here in the UK? (off topic, but I'm curious)

    Yes, we have quays (/kees/) here in the states, as well. I don't think a lot of people recognize the word in print, though. I thought of "quay" earlier when someone was talking about someone mispronouncing queue as /kway/. I don't find that to be an obvious mistake. (How do you ever get /kway/ out of queue?)

  • w00t (unregistered) in reply to BillyBob
    BillyBob:
    How long before you see a zipcode as "John Doe" if you accept it as a string or your code base littered with protections around the zipcode? :-)

    As a filthy commie European, I'd have to say "who's to say that ISN'T a valid postal code?". If you take into account even the slightest internationalization issues, a string is the only reasonable way to store a postal code. You might want to do some light validation on the input side of things, provided you actually know the postal code scheme that is being used and are prepared to update the validations when it inevitably changes - and specialized classes that e.g. generate barcodes may require some specified format (but again, those will be country specific).

    There's always a tendency to want to do too much with ZIP codes; like trying to translate ZIP codes to addresses based on deprecated databases. Another thing that irks me is that, yes, there are addresses that span multiple lines and won't fit in the standard format, and there are adresses with no housenumber, or a huge housenumber (e.g. an appartment complex I lived in that, for no good reason, had 4-digit appartment numbers, even though each floor had it's own housenumber and only contained 12 appartments). I say; an address is a bunch of strings, most of the time.

    Also, let's not forget there are too many applications which do not even support ZIP+4 codes.

  • Anonymous (unregistered)
    Still, Kat was surprised when the CalculateCheckDigit function turned out to be thousands of lines long. To wit,
          counter=0;
          if(zipcode>=00000)
          if(zipcode< 10000){
            if(zipcode>=0000)
            if(zipcode<1000){
              if(zipcode>=000)
              if(zipcode<100){
                if(zipcode>=00)
                if(zipcode<10){
                  if(zipcode>=0){
                    if(zipcode>=1){
                      if(zipcode>=2){
                        if(zipcode>=3){
                          if(zipcode>=4){
                            if(zipcode>=5){
                              if(zipcode>=6){
                                if(zipcode>=7){
                                  if(zipcode>=8){
                                    if(zipcode>=9){
                                      counter+=9;
                                    }else{counter+=8;}
                                  }else{counter+=7;}
                                }else{counter+=6;}
                              }else{counter+=5;}
                            }else{counter+=4;}
                          }else{counter+=3;}
                        }else{counter+=2;}
                      }else{counter+=1;}
                    }else{counter+=0;}
                }
                if(zipcode>=10)
                if(zipcode<20){
                  if(zipcode>=0){
                    if(zipcode>=1){
                      if(zipcode>=2){
                        if(zipcode>=3){
                          if(zipcode>=4){
                            if(zipcode>=5){
                              if(zipcode>=6){
                                if(zipcode>=7){
                                  if(zipcode>=8){
                                    if(zipcode>=9){
                                      counter+=0;
                                    }else{counter+=9;}
                                  }else{counter+=8;}
                                }else{counter+=7;}
                              }else{counter+=6;}
                            }else{counter+=5;}
                          }else{counter+=4;}
                        }else{counter+=3;}
                      }else{counter+=2;}
                    }else{counter+=1;}
                }
                /* ... */
              }
              /* ... */
            }
            /* ... */
          }
          /* ... */
    
    Wow! Very nice ASCII graphics. It's encouraging to learn that nowadays, there are still productive ASCII artists around. :D
  • Anonymous (unregistered)
    The way Kat (who has been in the industry for many years) describes it:
    The check digit is calculated by adding up all the digits of the barcode, throwing away the tens place, and modding the rest by 10.
    
    Well... why bother with "throwing away the tens place", if you're going to do a "mod 10" anyway?
    So, if all the digits of the barcode add up to 39, you ignore the 3, and 9 mod 10 leaves you with 1. Couldn't be simpler, right?
    Oh! I see. This "mod 10" doesn't mean what we programmers would take it to be. The "mod 10" here means: (x mod 10) == (10 - x)
  • Anonymous (unregistered) in reply to dave
    dave:
    besides the issue of the post making me question my knowledge of modulus, I think this is a prime example of the folly of metrics. As horrible as this code is I bet the coder was very efficient in terms of daily LOC (lines of code) produced.

    Yeah, if not just in terms of daily WTF. :D

  • Robert (unregistered)

    Nice graphics this double arrow thingie.

  • Matthew Watson (unregistered) in reply to Edwin
    Edwin:
    chrismcb:
    I'm pretty sure they will NEVER go to 9 digits. And I doubt there is a country that uses letters in their zipcode. That would be crazy.

    Assumptions... In the Netherlands we use letters in zipcodes (I am at 3584 BH now).

    I'm pretty sure he was being sarcastic.

  • Will (unregistered) in reply to Franz Kafka
    Franz Kafka:
    Welbog:
    Will:
    If you assume that the number 10 (and none of the others) is an octal representation, then sure, 9 mod 10 == 1.
    It could also be base 4, while we're making silly assumptions.

    Nah, 9 isn't a number in base 4. Not in base 8 either.

    Sorry, which part of "and none of the others" wasn't clear?

  • captcha: yummy ??? (unregistered) in reply to Whitey
    Whitey:
    The point being that sure, custom object for a "zipcode" is probably the right way to do things. But if you've got a deadline and the "zipcode" you're dealing with is only referenced a few places and you can control the inputs, you may decide that it will work just as well and be done quicker if you just use a string.

    OMG WTF !!!

    erm what about the poor sap coming along behind you that has to maintain your mess, or make a small change im sure he will have a deadline too.

    Im fairly certain it is exactly this sort of thinking that is the root cause for a lot of the WTF's we see. It certainly is for the ones i come across in projects i work on.

    Sure you know you should 'probably' do it the right way, but because you cant be bothered spending an extra minute or two doing it properly you dont? sheesh! hell most modern IDE's will do half the work, if not more, for you.

    <runs away to burry his head in the sand>
  • (cs) in reply to Troy Mclure

    [quote user="Troy Mclure

    Ok well when databases/programming languages establish the zip code or telephone number datatype please let me know. I'm sitting here with baited breath[/quote]

    Do you mean that you have a worm in your mouth (baited) or that you are holding your breath (bated)? It probably makes a big difference to most people who know you.

  • (cs) in reply to real_aardvark

    "As has been pointed out above, it makes no sense to apply arithmetic operations on zip codes. Ergo, they are not numbers. It makes no sense to concatenate them, or to compare them lexically, or to do any one of a number of stringesque things with them. Ergo, they are not strings."

    I work in a mailing house. We have to deal with millions of Zip codes per week.

    Hate to say you're wrong, but I have to perform "stringesque" operations on Zip codes all the time.

    For example, comparing the first 2 or 3 characters to the first 2 or 3 characters of other Zips. This gives you the state, and the first level of presort discount (a "3-digit presort" gives you a discount on the postage of the mailing; a "5-digit presort" gives you a bigger discount).

    (Has to be range comparisons on the first 2 digits. California, for example, has several "first 2s".)

    I have to parse them pretty regularly. (To generate check-digits, amongst other things.)

    I also regularly have to concatenate them to generate paragraph-format lists of Zip codes. The address list tables have 1 address per row, which has to be concatenated for certain reports into a single field (usually comma-delimited) on a per-mailing basis. This, of course, also means pulling out the +4 portion of 9-digit Zips, and eliminating duplicates.

    These are all very basic, very simple operations, and the code I write does them very easily, but they are all string manipulations on Zip codes.

    Sure, if the only time you have to pay attention to a Zip code is when you're typing yours into an order form on Amazon.com, or when you're filling out an application for a credit card, you can assume that Zips aren't treated as strings. But in the line of work I'm in, Zips are, very definitely, strings. Let some spreadsheet file treat them as numbers and drop leading zeroes or subtract the +4 from the Zip, and there are major problems.

    (And, since what I do with them is what the Post Office originally created them for, I think it's fair to assume the basic type is "string".)

  • AdT (unregistered) in reply to real_aardvark
    real_aardvark:
    The statement that "Zip codes ... are strings" is an example of the sort of category error that brings us the diseased syntax of XML (where metadata is forced in-band and considered as a string).

    Yeah, let's do serialization without forcing metadata in-band. Oh, wait, that's the entire purpose of serialization.

  • AdT (unregistered) in reply to Troy Mclure
    Troy Mclure:
    No I am not a VB programmer. I actually sell paintings under the 101. Maybe you've seen me - Painting by Troy. And you can throw all the fucking semantics you want at me. I'm not very impressed.

    Never mind, he's just an aarrogant aardvark.

  • AdT (unregistered) in reply to real_aardvark
    real_aardvark:
    Well, I make a perfectly innocent Sesame Street joke and get blasted by mindless idiots.

    Well put, aardvark. You are absolutely right about the proper use of abstractions. And you are also a loud-mouthed, self-indulgent, unmannered dirtbag who makes Amon Göth look like a philanthropist. Should a truck run you over as you cross the street, I promise to buy the driver a bouquet.

    Captcha: sanitarium (again, I swear!)

  • (cs) in reply to ssprencel
    ssprencel:
    zip:
    "9 mod 10 leaves you with 1" Have I seriously been doing mods wrong my entire life?
    Have you seriously been doing mods your entire life? I didn't get started until I got into programming. ;)
    It was very useful before playing tag when everyone formed a circle and every seventh fist was tossed out.
  • (cs) in reply to webhamster
    webhamster:
    chesta:
    zip:
    Troy Mclure:
    anon:
     if(zipcode>=00000)
         ...
            if(zipcode>=00)
    
    Umm, is this another WTF - treating numbers as strings?

    Zip codes are NOT numbers. They are strings. Numbers trim leading zeros

    So what's wrong with representing the zip code 00012 as 12 internally?

    Internationalization, for one thing. Not everyone uses numbers.

    Extended (9-digit) zip codes, for another. If you have 1234 in a 9-digit zipcode field, is that supposed to be 01234-0000 or is the first part missing, as in 00000-1234?

    Exactly. Try feeding this thing R2J 8K7 and see what happens.

    Let me guess, eh. The CD-ROM drive opens and a piece of back bacon comes out.

  • GrouchyAdmin (unregistered) in reply to Franz Kafka
    Franz Kafka:
    Nah, 9 isn't a number in base 4. Not in base 8 either.

    Overflow. ;)

  • bull (unregistered)

    Perfect example of "NICE and clean" code

  • chrisM (unregistered) in reply to Troy Mclure

    -- If you pronounce "queue" as "kway", people need to know so they can stay the fuck away from you.

    -- Yes but spelling and pronouncing are not the same thing. Baited and Bated are pronounced the exact same way - hence the reason I didn't spell it right. Queue and Kway are not pronounced the same.

    Ok - maybe this is a Brit. U.S. thing. What is this Kway of which you speak? Could it be the word quay which on this side we pronounce as key?

  • chrisM (unregistered) in reply to chimaera
    chimaera:
    'quay' is the word that isn't pronounced 'kway' . . . um, is that a word in the US, or is it used only here in the UK? (off topic, but I'm curious)

    00ps - sorry, missed that reply when i skimmed. over 2 years ago anyway so prob will never know.

Leave a comment on “Checking Your Digits”

Log In or post as a guest

Replying to comment #:

« Return to Article