views:

82

answers:

6

Hi to all,

Basically what I need to know is this:

I have to show a drop down list of countries to my users each country also has a code associated to it. I will have to work with both the country and the code What would be the best approach:

-We (the dev.) are thinking about a table in our app database with this data, or XML file. -Our "architect" says that is old school and that we should use constants in our app with a map that associates the country with the code

Please Help me feel smart

+2  A: 

I agree with you that you should not hard code this or use constants. There are a few good options depending on yours needs:

  • Java Properties Files - If you just have a few key-value pairs to store, these are the simplest way and easy to use.

  • XML Storage - If you are looking for persistence and are looking at XML for storage, I would recommend looking at JAXB. It is part of Java 6 and will make your life easier than trying to use the DOM.

  • Database Persistence - If you have more data that is changing often, you could also look at storing it in a database. JPA is a great standard library for doing this. That is probably overkill for what you are looking for though.

Bottom line is hard coding is a thing of the past. There are lots of great ways to get data in quickly and easily without resorting to hard coding everything.

Chris Dail
Actually, the irrational fear of hardcoding is a thing of the past. Data is code, code is data, and neither is strictly "harder" than the other.
Michael Borgwardt
+1  A: 

Countries rarely change, so adding them statically as code or a config file seems reasonable. If you don't use a database for anything else, don't add one just for this feature.

If you already have XML parsing in your app, use an XML file to define the data. It already solves all kinds of issues (for example if you need to add a second attribute per country or something).

If you don't use XML for anything else, I suggest to give it a try. It doesn't add much to your app. Otherwise, use a plain text file, maybe a CSV one.

Aaron Digulla
A: 

In short your architect is wrong (Or at least he is if your paraphrase of his position is accurate). It shouldn't be in the code.

This data is not static; a country's name changes, a new one is founded, or some cease to exist.

As far as what mechanism, it doesn't necessarily matter. Just make sure you can retrieve the data easily, that you have unit tests, and that there is straightforward mechanism to update the data.

jabbie
So what if the data is not static; neither is the code.
Michael Borgwardt
@Michael Borgwardt when you tell your boss that in order to start shipping to customers in WhereverStan, or fix the spelling of Myanmar that you need to redeploy the application. That is when you realize that you should have separated out this information from the code.
jabbie
So what if I have to redeploy the app? For a webapp that is a press of a button. And if I separate the "information" from the "code" I *still* have to redeploy unless I keep it in a server and the app has to keep asking for it (and cache it somehow). If you already have that infrastructure in the form of a database, that's fine. If not, introducing it for this purpose would be absurd.
Michael Borgwardt
A: 

I think that "table solution" has more flexible approach: 1. You can manage data and connecting properties 2. You can work with table directly 3. You can create associated map, based on db table))

ziftech
A: 

I would certainly not use them as constants in the code. Names can change, while countries can be created, merge, disappear, etc.

If you are already using a database, adding this may make sense. For one, it ensures that the codes that may be stored with client data are valid in terms of your country code list. So if a country disappears but a client record still refers to it, the data stays valid.

Make sure that your UI loads and caches the list; no point making a query every time if you can avoid it.

By the way, correctly handling countries in an internationalized application is much more complicated than just dealing with renames. For example, if a country or part of a country declares independence, some countries will recognize it, while others do not.

Uri
+1  A: 

The different methods have different advantages and drawbacks:

Database:

  • allows you to use the country data in queries
  • data can be changed without redeploying the app
  • editing the data requires you to write some sort of frontend or do it manually via some generic SQL browser
  • requires database access code, and some sort of caching strategy
  • Any country-based logic in the code can break when the DB changes, or has to be reflected in the DB

XML:

  • Very easy to edit
  • can be changed without recompiling the app, but changes have to be deployed somehow
  • Requires parsing code and some sort of caching strategy
  • Any country-based logic in the code can break when the XML changes, or has to be reflected in the XML

Code:

  • Easy to edit - for developers
  • Changes require compilation and deployment
  • Requires no extra technical layers
  • Code and country data can't get out of synch

All in all, the "code as data" solution is indeed the nicest, if the compile&deploy step for each change is acceptable to you. The other solutions create overhead and duplication of structure (or even logic) - and no, they don't magically make it "safe" to do last-minute changes "because it's not code". Code is data, and data is code.

Michael Borgwardt