How not to generate a CSV report

  • I've been tasked with migrating a CSV report generator to our new codebase.  Simple enough, I thought.  (Famous last words, I know.)

    Here's how the developer of the old code decided which fields to output to the report (community server ate the tabs, and won't let me do <int, String> properly):

    [code] private static Map<int, string=""> templates = new HashMap<int, string="">();
    static {
    // spacing appears to be critical (especially no space before quoted
    // values)
    // cases with no defined mapping, mapped to same as 1
    templates.put(0, "%s, , , , ,%s, , , , %s");
    templates.put(2, "%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s");
    templates.put(6, "%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s");
    templates.put(33, "%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s");
    templates.put(34, "%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s");

    templates.put(1, "%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s");
    templates.put(8, "%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s");
    templates.put(9, "%s, , , , , ,%s, , , %s");
    templates.put(10, "%s, , , , , ,%s, , , %s");
    templates.put(11, "%s, , , , , ,%s, , , %s");
    templates.put(12, "%s, %s, %s, %s, %s, %s, %s, %s,%s,%s");
    templates.put(13, "%s, , , , , , %s, , , %s");
    templates.put(14, "%s, , , , , , %s, , , %s");
    templates.put(17, "%s, , ,%s , ,%s,%s, , , %s");
    templates.put(18, "%s, , ,%s , ,%s,%s, , , %s");
    templates.put(19, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(20, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(21, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(22, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(23, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(24, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(25, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(26, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(27, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(28, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(29, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(30, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(31, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(32, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(35, "%s, , ,%s , ,%s,%s, , , %s");
    templates.put(36, "%s, , ,%s , ,%s,%s, , , %s");
    templates.put(37, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(38, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(41, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(42, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(51, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(52, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(53, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");
    templates.put(54, "%s, %s, %s, %s,%s,%s, %s, %s,%s,%s");

    It should be noted that in the cases where he omits fields, those fields would be blank anyway.

    These integers look magic; they're actually obtained from another map (a Map<String, int>) which maps transaction descriptions to the arbitrary integers you see above. So I guess at least I can follow the numbers and figure out which types of transactions end up with which formats... though I don't think that makes it acceptable.

    The integers are then used in a giant switch statement:

    [code]switch (transaction) {
    throw new IllegalStateException("unsupported format: " + transaction);

    case 0:
    // do something

    case 2:
    case 6:
    case 33:
    case 34:
    case 1:
    case 8:
    // do something

    case 9:
    case 10:
    case 11:
    case 13:
    case 14:
    // do something
    if (transaction == 10) { // debit
    if (somecondition()) {
    transaction = 11;
    } else if (transaction == 11) { // credit
    if (somecondition()) {
    transaction = 10;
    // do something

    case 17:
    case 18:
    case 35:
    case 36:
    // do something

    case 12:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
    case 26:
    case 27:
    case 28:
    case 29:
    case 30:
    case 31:
    case 32:
    case 37:
    case 38:
    case 41:
    case 42:
    case 51:
    case 52:
    case 53:
    case 54:
    // do something

    I replaced some code with "// do something" comments to remove code I probably shouldn't publish. The section for cases 2, 6, 33, 34, 1, and 8 is just two lines, one of which is commented out; these two lines are identical to the lines in the last large case block.


  • Huzzah! Magic numbers and ignorance of .join().

  • What a moron whoever wrote this, I mean, google for "java csv" and you've thousands of hits

  •  Have you considered homicide as a soultion?

Log in to reply

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