Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I was working on a programming project. I came across a problem where I had to store data in two different variables. The first one was a piece of data, and the second one was, well, another piece of data, and the order in which the information arrived really didn't matter.

So I had to make a choice: I could have either named them city1 and city2, or I could've named them firstCity and secondCity.

With the issue of time in mind, I simply decided to stick to city1 and city2.

But, what would've been a better good choice, or at least what could I have done instead?

share|improve this question
What programming language? – Jonathan Khoo Apr 14 '11 at 4:30
@Jonathan Khoo: It was C++. – skizeey Apr 14 '11 at 4:32
I'm glad to see that you're learning by searching for 'better' ways. – Jonathan Khoo Apr 14 '11 at 4:47
@Jonathan Khoo: Yes, I'm also aware that in programming, there are more than one right answer to a problem. – skizeey Apr 14 '11 at 5:15
up vote 3 down vote accepted

Use an array container to encapsulate (group) information about the cities together. However, the ideal choice is dependent on the programming language.

Edit: As the OP has disclosed he is using C++, I recommend using a vector.

std::vector<City> cities;

Use the push_back method to insert new cities. And access via the following:

cities[0]; // equivalent to city1 and firstCity in the example
cities[1]; // equivalent to city2 and secondCity in the example

If there are only two cities and they were somewhat related to each other, consider using std::pair.

share|improve this answer
The C++ vector class didn't even cross my mind when I was working on the problem. Thanks for that! I'll remember next time. – skizeey Apr 14 '11 at 4:46

It is a matter of personal preference but if the order of the cities had no bearing on anything else in the flow then city1, city2 works fine.

Normally, however the order of the cities has special significance, in which case, you should use properly named variables. Sometimes the number of cities can vary, in which case you would use a List<> or whatever construct your programming language has to allow you build a dynamic list of values.

share|improve this answer
The ACTUAL problem involved inputing the names of two cities from the user. Then, I had to calculate the distance between the two using a function that took in two parameters. The user could've typed in Vancouver first, then New York, or New York first, then Vancouver, the result would've been the same, hence that's why I felt that they weren't different. But hey, thanks for the answer. – skizeey Apr 14 '11 at 4:41

There really is no difference so long as you are able to differentiate between them in the code easily. It is best to name variables in such a way that you will not confuse them when reading over your code in the future. You can use an array, you can use a list, you can use individual variables, but those choices are based off of how you will need to access the variables in the code and the language being utilized. The actual naming convention is all based on preference of the coder or team of coders to make the code easy to read and understand.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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