5.4. Communicate with Text

 <  Day Day Up  >  

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 one of the values for the enumerations, the error can be reported immediately, instead of trying to handle it in processing later.

Creating an enumerated data type is language dependent. If a language does not support enumerations, a class with class (static) attributes initialized to different values can give the same functionality. Conversion of those values to and from strings can be part of that class. Even if a language does have primitive enumerations, creating a class with those methods might be worthwhile for creating the to/from string conversion methods . For example:

 class CDCategory         {         static String string_values [] = {             "NewRelease", "GoldenOldie", "Regular"};         static int corresponding_values [] = {              NEW_RELEASE_CD, REGULAR_CD, GOLDIE_OLDIE_CD}         static int NEW_RELEASE_CD = 0;         static int GOLDIE_OLDIE_CD = 1;         static int REGULAR_CD = 2;         String to_string(int category)             {             return string_values[category];             }         int from_string(String a_string) throws BadValueException             {             for (int i = 0; i < string_values.length; i++)                 {                 if (a_string.equals(string_values[i]))                     return corresponding_values[i];                 }             throw new BadValueException(  );             }         } 

TO TEXT OR NOT TO TEXT

Use text between programs, not within programs.


 <  Day Day Up  >  


Prefactoring
Prefactoring: Extreme Abstraction, Extreme Separation, Extreme Readability
ISBN: 0596008740
EAN: 2147483647
Year: 2005
Pages: 175
Authors: Ken Pugh

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net