views:

234

answers:

5

Hi!

I have a situation that may seem ridiculus but I have not been able to figure out a good enough solution. To simplify things the problem is something like this, suppose you have an object like Manufacturer that has a countryname property (like car.Manufacturer.CountryName) and you want to be sure that the countryname property can not have duplicates or misspellings or other errors.

It is basically a string property but a string can be anything which I do not want. An object seems like overkill and an enum means I have to recompile if new countries are to be added or existing countries to be changed.

I could easily control this in a GUI but I need to control this in the application code. So I have an object with a property that could be a string, an object or an enum (or other) and I cant decide which to use. So my options are something like this:

a) Control this in the GUI and do not check this in the application code, taking the risk that I can get "illegal" country names.

b) Make an object (Country) and use that, which is overkill and makes the code more complex, but I have complete control over duplicates and all that stuff.

c) Use an enum and hope that I do not have to recompile too often. It is simple and effective but a static solution.

d) Use an internal string list of valid country names and have CountryName as a string property and make sure that it is validated against that string. I get validation and CountryName is just a simple string, but what if I change that internal string of valid country names? Than I have to make code the revalidates all Manufacturer objects in the program too make sure they still have valid contry names.

I am not totally sure how important it is too have valid country names but the more I think about this the more I realize that I am in a grey zone. An object, or struct, is too much, an enum too static, a string too simple.

I could be totally overcomplicating things here but I would really like too know what to do, or rather how to think, when you get into this grey zone of object vs string vs enum.

Thankfully yours! Hal

+1  A: 

c) Use an enum and hope that I do not have to recompile too often. It is simple and effective but a static solution.

From this statement I can infer that your data can be subject to change. In these situations, I suggest using an external data store (a database, text file, XML file, whatever).

Mehrdad Afshari
A: 

If country name data integrity is important to you I'd store a list of valid country names in a database or other persistence method, query and cache them inside the application.

Then you can add one post-deployment without recompiling or changing code and with caching you don't incur a lot of overhead for the extra database query.

jfar
A: 

There might be another option:

use a list of Country names stored in an external (possibly human-readable) file. When your program starts, load the country names in an array of strings (or in some other ordered container), and use the order in the list to assign a number to each country (if you use an array, just use the index). A variable containing a country is now just an integer.

Another trick is to use a separate object (which has only one instance) to handle country names. This will ensure that all objects are using the most recent version of the list of country names. Manufacturer objects then do not have a string list, but a reference to the unique object containing the string list.

Martijn
+1  A: 

Go with the object.

It really comes down to a case of do the 'complex' work now and life is easier later on or take the easy route now and life may be more 'complex' later.

Also considering you are actually worried about all the negatives of options b, c & d then just remove your worries!

Also as others have suggested your data should be stored externally to the code (database, xml file etc...) writing an object to wrap this would be your best solution.

mundeep
A: 

I would use a dictionary stored as a separate file, like a XML property list, something like:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>UK</key> 
    <string>United Kingdom</string> 
    <key>FR</key>
    <string>France</string>
    <key>SP</key>
    <string>Spain</string>
</dict> 
</plist>
mouviciel