• 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?

Leave a comment on “A Little Extra Padding”

Log In or post as a guest

Replying to comment #:

« Return to Article