Classify your magic numbers in the database

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...

I often see how developers do the same mistake over and over again.

This time it’s concerned with using the magic numbers in the database.

Often you get a request to realize something like:

“If a given piece of data is in positive status 1 or 101 then do this or that” or “Export all data which is in the positive status 1 or 101″

 statuses

Usually what I see as being done then is:

statuses2

Now think about what happens when the client calls you in 3 months from now asking to add another positive status?

Probably you will need to *hard code* it’s positive status in half a dozen places and then even forget about a couple of them… argh… awful.

What can be done?

Well, you can introduce your own level of abstraction - or better said - classify your statuses to avoid maintenance nightmare.

 statuses3[5]

Now you can create your “Positive response” status classification in the Status Classifications table, and connect it to the current and future statuses pretty easily through the third table StatusClassificationStatuses.

Also, then you can do a better and cleaner query with something like:

statuses4

This way your code doesn’t depend on the clients direct changes but on your abstraction - which in my book is a lot better.

As you can see this is a very simple and effective way to get yourself out of some bugs and issues in your applications.

Cheers!


Filed under: Best practices, SQL
Written on: 20 Nov 2007 ·

kick it on DotNetKicks.com

Leave a Reply