Copy and paste gone bad

A very common way for programmers to build new software is to copy code that is similar to what is needed and then modify it to suit the immediate need. This works regardless of the level of experience or programming language. For significant pieces of code, copying is discouraged in favor of using language features (object oriented or procedural) for sharing code. But, when you just need a few lines that show you how to do a loop or whatever that you haven't done in a while, it often saves time to find a very similar loop that you or someone else coded last month or last year.

So, what could possibly be wrong with this? Well, the current example is a set of paging controls in a web application. Paging controls are those little arrows (and text) at the top of a table that has possibly more data than is reasonable to show to the user at one time. You use the controls to move from one "page" of data to another by clicking on previous, next, last, first and changing the number of rows that you want to see at one time.

This is a good example of where it is complicated enough that it is well worth the time to share the necessary code. It is also something that has been done so many times in so many languages that it is very likely that example code is available (probably in your own software system). In our system, this was the case: there were actually several implementations but none were easy to share. The reason for the title of the post is that copying a bunch of crappy code makes a mediocre system worse. This is where the level of programming experience becomes an issue. Our less experienced programmers who originally copied the code must have spent lots of time trying to make this code work without realizing that there was roughly 3 times as much code as necessary to do what was necessary. Simplifying the code, reduced the difficulty of using it which will allow it to be used in other places with a lot less effort.

This makes me wonder about the big picture issues such as project estimating, training programming skills and software development life cycle.