Simultaneously ingenious and wtf.



  • found this today (c#):

                    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();
    



    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()



  • @lincoln said:

    found this today (c#):

    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();

    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()

    PadLeft() wouldn't throw an exception if orderId.ToString() was length 0 though (I assume it's an int so that wouldn't happen, but it could be something else).  I mean using that for error checking on orderId is crazy when compared with a simple check for it, but still.



  • Simple case of "not knowing the default library calls". Just tell the guy who made it that the pad function exists and be done with it.



    Did the same in python a week ago.



    if line[0:11] == "hello jonny":

    print "It's jonny!"



    And then you encounter the "startswith" function a day later.



  • string orderNumber = orderId.ToString();

    while(orderNumber.Length < 8)
    orderNumber = "0" + orderNumber;

    I don't know why everyone insists on making everything so complicated.



  • @pkmnfrk said:

    string orderNumber = orderId.ToString();

    while(orderNumber.Length < 8)
        orderNumber = "0" + orderNumber;

    I don't know why everyone insists on making everything so complicated.

    I will admit one time out of laziness I did:

    if(num < 10)
        fileOut.write("0");

    fileOut.WriteLine(num);



  • @locallunatic said:

    @lincoln said:

    found this today (c#):

    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();

    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()

    PadLeft() wouldn't throw an exception if orderId.ToString() was length 0 though (I assume it's an int so that wouldn't happen, but it could be something else).  I mean using that for error checking on orderId is crazy when compared with a simple check for it, but still.

    I don't see where that happens in the OP.

    Also:

    string orderNumber = orderId.ToString("D8");

    Of course, having to carry around the orderId as a string is probably TRWTF (it certainly is in the codebase I maintain).



  • @Sutherlands said:

    @locallunatic said:

    @lincoln said:

    found this today (c#):

    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();

    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()

    PadLeft() wouldn't throw an exception if orderId.ToString() was length 0 though (I assume it's an int so that wouldn't happen, but it could be something else).  I mean using that for error checking on orderId is crazy when compared with a simple check for it, but still.

    I don't see where that happens in the OP.

    "0000000".Substring(0, 8 - orderId.ToString().Length)

    If the ToString() is length 0 then you are doing a Substring(0,8) on a length 7 string.  Unless I picked the wrong language from the line (.Net throws an exception if start+length ends up outside the string).



  • @locallunatic said:

    @Sutherlands said:

    @locallunatic said:

    @lincoln said:

    found this today (c#):

    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();

    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()

    PadLeft() wouldn't throw an exception if orderId.ToString() was length 0 though (I assume it's an int so that wouldn't happen, but it could be something else).  I mean using that for error checking on orderId is crazy when compared with a simple check for it, but still.

    I don't see where that happens in the OP.

    "0000000".Substring(0, 8 - orderId.ToString().Length)

    If the ToString() is length 0 then you are doing a Substring(0,8) on a length 7 string.  Unless I picked the wrong language from the line (.Net throws an exception if start+length ends up outside the string).

    What number converts to a zero-length string?


  • @Jaime said:

    @locallunatic said:

    @Sutherlands said:

    @locallunatic said:

    @lincoln said:

    found this today (c#):

    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();

    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()

    PadLeft() wouldn't throw an exception if orderId.ToString() was length 0 though (I assume it's an int so that wouldn't happen, but it could be something else).  I mean using that for error checking on orderId is crazy when compared with a simple check for it, but still.

    I don't see where that happens in the OP.

    "0000000".Substring(0, 8 - orderId.ToString().Length)

    If the ToString() is length 0 then you are doing a Substring(0,8) on a length 7 string.  Unless I picked the wrong language from the line (.Net throws an exception if start+length ends up outside the string).

    What number converts to a zero-length string?

    They don't, but we don't know what kind of thing orderId actually is so I put qualifiers on it (bolded it for you).



  • @Jaime said:

    What number converts to a zero-length string?
    Zero, in certain numbering systems. You gotta think of the Ancient Roman users.



  • @locallunatic said:

    "0000000".Substring(0, 8 - orderId.ToString().Length)

    If the ToString() is length 0 then you are doing a Substring(0,8) on a length 7 string.  Unless I picked the wrong language from the line (.Net throws an exception if start+length ends up outside the string).

    Didn't notice that there were only 7 0s.  Yes, that would be the case then.


  • @lincoln said:

    found this today (c#):

                    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();
    



    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()

    Our off-shore team once came up with such a function containing 10 lines of string manipulation for doing something simple like:

    [code]return 1000000000 + orderId;[/code]

    When I pointed it out during code review, their response was: "but it passed code review when your predecessor was in charge".



  • @bjolling said:

    "but it passed code review when your predecessor was in charge."
     

    "And that would be why he's nolonger in charge."


  • Discourse touched me in a no-no place

    @lincoln said:

    found this today (c#):

                    string orderNumber = "0000000".Substring(0, 8 - orderId.ToString().Length) + orderId.ToString();
    



    So apparently the previous guy was sharp enough to come up with a sort of compact solution for padding out some text, but not so much that he would just call PadLeft()

    Well, that's how you do it in SQL and in Postscript. So the previous guy might have come through my meatgrinder of a team, since I'm pretty sure we're the only programmers on earth that deal in all three of C#, Postscript and SQL (in addition to a couple of our own homegrown XML-based languages that run on top of C#)



  • @Weng said:

    ..., since I'm pretty sure we're the only programmers on earth that deal in all three of C#, Postscript and SQL (in addition to a couple of our own homegrown XML-based languages that run on top of C#)

    Sorry, there are others who include all three in the suite of environments they have to work int. In fact it is not uncommon to have many more than just those three....


Log in to reply
 

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