Take away that man’s copy and paste

dctr

Looking for the perfect gift for a programmer or software engineer?  Try Design, Code, Test, Repeat.  It’s a fun, funny, and helpful read.

Now, on to today’s post.  I recently started working on a new project which involves modifying a large application.  After talking with the boss about what needs to be done, the first task became obvious.  The large application needed to get smaller.

To give you an idea of he horrors in this project, let’s start with the UI code.  There are three types of interactions you can have with the UI and they all look similar, but with slight modifications.  This UI is essentially duplicated for two units which you are interacting with.  So, what’s the best way to handle this?  Well, any normal programmer would write a bunch of methods or subroutines and divide the work, reusing the common code and making some exceptions here or there.  Or you might want to create a few classes and use some object-oriented way of differentiating the mechanics.

But, no.  Here’s what our hero did:  he copied all of the code.  So now, there are five extra copies of a ton of code that need to be maintained and modified for future work.

Next, there is a massive amount of data that is read from a database, put into an in-memory structure, and then set in the UI.  When the user wants to save, the data is read from the UI, put back in the in-memory data structures, then written back to the database.  There are hundreds of possible pieces of data to read and write.  Is any of this work done with a clever class or technique that uses a lookup table(s) or some other maintainable/enhanceable manner?  You already know that the answer is: no.  It’s all done one piece of data at a time, copied five more times.

I won’t tell you that I haven’t copied code; of course I have.  But, all of us have our tolerance for doing this.  Every time I copy a small section of code, I think, is this the time to add a method or class that does this job?  If it’s a large chunk of code, the answer is always yes.  If it’s just a few lines, I’m likely to say, next time for sure.

Here’s the real issue.  If you find yourself having to repeat the same procedure constantly to handle differences in names of data and their UI counterparts, it’s time to get creative.  I’ll be doing this shortly and I’ll let you know what I come up with.  Be sure it won’t be a copy and paste answer.