Raleigh NC, USA
support@misfittech.net
lab1_dark
Code Bug – Multiple Copies
Home » Uncategorized  »  Code Bug – Multiple Copies

I was describing a bug in some code I was working on to a friend and he made the comment that he has found this bug in several places with his team and project. Therefore I figure it is a good thing to discuss.

Multiple Copies of Data

The bug I was having was one where the designer had taken the hard path to make his work more difficult while perceiving he was taking the easy path. Specifically this code loaded a structure of data from a database. For example we will assume the structure is a person's name and address.

What the code was doing was providing a user interface for editing the person's information. So the user could create a new person and add data, change someones data, the save and delete. Additionally a group of persons could be added to a list.

Mistake #1

The first mistake in the code was to assume the primary id from the database was unique. The primary id is unique, except when you create a new person that has not been saved to the database. So to fix this problem the developer assigned the new person an id of zero. Which did not exist in the database. Problem solved!

Mistake #2

Since the new person had id of zero which did not exist in the database life was good, until you created a second person who had id of zero. Now you had two people with the same id. This was not bad but the code used the id for unique keys in drop down menus and such as such with two objects having same id things started breaking.

Mistake #3

The developer after being presented with the latest bug said he would fix it. So he decided to use the number of milliseconds since 1970 as the id for new person. This way if you create two new people they will have different ids. Problem solved!

Mistake #4

Then the problem became when the person was saved to the database it would assign the person a new id. So you would hit save and then the drop down menus stopped working as the id of the milliseconds no longer existed in the data.

Mistake #5

So seeing this mistake the developer had the idea that he would keep the previous copy of the person with the millisecond id and not update the id when saved to database. This of course created a bug when you changed the data for that person and saved it would not know it had been saved before and so it created a new person in database.

Mistake #6

Still trying to work around these bugs the developer decided that the lists containing people should then have their own copy. So now he had multiple copies of the same person and when he updated one, the others did not update.

As you can quickly see the code was a hack on top of a hack. The reason developer gave for this was that "We are just prototyping and it does not have to be perfect." Therefore each time a bug came up with customer he was hacking what he thought was a quick easy fix on top of his bad architecture design. Which is the heart of the problem. That is he did not take the time to architect a real solution.

By the time he was done hacking the customer had lost confidence in the project. Not sure how much was this bug verses other issues, however the result was a bunch of code in the trash pile. Where if he had taken the time to architect a good solution than many parts of that code could be reused for years to come.

The net result is that when you see code saving the same data in multiple places (arrays) then it is a big red flag there is a bug. That is you can update data in one place and not in the other. So always try to keep one copy of the data and let everyone reference that one copy.

The developer in this story could have created a global unique identifier, GUID, which he could use to reference each person. Then his arrays of people could have been arrays of GUID, which referenced back to master list of persons. Doing this if the primary ID changed it would be ok as the GUID would not.

Leave a Reply

Your email address will not be published. Required fields are marked *