Information Representation and Transformation

Perhaps the greatest hardship for new programmers is the ability to quickly grasp how to represent information to describe a "phenomenon" in a computer. In reality the choices are very limited (numbers, strings, booleans, arrays, and objects/structures). In practice choosing the right representation is key to making working programs. One of the key attributes of a "Good" representation, is the ease in which it is transformed into other valid information the computer can process.

Information Representation and Transformation

Lets start with an example. Is the representation for my age better as:

  1. a String

    "twenty five"

  2. a whole number (25)

  3. a floating point number (25.62)

  4. or even as a birthdate:

    (born.month = "January", = 25, born.year = 1990)

The answer lies in two questions:

  1. What are you (the programmer) going to do with the information?

  2. How easy is it for you (actually for the computer) to transform this information into something else?

For the above example, if you are just going to print the information for the user to read, then example 1 is probably fine. But, if you are going to determine, for example, how many birthday's I have had (the number 25) then turning the string "twenty five" into the number 25 is non trivial... and in fact option 2 would be better. On the third hand, if you want to know how many days I've been alive, option 3 or 4 are probably much better. Finally, if you need to know when to celebrate my birthday, as well as how old I am, option 4 is the best.

Remember, computers can do math really well, but don't do very well with human readable information (strings).

Computatoin vs. Storage

Often as a programmer, you will have the option of storing or computing the same piece of information.

Consider the idea that you need to turn 90 degrees to your right. 90 degrees is how most of us think about degrees, but computers and mathematicians think about angles in radians.

In the big schemed of thing THEY ARE THE SAME THING! Radians mean degrees, degrees mean radians... It's just like saying "hello" in English and "Bonjour" in French. THEY ARE THE SAME THING.

So, if we need to use some math routine with radians, but ask the user for a number in degrees, what should we do? The answer comes down to two options:

  1. Store both!
  2. Store only one, and then compute one from the other

The correct answer is almost always the latter (compute one from the other).

Transformations and Modifications

Computer programs are all about modifying current values of data and transforming this data from one form to another.

Back to Topics List