Characters and Strings

This section provides some tips for using strings. The first applies to strings in all languages.

Cross-Reference

Issues for using magic characters and strings are similar to those for magic numbers discussed in Numbers in General.

Avoid magic characters and strings Magic characters are literal characters (such as ‘A’) and magic strings are literal strings (such as "Gigamatic Accounting Program") that appear throughout a program. If you program in a language that supports the use of named constants, use them instead. Otherwise, use global variables. Several reasons for avoiding literal strings exist:

  • For commonly occurring strings like the name of your program, command names, report titles, and so on, you might at some point ...

Get Code Complete, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.