sponsored links



No announcement yet.

Stories of bad coding

  • Filter
  • Time
  • Show
Clear All
new posts

  • Stories of bad coding

    Ever been amending code written by someone else, and seen something that made you go "what the ****?"

    I thought it might be fun to share stories like that, so I've created this thread for people to post them.

    I'll start, with a story form my first year as a dev, over 10 years ago.

    Program processing a list of records for a specific transaction. Old RPGLE program, so it's a READE loop. Probably initially coded in the 90's.
    Then after processing all records, among other things the program has to know the highest date of all the processed records. The records are not being read in date order.

    The obvious answer would be to store the date of the first record, then from then on, update the stored date with the date of the next record if it's higher:

    (all code is RPGLE-ish pseudocode)
    maxDate = *loval;
    READE loop through records
        // Do stuff
        if recordDate > maxDate
            maxdate = recordDate
    //maxDate is the highest date of all records
    But that's not what the original author if this program did
    They read each date into an array
    Then sorted the array into ascending order
    then read the last element in the array, with a hard-coded index

    And then when the program had crashed because the array was too small, various other developers had simply increased the size of the array!
    It looked something like this:

    // maxDateArr dim(100)
    // maxDateArr dim(500)
    //maxDateArr dim(2000)
    maxDateArr dim(10000)
    // dateIndex packed(3) // 3 digit index
    //dateIndex packed(4)  // 4 digit index
    dateIndex packed(5)  // 5 digit index
    READE loop through records
        // Do stuff
        dateIndex = dateIndex + 1
        maxDateArr(dateIndex) = recordDate
    sortA maxDateArr // sort into ascending order, then read value of last element
    //maxDate = maxDateArr(100)
    //maxDate = maxDateArr(500)
    //maxDate = maxDateArr(2000)
    maxDate = maxDateArr(10000)
    I was I think the 4th person to amend the program after an array overflow error. With about 6 months of development experience at the time, and no formal RPGLE training (self taught from the internet). Needless to say, I removed the array and used the if comparison method. And to this day I cannot understand how any professional developer could think that was a good idea