I have a piece of code that parses some obscure text file.
This text file can contain various keywords. At some point, there is some lengthy part that reads like this:
void loadKeywords() {
tmpString = getValueForKeyword("width");
if (tmpString != NULL) {
/* do something for several lines */
}
tmpString = getValueForKeyword("height");
if (tmpString != NULL) {
/* do something for several lines */
}
/* and so on a few dozen times */
}
These strings "height"
and "width"
are only ever used in this very piece of code. Still, I am wondering if it would be better to use defined string constants like
#define KEYWORD_WIDTH ("width")
instead of those literals in the code above.
What would you do?
Having the constants in one place helps especially if the constants need to be changed in the future - i.e. in case of localization.
If the string constants are specific to a solution (i.e. parsing some kind of config file with well-estabilished keywords), then I'd say that introducing a const doesn't do anything - except for keeping your coding style consistent. :)
Ofc, if you use the same string constant twice, the constant gives you one big advantage: the compiler will warn you when you make a typo in constant name, but it won't if you make a typo in a repeated literal.
Start without extracting constants. Then later on, your editor can probably do this for you if you need it for some reason later on.
Though if you think it will improve the readability for your code, then you can use constants. Do this if you can add more semantic meaning by doing so:
The advantage of using constants for strings, even if they're only used once or twice, is that the compiler can check you spelled the identifier names correctly, which it can't do if you're just using string literals -- so once you've got the actual strings themselves correct, you're more likely to pick up certain types of typo at compile time. This is usually helpful (for obvious reasons) -- though sometimes it CAN be a bit annoying for whoever meets your code next, having to regularly find the definition of each constant to see what sequence of chars it actually refers to.
One recommendation I would have for C (and indeed C++) would be to use static const char arrays to hold the strings, e.g.:
This makes it easier to see in the debugger, and you're guaranteed to get only one copy of each string.
I'd use a const.*
Even if it is only used once.
I would not use a #define.
*except in a very narrowly defined set of situations where a poor compiler generates extra bytes in an extremely constrained memory environment
One advantag of using a constant symbol instead of magic number/string is that you can express semantic of the value more precisely. E.g. a string-token in your text could be "wdh". It is not obvious that it is means "width", or "token meaning width of car" for example. Using a constant you express it better:
It is only an idea.
It is a good habit to use constants with meaningful names even if they are used once in the code. If you use them more than once, you must define constants.