Think it all the way through...



  •   public String getAgeAsString(int age) {
        if (age < 10)    return new DecimalFormat("0").format(age);
        if (age < 100)   return new DecimalFormat("00").format(age);
        if (age < 1000)  return new DecimalFormat("000").format(age);
        if (age < 10000) return new DecimalFormat("0000").format(age);
        return "" + age;
      }
    


  • All i can think of is there's some reason to use DecimalFormat (internationalization?) rather than just calling .toString();

    (Or it was written by an idiot.)



  • Wow. Just wow. Just wow. I mean, ToString(). I mean, why stop at 10000. I mean, why only AgeAsString? What about HeightAsString, WeightAsString, IQAsString? I mean, wow. I mean, paid by lines of code?



  • If I didn't know that Snoofle's shop does Java, I'd be shouting WTF about all the memory leaks. 

    People, please remember to language-tag your samples. They're not than unambiguous.



  • @blakeyrat said:

    All i can think of is there's some reason to use DecimalFormat (internationalization?) rather than just calling .toString();

    (Or it was written by an idiot.)

    We are *not* international, so yes, it was written by an idiot.


  • @DrPepper said:

    Wow. Just wow. Just wow. I mean, ToString(). I mean, why stop at 10000. I mean, why only AgeAsString? What about HeightAsString, WeightAsString, IQAsString? I mean, wow. I mean, paid by lines of code?
    There are a few variants (cut-n-pasted) throughout the code base; some up to 8 digits. I figured one instance was horrible enough for a post.



  • @OldCrow said:

    If I didn't know that Snoofle's shop does Java, I'd be shouting WTF about all the memory leaks. 

    People, please remember to language-tag your samples. They're not than unambiguous.

     

     

    What other language could this be that forces deterministic cleanup?

     



  • @BC_Programmer said:

    @OldCrow said:

    If I didn't know that Snoofle's shop does Java, I'd be shouting WTF about all the memory leaks. 

    People, please remember to language-tag your samples. They're not than unambiguous.

     

     

    What other language could this be that forces deterministic cleanup?

     

     What language could this be, period?  Native classes have proper case, functions have camel case, uses new to generate an instance... semicolon ended...



  • @Sutherlands said:

    @BC_Programmer said:

    @OldCrow said:

    If I didn't know that Snoofle's shop does Java, I'd be shouting WTF about all the memory leaks. 

    People, please remember to language-tag your samples. They're not than unambiguous.

     

     

    What other language could this be that forces deterministic cleanup?

     

     What language could this be, period?  Native classes have proper case, functions have camel case, uses new to generate an instance... semicolon ended...

    Can't be python because of the irritating alignment of those returns. I really hate aligners.



  •  I believe this is a common pattern... We got something similar (but in C, and with decimal, so it is much more ugly) :

     AmountToStr(char* Str,long long Amount, int NbDecimals){

          sprintf(Str,"%lld",Amount);
          int Length = strlen(Str);

           /* Apply scale down /
          if (abs(Amount) >= 1000){
             switch (NbDecimals){
             case 0:
                /
    Delete the 2 decimals /
                Str[Length-2] = '\0';
                break;
             case 2:
                Str[Length]   = Str[Length-];
                Str[Length-1] = Str[Length-2];
                Str[Length-2] = '.';
                Str[Length+1] = '\0';
                break;
             case 3:
                /
    Add a decimal*/
                Str[Length]   = Str[Length-1];
                Str[Length-1] = Str[Length-2];
                Str[Length-2] = '.';
                Str[Length+1] = '0';
                Str[Length+2] = '\0';
                break;
             default:
                /* Should not happen /
                puts("!!! Nb of decimals to display must be 0, 2 or 3\n");
                break;
             }
          }
          else {
             if (abs(Amount) >= 100){
                switch (NbDecimals){
                case 0:
                   /
    Delete the 2 decimals /
                   Str[Length]                = Str[Length-2];
                   Str[Length-1] = '.';
                   Str[Length-2] = '0';
                   break;
                case 2:
                   Str[Length+1] = Str[Length-1];
                   Str[Length]   = Str[Length-2];
                   Str[Length-1] = '.';
                   Str[Length-2] = '0';
                   Str[Length+2] = '\0';
                   break;
                case 3:
                   Str[Length+1]   = Str[Length-1];
                   Str[Length]   = Str[Length-2];
                   Str[Length-1]   = '.';
                   Str[Length-2]   = '0';
                   Str[Length+2]   = '0';
                   Str[Length+3] = '\0';
                   break;
                default:
                   /
    Should not happen /
                   puts("!!! Nb of decimals to display must be 0, 2 or 3\n");
                   break;
                }
             }
             else {
                switch (NbDecimals){
                case 0:
                   /
    Delete the 2 decimals: amount value is 0 /
                   Str[Length-1] = '0';
                   Str[Length]                = '\0';
                   break;
                case 2:
                   Str[Length+2]   = Str[Length-1];
                   Str[Length+1]   = '0';
                   Str[Length]                  = '.';
                   Str[Length-1]   = '0';
                   Str[Length+3] = '\0';
                   break;
                case 3:
                   Str[Length+2]   = Str[Length-1];
                   Str[Length+1]   = '0';
                   Str[Length]                  = '.';
                   Str[Length-1]   = '0';
                   Str[Length+3] = '0';
                   Str[Length+4]  = '\0';
                   break;
                default:
                   /
    Should not happen /
                   puts("!!! Nb of decimals to display must be 0, 2 or 3\n");
                   break;
                }
             }
          }
          break;
    }

     I let you check that it does not work in some cases...

    Fortunaly, the guy who committed this is not there anymore.

    Here's what was intended:

     AmountToStr(char Str,long long Amount, int NbDecimals){

    switch(NbComDecimals){
        case 0:snprintf(Str, AMOUNT_LENGTH, "%lld",Amount/100);
               break;
        case 1:snprintf(Str, AMOUNT_LENGTH, "%lld.%1lld",Amount/100,abs(Amount/10)%10);
               break;
        case 2:snprintf(Str ,AMOUNT_LENGTH,"%lld.%02lld",Amount/100,abs(Amount)%100);
               break;
        case 3:snprintf(Str ,AMOUNT_LENGTH, "%lld.%02lld0",Amount/100,abs(Amount)%100);
               break;
        default:puts("Wrong nb of decimal);

    }




  • @Sutherlands said:

    @BC_Programmer said:

    @OldCrow said:

    If I didn't know that Snoofle's shop does Java, I'd be shouting WTF about all the memory leaks. 

    People, please remember to language-tag your samples. They're not than unambiguous.

     

     

    What other language could this be that forces deterministic cleanup?

     

     What language could this be, period?  Native classes have proper case, functions have camel case, uses new to generate an instance... semicolon ended...
     

    Actually how many languages COULD this be compiled to?

    Based on the leading "public" I'd guessed Java, but it would seem to be syntactically correct in C++ also.

    EDIT: Wait, no, it wouldn't. C++ doesn't allow summing a native string and an integer.

     

     


  • Discourse touched me in a no-no place

    @OldCrow said:

    C++ doesn't allow summing a native string and an integer.
    It does if you define your own operator overload.



  • @dkf said:

    @OldCrow said:

    C++ doesn't allow summing a native string and an integer.
    It does if you define your own operator overload.

    C++ does not have any "native string". Adding an integer and a char* works fine though.



  • @boh said:

    @dkf said:

    @OldCrow said:

    C++ doesn't allow summing a native string and an integer.
    It does if you define your own operator overload.

    C++ does not have any "native string". Adding an integer and a char* works fine though.

    The STL has a string type, but I'm sure you know that, so all I can figure is you were making some pedantic dickweed point.



  • So, the bigger the number is, the more leading 0s? Or am I reading it wrong?



  • @tutti said:

    So, the bigger the number is, the more leading 0s? Or am I reading it wrong?
    a) it's Java

    b) in the formatter, a zero means output a digit here (as opposed to a '#', which means output a digit if needed). The whole thing could have been reduced to: ""+n, or if you're hell-bent on using a formatter: new DecimalFormat("###0").format(n);

     



  • @snoofle said:

    @tutti said:

    So, the bigger the number is, the more leading 0s? Or am I reading it wrong?
    a) it's Java

    b) in the formatter, a zero means output a digit here (as opposed to a '#', which means output a digit if needed). The whole thing could have been reduced to: ""+n, or if you're hell-bent on using a formatter: new DecimalFormat("###0").format(n);

     


    Integer.toString(n) would be more correct.



  • @Ben L. said:

    @snoofle said:

    @tutti said:

    So, the bigger the number is, the more leading 0s? Or am I reading it wrong?
    a) it's Java

    b) in the formatter, a zero means output a digit here (as opposed to a '#', which means output a digit if needed). The whole thing could have been reduced to: ""+n, or if you're hell-bent on using a formatter: new DecimalFormat("###0").format(n);

     


    Integer.toString(n) would be more correct.

    Ben, will you stop talking about shit you clearly are ignorant of? There's no "more correct". Casting an int to a string by concatenating it with an empty string is extremely common. If I saw somebody using Integer.toString() in my code, I'd probably change it.



  • @morbiuswilters said:

    If I saw somebody using Integer.toString() in my code, I'd probably change it.

    I would change it to Integer.ToString() but it would require a bit of additional work to upgrade the rest of the application and its dependencies.


  • Discourse touched me in a no-no place

    @boh said:

    C++ does not have any "native string".
    Oh, is it called “Original American string” these days? I admit I really don't follow the changes in official preferred lingo for these sorts of things.



  • @dkf said:

    @boh said:
    C++ does not have any "native string".
    Oh, is it called “Original American string” these days? I admit I really don't follow the changes in official preferred lingo for these sorts of things.
    It all depends, I think, on whether you consider the STL to be an integral part of C++ or merely a library to be included to provide extra functionality. Since it's mentioned regularly in The Standard[tm] I'm inclined towards the former.



  • @PJH said:

    @dkf said:
    @boh said:
    C++ does not have any "native string".
    Oh, is it called “Original American string” these days? I admit I really don't follow the changes in official preferred lingo for these sorts of things.
    It all depends, I think, on whether you consider the STL to be an integral part of C++ or merely a library to be included to provide extra functionality. Since it's mentioned regularly in The Standard[tm] I'm inclined towards the former.
     

    I prefer the term "Kanien'kehá:ka rope"



  • @dhromed said:

    @PJH said:

    @dkf said:
    @boh said:
    C++ does not have any "native string".
    Oh, is it called “Original American string” these days? I admit I really don't follow the changes in official preferred lingo for these sorts of things.
    It all depends, I think, on whether you consider the STL to be an integral part of C++ or merely a library to be included to provide extra functionality. Since it's mentioned regularly in The Standard[tm] I'm inclined towards the former.
     

    I prefer the term "Kanien'kehá:ka rope"

    I'm sure it's been mentioned here before, but I'll just leave this here: C string (potentially NSFW, depending on how prudish your W is.)



  • @PJH said:

    I'll just leave this here: C string
     

    That looks super unpractical and uncomfortable. If you're going for such novelty "underwear", might as well wear nothing, and if it's about suggestively covering, then I personally prefer the cloth variants of such garb anyway.

    @PJH said:

    (potentially NSFW, depending on how prudish your W is.)

    If it was my coworker, it would have gone on the LOL mailinglist.

     

     

     

    ) they feel better against my sack.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.