• Hanzito (unregistered)

    Slightly disappointed the code doesn't use the classic# switch-fall-through (goto 2, goto 3, if I'm not mistaken.)

  • TheCPUWizard (unregistered)

    Please provide an alternate implementation that allows one to se a breakpoint when 2 leading characters need to be added...

  • (nodebb)

    Beautiful example of enterprise-level code, including the unnecesary cast to string in the first line. The presence of a DataRow might indicate this is an attempt to somewhat sanitize user input (best case case scenario) or just to deal with poorly formatted data from a database, or maybe both.

  • (nodebb)

    Ehm, honestly not sure how to make this a lot better because we know little about the DB in question. At least I guess that dr stands for a DbDataReader, so the first ToString() is required but using the actually type would be better. Maybe it's a float, maybe a int, maybe some weird Oracle type - who knows. It could also be a string, not clear from only this bit of code. Now if you have the string I don't see how you would implement the switch otherwise, sure you could use a switch expression but that's it. Using a if else if chain is definitely less clear, using a reference type is obviously wrong too for a 3 case example (those only make sense with 10+ cases).

    So TLDR, the only WTF here is pretty much not using an the correct type specific method of the DbDataReader (https://learn.microsoft.com/en-us/dotnet/api/system.data.common.dbdatareader?view=net-9.0).

  • Marvin (unregistered)

    Well, there's String.PadLeft() ...

  • (nodebb)

    If that is a database connection, reading the value will give an opaque 'object', not the properly typed 'int' or 'long' that 'ToString("d3")' could be used with. Even then, though, they could 'PadLeft('0', 3)' the string instead of using the switch statement.

  • (nodebb)

    Yes, we've seen it many times . . . but I admire the nice touch of assigning a variable to itself in the "default". The cherry on the top of this Unnecessary Sundae.

  • (author) in reply to TheCPUWizard

    You can just use a conditional breakpoint, like a normal person.

  • Tim (unregistered) in reply to Hanzito

    I'm sure the true WTF programmer could have found a way to incorporate Duff's device into this

  • left-pad (unregistered)

    still better than npm install left-pad

  • (nodebb) in reply to Hanzito

    In C# fall-through doesn't require a goto, because it's 100% safe by itself:

    switch(example)
    {
      case null: case int _: case string _: Console.WriteLine(example); break;
    }
    

    However there is a transfer control statement goto case for non-fall-through scenarios:

    switch(example)
    {
      case null: return;
      case int intValue: Console.WriteLine(++intValue); goto case null;
    }
    
  • Officer Johnny Holzkopf (unregistered)

    9999 TransactionOrders, and then?

  • Álvaro González (github) in reply to Officer Johnny Holzkopf

    9999 TransactionOrders, and then?

    Company closes for the day and everybody goes home?

  • (nodebb) in reply to Officer Johnny Holzkopf

    Well, we don't know the whole story. It is quite common in business to have combined numbers, so for example a product segment code, the order date plus an index - so a four digit index could be more than enough. Would also explain the leading zeros cause as we all know, it's best practice to eliminate leading zeros - but never say that out loud with a line manager in the room.

  • (nodebb)

    The cast to coerce the return of .toString().Trim() (which, logically, should be a string) to string makes my eye twitch...

  • (nodebb) in reply to Steve_The_Cynic

    Looks to me like unclean refactoring. I wouldn't be surprised if there was a hard cast before because the value is actually also a string in the DB. Then someone used ToString() instead of the proper GetString() to add the Trim() and was to sloppy to remove the now redundant cast. However, maybe it's not even a DbDataReader but something completely different - it's very hard to guess what is even going on by those limited copy examples.

    Addendum 2024-12-16 17:37: copy examples ? More like code samples - my cue to get back to coding :-)

  • LZ79LRU (unregistered) in reply to MaxiTB

    With numbers maybe. But this is definitively not a number. So much is obvious from the code. It's a string that happens to be composed of numerical digits. And for those, leading zeroes can be both a requirement and are often just plain useful precisely to ensure nobody can store it as a number and do number things with them.

  • Virginia (unregistered) in reply to Steve_The_Cynic

    I think it might have started with =(string)dr["TransactionOrder"]; then the requirement came to add a trim, and =(string)dr["TransactionOrder"].Trim() didn't work, so they added an extra .ToString()

Leave a comment on “A Little Extra Padding”

Log In or post as a guest

Replying to comment #:

« Return to Article