When code becomes a database

I saw some code recently that made me think “that person has lost their mind”; it was a class with a public enumeration in it with 15,000 (yes fifteen thousand) members. I noticed it when using resharper; mostly it takes a few seconds to open a solution when it works through all the files, but in this case it gets hung up for minutes. My first reaction is that this was mad, but I did at least try to understand why they had done it.

The enumerations were clearly imported in bulk from some external file (they were Bloomberg terminal commands). My first reaction is that this was not code, it was a database, and should be stored in a database. Any claim that the enumeration could be used for Intellisense is clearly rubbish as there are so many members that you need to know the thing you are looking for, thus eliminating the purpose of having the enumeration prompt you with the right options. I’m not really sure what it is good for. Of course, as soon as things go anywhere but in code, then we are in some sort of late-bound, text-based nightmare when we are just tossing strings around and hope we have the right string and we won’t know until runtime whether things will work. So, damned if you do, and damned if you don’t.

Obviously all the members aren’t used, but maybe having them all in one place is useful, so where should they be? In a relational database? Seems a little excessive to put them in a database as a) the data isn’t really relational, it is just a list and b) all the other features of a relational database – shared access, powerful query and update language, centralised management – don’t really seem that useful. The really key feature of a database is that it stores data. What is data? Well, one way to look at it is that it isn’t logic. If the logic is the same for all the inputs then the inputs are data. So these commands aren’t really data, as they all do different things that mean we get different results.

So if it is not data, what is it and where else could it be. In an XML file? Is that list of commands “config” for my program? I have some logic and I want to issue a certain command and so I store the command in a config file. Of course, normally one would store only the commands that you needed, and tightly bind the config file to the logic so we’d be more hopeful that we wouldn’t get run time errors when logic and config didn’t connect.

In general, I’d say that we have some sort of hierarchy that goes from code to config to database. We expect that order to work for size and changeability. If the enumeration of 15,000 is huge but never changes is it code or database? I don’t know but it is definitely a hack.


2 thoughts on “When code becomes a database”

  1. My preference would have been to use a simple csv file to store the various elements of the dictionary and use a class to load them. Then instead of having:
    Field myField = new Field(“this_bloomberg_field”)
    you could do a
    Field myField = FieldDictionary.GetField(“this_bloomberg_field”)
    which would check that there is a corresponding field in the csv file.

    Not perfect and not intelisense friendly, however, you can still have a quick feedback if the field doesn’t exist… Unfortunately, I also believe that in your case, propagating the change would be a nightmare…

  2. I agree that something that breaks early is almost as good as a compile time error. Also, something like what you suggest can throw a clear exception that will leave no doubt as to the cause of the exception.

    And you are right; any person who makes an enumeration of 15,000 members is unlikely to have well structured code that will be easy to refactor!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s