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?
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: