Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: All
May 5, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Avoid Hardcoded Literal Strings

Hardcoded literal strings can become a serious maintenance problem. Suppose you have numerous message boxes in your application that display a hardcoded text message and you want to change that message. You have to chase every occurrence of the message box in your source file. A similar problem will arise when you internationalize or localize an existing application. To avoid this problem in the first place, use constants or string tables instead of hardcoded strings. This way, you will have to change a message only in one place:

// file messages.h
const char msg1[] = "hello";
const char msg2[] = "goodbye";

// file app.cpp

void f()
 messagebox(INFO, msg1);
int main()
 messagebox(INFO, msg1);

To create a French version of this program, all you have to do is replace the constant strings in messages.h and recompile:

const char msg1[] = "bonjour";
const char msg2[] = "au revoir";

No additional changes in the source files are needed.

Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date