jellomonkey
jellomonkey

Reputation: 1964

Handling states and countries (or provinces): best way to implement

In my career I have seen literally dozens of ways that people choose to handle implementing state and country data (as in NY or USA). I've seen enumerations, multi-dimensional arrays, data driven classes from XML docs or databases, and good old-fashioned strings.

So, I'm wondering, what do people consider the "best way" to handle this very common implementation? I know that most of the answers will be based primarily on opinion and preference; but, I'm curious to hear arguments as a recent discussion for an app I'm working on has resulted in a small debate.

Upvotes: 2

Views: 1556

Answers (3)

MikeJ
MikeJ

Reputation: 14565

I would pick a standard for enumerating the geogrpahical areas and then associate keys in a table. In the past I have used FIPS 5-2 place codes. Its a federal standard and the numbers and abbreviations are reasonablly well known. for international country codes we use FIPS 10-4 for encoding places. There are some iso standards such as ISO 3166 that may also be appealing, but this is more preference and taste.

Upvotes: 1

MikeW
MikeW

Reputation: 5922

I think enums or XML is fine if all you want is a simple list of states. If you have other dependent data like say state tax rates or postcode data then I think I'd store the states in the database.

Same with countries. You may want to control languages, currencies etc. based on country. I think that's just easier to deal with if it's in a database.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755121

If you consider the number of states in the united states to be unchanging then what you have is a finite, small, fixed set of values which can easily be associated with a number. For that type of data structure an enumeration works great. My choice then would be an enumeration as they have good compile time validation and typing something like the following reads nicely.

var state = GetStateInfo(States.Nebraska);

However if you consider the number of states to be a changing value then an enumeration may not be the best choice. The framework design guidelines (link) reccomend you do not use an enumeration for a set of values which is considered to be open (changing). The reason why is that if the value does change you risk breaking already shipped code. Instead I would go with a static class and string constants for the name.

Upvotes: 2

Related Questions