5.4. Communicate with Text

Text is an excellent way to communicate between systems. You do not need to worry about the homogeneity of the systems. The physical representation of primitives, such as a double, in each of two systems can be different. The text form is the lingua franca into which each system translates its primitive values. The text can be unformatted or formatted, such as comma-delimited files or XML. In addition, the text form can be read by a human tester and can easily be created to fashion data-dependent tests.[*]

[*] See The Art of UNIX Programming by Eric S. Raymond (Addison-Wesley Professional, 2003) for a discussion on text.

Text inside a system is a different matter. When text strings are read into a program, they should be converted into the corresponding data types as soon as possible. Performing this conversion separates the external presentation of the information to the user and to other programs from the internal usage of that same information. If there is an error in the representation, the failure will appear in the translation process, not later on when you are trying to process the data.

We defined the CDCategories used in the previous section with an enumeration. If the values for a CDCategory were kept in a configuration file, it might be denoted in that file as the string "GoldenOldie." When reading in that data, this string value should be converted to the corresponding enumeration value. If the input string is misspelled or does not match ...

Get Prefactoring now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.