I am currently working on a rails project and have a requirement where I want to be able to map a country name's initial letters to its ISD code. eg: IND => 91, USA => 1, etc.

The broader question here being (notwithstanding which framework you work on), what should be the best practice to store these things. Should you be using a property file (yaml, in this case)? OR Should you just make a table in the database and read it from there? OR Should you just have a hash_map like structure in your ruby (or any other language) code?


It depends partially on your requirements:

  • If the configuration is supplied/updated by third parties (e.g. multilingual text resources that translation agencies work on), use whatever they can use easily. If you can choose the format, a very simply text format like YAML or Java Properties is best (but code that consists just of constant declarations could also work).
  • If you need to be able to update the data while the application is running and via a UI (i.e. admin section of the app), a DB table may be best.
  • If the configuration is complex (has conditions, hierarchies, overrides, etc.), that's really programming logic and should be done in code. Avoid soft coding.

If none of these is the case, just use what is most popular in your language/framework/community/ecosystem, so other developers will be familiar with it. For Rails projects that seems to be YAML, for Java it's .properties files, etc.

| improve this answer | |

As you mentioned "any other language", I would've created a utility class in JAVA with a property object containing country names and codes as key,value pairs.

The reason I mentioned a utility class is, it can be used to store other things like these (for example mapping language files with countries for localization stuff, etc). Simply I mean having a utility class with globally required stuff is a better option.

This is just my opinion in JAVA.

ADDED : By using an externalized property file, you just have to add/remove entries in the properties file without having to change your program code. Gives lot of flexibility.

| improve this answer | |
  • up vote 0 down vote if changing program code is harder than changing a properties file' you're doing something wrong. it's code that has the most flexibility. – Michael Borgwardt Jan 31 '13 at 6:39
  • 1
    @MichaelBorgwardt: Take for example, a new country is added a year after the code is written. Do you think its easy to 1) Add an entry in the properties file or 2) change the code, compile, deploy, etc. Which one do you prefer ? I would say "code becomes more flexible by using an externalized property file" – Vivekanand S V Jan 31 '13 at 6:43
  • 1
    I reiterate: if "compile, deploy, etc." is scary and something you avoid, you're doing something wrong. It should be a single press of a button, and your case 2 exactly as easy as case 1. – Michael Borgwardt Jan 31 '13 at 8:39
  • @MichaelBorgwardt I am talking about the number of steps one needs to do to achieve a simple task. Its definitely not scary, adding a line of text in a normal text file is far more easier than changing the code (no matter how good one writes the code) – Vivekanand S V Jan 31 '13 at 8:46
  • 1
    @MichaelBorgwardt I have a product which allows user-defined parameters (for example connection properties in the database connection url). In this case I can simple ask the customer using the product to add/append their properties in a plain text file instead of changing the code and asking the customer to upgrade the product or apply a patch, etc. – Vivekanand S V Jan 31 '13 at 8:59

If you have external property file, you also have the advantage that you can easily look up the configuration and you do not have to start the actual program.

This can be quite handy if you maintain more than one instance of you program and have to look up quickly, how everything is configured. You can just check the Config-Files and see, if you spot an error there, change some Settings (e.g. a database-connectionstring) etc.

Another advantage is that you can back-up these settings easyily.

| improve this answer | |
  • The 2 points contradicts each other "you can easily look up the configuration and you do not have to start the actual program" and "This can be quite handy if you manage more than one instance of you program and have to look up quickly". – Vivekanand S V Jan 31 '13 at 8:30
  • Thanks for pointing that out. I hope my edit made it clearer. – mhr Jan 31 '13 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.