• (nodebb)

    It's not someone who's afraid of logarithms, but more afraid of mhtiragols, better known as powers (we want Math.Pow(...) rather than Math.Log(...)).

    But yeah, on a scale of badness of code, this scores highly.

  • Zatapatique (unregistered)

    ' *** the generates a frist post!

  • Hans (unregistered)

    12345.ToString("D99") generates 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012345

    which I assume (didn't count it) is 99 digits long

  • Robin (unregistered) in reply to Steve_The_Cynic

    I came to make the same point - glad to see I wasn't getting confused!

  • (nodebb)

    I wonder why they cap the "number of decimals" to 99 whereas x will be hard-coded to 1 as soon as numDecimals > 9 (due to the conversion to int throwing an OverflowException).

    Also, the actual function... Why do they even tie the case to parity?

  • NotACoder (unregistered)

    And... the actual function's comment gives 'aa7bb' as an example, but can't actually return that? (only 'aa7BB')

  • Weretaco (unregistered)

    It can't return either, but can return aA7Bb

  • Weretaco (unregistered)

    Nvm, I was mistaken

  • Tinkle (unregistered)

    Ah, so the real WTF is they excluded z, Z and 9?

  • erichamion (unregistered) in reply to Medinoc

    Why tie the case to parity? Generating a random character code that could be in either the capital letter range or the lowercase letter range, but not in between, is hard. It's easier to use a random number to flip the case 50% of the time. But why generate a new random number, when a perfectly good random number has already been generated? Of course they reuse i. To do anything else would be wasteful!

  • (nodebb) in reply to erichamion

    Generating a random character code that could be in either the capital letter range or the lowercase letter range, but not in between, is hard.

    No, it's easy. In C/C++ style pseudocode "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[random(0 to 51 inclusive)]

  • (nodebb)

    Am I missing something, or is the first if block totally unnecessary? It sets n and x, but these variables are never used. It fills in strNum, but then it's immediately cleared.

    Could it be left over from some previous iteration of the code that used these variables?

  • (nodebb)

    It also sets the 1st, 2nd, 4th, 5th, 8th and 9th characters to letters (and the rest as numbers) - even though the string can't have an 8th or 9th character. Which rather suggests that it used to generate 10-character strings at one point.

  • HO (unregistered)

    Moment of horror as I saw ***. Someone found my code. But the naming on the next line calmed me down, as I have never used that standard.

    I have used *** as TODO/Fix/GetBackTo for as long as I remember, but do not know where that came from. Was it a standard in some language?

  • (nodebb)

    A management edict for "minimize lines of code touched to close a ticket" probably has a lot to do with the evolution of this function. And most of the rest of their doubtless benighted codebase.

    When refactoring is outlawed, only outlaws will refactor.

  • (nodebb) in reply to Barry Margolin

    n is used to set x, and x is appended to the return value if numDecimals is greater than 0.

  • (nodebb)

    I came to the comments to point out that System.Random's next(min, max) method is exclusive on max. That is, if you want 2 digits, it will only return up to and including 98.

    But then I saw the code for the ID generator and quickly realised that was the least of their problems. That's the sort of code that is only fixed by selecting all and deleting.

  • (nodebb)

    I once worked on a system that generated "ticket IDs" that looked like a third of a UUID. The initial code generated all the random bits and then queried the database to see if it existed and if it did, generated a new one until it had a unique one. This was getting slow.

    At that time, my approach to unique IDs was to let the database generate the next sequence number, but that wouldn't look like the "ticket ID". And I didn't really know about UUIDs back then. So I modified the generation algorithm to generate the first part and get all IDs with that prefix, then it could do the generate-the-next-one-check-it-doesn"t-exist in memory. Way way faster, and it kept the same format. which were the only two things I cared about at that time.

  • (nodebb)

    I mean, I can see why you'd return your 5-digit random number as a string if you were going to support leading 0 digits, but....

Leave a comment on “The 5-Digit Session Identifier”

Log In or post as a guest

Replying to comment #:

« Return to Article