What is the standard approach to naming variables for a list of things that might change in order/number?

StackOverflow https://stackoverflow.com/questions/18151926

Question

Basically in my program, I have a list of questions. These questions could change, and their order could change on later releases of my program (this is why having numbers in the variable names would be bad).

What is the standard approach to naming the variables that will hold these questions' answers; ie, what's the typical nomenclature?

As of right now, I'm planning on having them named as the questions themselves (that is, the variable is a basically a sentence lol); however, some of the questions are pretty long so the variables would be long as well, so it looks pretty ridiculous. I see this not really being a big problem, unless we change the wording of the questions.

I just want to keep this code professional and robust so that when I go back to it in the future, I know where things are and it is easy to understand/adjust.

(by the way, I guess I should say this, I'm aware that the names of the variables have no effect on the program lol, I just want to know the best way to name these variables to keep the code clean, efficient, and adjustable)

Thanks!

Edit: Also, I'll mention, These "answer" variables are all properties of a class I made called Answers, so that is how I currently have it organized. Let me know if its better to put them in some other type of data structure that's more fitting, like a dictionary or something. (I'm coding in Objective-C)

Edit: Also, technically I could number the answers to the questions and refactor them in the future if need be... would this cause any unforeseen problems?

Was it helpful?

Solution

Define a class called Question

@interface Question : NSObject

@property (readonly, copy) NSString* question;
@property (readonly, copy) NSString* answer;

-(id) initWithQuestion: (NSString*) question answer: (NSString*) answer;

@end

I'll leave the implementation to you :)

Store them in an array

NSArray* questions = @[ [[Question alloc] initWithQuestion: @"What is the airspeed velocity of an unladen swallow?" 
                                                    answer: @"what do you mean? African or European?"],
                        [[Question alloc] initWithQuestion: @"What is the largest fish in the sea"
                                                    answer: @"Blue whale, no really, it is a fish as are all mammals"]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top