Data is everywhere in programming whether we are storing it, presenting it, manipulating it, or passing it. Let's look at bits of data and hopefully learn what pieces to use and when.
Boolean: This is true (1) or false (0). Sometimes you will hear it referred to as a flag. This is probably the most powerful piece of data because everything in a computer is built on it. A great use case for a flag is if only certain people are to access different parts of the system. When that person logs into the system you could check their clearance level, and then when they go to a different part of the system have that part of the system check their clearance level, etc. This may be fast, or it might be taxing on the system depending on how nested that credential is. Another option is you could set a flag "isCleared" or something like that, and then each part of the system just check if "isCleared" is true.
Integer (including short and long): This is the next level of abstraction for a computer, and also a very powerful type of data. All booleans become numbers. 00000101 is 5 in binary. Numbers are not only great for math, but also for conveying information. Continuing with our previous example, let's say that you are wanting to check the clearance level of a person. Is it faster to check if they are a "manager", or if they are position type 7? 7 is faster to a computer because it only has the one level of abstraction to break it down. While this isn't a big deal in most modern programs, another key thing to think about is what if in your system someone misspelled "manager", or if your company decided to change the title "manager" to "executive"? If you had chosen to use an integer to represent the title, and then had a separate sheet that mapped the integer to the title, then you would just have to change the title in that one place. DBAs and developers will use this practice quite often because it is a lot faster and cleaner.
Floats (including doubles): I am not going to go into great depth on this, but a float is an number with a decimal place in it. Fun fact: banks and financial institutions don't use floats in your account. They use the smallest unit they can (in the US it is a penny/cent) to keep track of your account, then convert it into a float (dollars and cents) on the front end.
Characters (char): This is a level of abstraction above integers. Every boolean -> number -> character. Let's use the alphabet as an example. 00001 -> 1 -> A. 000010 -> 2 -> B. 010010 -> 18 -> R. ASCII is actually how these characters are represented in computers. A character can never be more than a single letter or character.
Strings: Going up another level of abstraction we get to strings which is just a fancy way of saying words. Strings are actually the first data type that in some languages isn't considered "primitive" or low level (Java would be an example of this type of language). I am including it here though since a decent amount of popular languages consider it primitive. A string is essentially just an array (more on this later) of chars strung together. It is why you can actually ask for the letter at a specific place in a word so easily in most languages (usually same syntax as you would ask for a certain item at a place in an array). Strings are great for many things, but one thing they are not good for is comparison. Think about how you would go about checking if "flimflappable" is the same word as "flimflpapable". You would start at the first letter of each word, check if they are the same, then go to the next letter in each word to check, then so on and so forth. This takes quite a bit of time.
The reason I talk a lot about levels of abstraction in this and how a computer looks at things is because at the end of the day if you need to increase the efficiency of your program or database you will want to think like a computer. If you are looking at strings, you are four levels of abstraction up and so the computer it will take longer to process. "cab" -> 'c' 'a' 'b' -> 3 1 2 -> 00000011 00000001 00000010.
In our next part we will take a look at data structures and the pros and cons of their use in programming.
Top comments (0)