Krista's Coding Corner


Data types, why, oh why

In most of programming languages we have different kind of data types (actually in all but they are not always visible). These are the weird definitions before every data mound the computer will be processing. Here is an example of a code:

int amount_of_apples;
double price_of_a_candy_bar;
boolean was_it_true;
string girls_name;

So, int, double, boolean, string are all data types. They define what kind of data the variable (or something else) has in it, like we could define whether the basket has apples or screws in it. Mostly and simplified: we can have numbers, letters or boolean values (is something true or false).

We need these definitions because how else the computer would know that your clip of text is actually boolean and not just some letters ("true" vs. "t", "r", "u", "e")? Or whether we can calculate with the clip or not. (Note: we have for example hexadecimals that include letters in numbers). This is same thing as we have with double entendres.

An example: “kids make nutritious snacks” . The meaning most likely was that kids are doing nutritious snacks, not that they are snacks themselves. But the computer wouldn’t see the difference and it definitely won’t use common sense.

This probably is relatively easy so far. You just need to accept that computers are quite stupid and they can’t see / guess what you really want from them. They need fool proof explanations and instructions.

But why there are so many different number data types? We have int, double, long int, unsigned int and so on. There reason is quite brutal: space. Even thou capacity is getting cheaper and cheaper, there still are situations where it is advisable to use as little space / memory as possible. And it is good practice to use as little resources as possible even you could afford more (let’s save the planet and so on :P).

If you transfer data or use some tiny processors (in embedded systems specially), the space /memory / bandwidth is very limited. Then you will definitely see the difference between using 10 or 10.0000000000000000000000000000000000000000000000000000000000000000000000000. If there is no need for long decimal numbers, don’t use them.

Also the data type tells others what the program wants / expects to get. It makes many things easier and faster to understand if you know that the input number is something like 1, 2, 3… and you don’t have to think about the possibility that it would be -30,0375037u503750847038574098753 (or “guineapigswithcustard”). For example some calculations do get a quite different levels of difficulties if you can put any numbers to the formula, and it can also affect the result because some roundings.

PS. And whether you use 32-bit or 64-bit system the size of your integer number is different! Don’t fix the size because it will create problems later on if the system changes. There is a lot of problem with this as people think that memory pointer is exactly 4 bytes, which is true only with 32-bit systems.

blog comments powered by Disqus