Term of the Week: Variables
As a recovering math major, variables still make me smile. But knowing that most of the world, including many programmers, wasn't real fond of algebra and probably saw calculus as a curse, it's probably no comfort if I explain variables in programming by saying that they aren't any more complicated than variables in algebra.
In programming languages, variables are like a storage place for different pieces of data. You might have one variable for a person's name, another for their phone number, and yet another for their address. When the program runs and requests that information from the user, the computer will allot some storage for each variable and store each item the user enters there. Or conversely, if the program is retrieving data to display to the user, each item it retrieves needs to have a variable to hold it for the process that outputs it to the user.
While in mathematics, we're probably most familiar with the variable x—and its close companions y and z—most programmers find it good practice to give their variables names that make it easier to follow what the purpose of the variable is. So, phone is a better variable name for a phone number than x. Likewise, it's best to be the most specific when possible with firstname and lastname being better choices than just name for a person's name. You want to make your variable names long enough to be unique and identifiable, but short enough not to waste memory and typing time. Some advanced development tools such as Visual Studio have automated shortcuts and tips for entering variable names, but if you have long similar variable names, even those shortcuts take a few extra keystrokes to implement.
All programming languages set some limits on what characters are legal for variable names. You can't use a built-in keyword from the language. Some languages are more specific than others in their naming requirements; some languages go so far as to determine the variable type by the name you give it. Other languages have looser rules that are just general conventions that the language developers suggest. If you don't follow the conventions, though, other developers may have trouble working with your code. After you have written your code, if you want to obscure the meaning of your variables—to make it more difficult for someone to understand your code if the code will be public and you need to protect it—you might want to look at a technique called obfuscation.
Variable types limit the kind of information that each variable can store in your program and what operations the program can perform on the variable. Some of the most common variable types that most languages recognize are:
- String: For alphabetic characters or combinations of alpha and numeric. For example, a person's name would use a string, as would Canadian zip codes that are combinations of letters and numbers. Some of the operations you can perform with a string are things such as parsing it (breaking it down into smaller strings) or concatenating it (joining two or more strings together), but it wouldn't make sense to add strings.
- Integers: Numbers such as 7 or 645 that are whole numbers. Integers (and floating point) variable types are ones you commonly perform some math operations on.
- Floating Point: This refers to non-whole numbers such as 3.14159 or 362.71 or 0.003. Not only are there numbers before and after the decimal, but the variable can hold numbers with different amounts of numbers before and after the decimal.
- Boolean: Used in logic operations, this can only have the values true or false. Boolean operations wouldn't include either parsing or mathematical operations; all you can generally do with these is examine them to see "is it true or is it false?"
The length allowed by your language for strings, integers, and floating points will vary from language to language, and sometimes even will depend on the type of system you run the language on. Some languages might allow longer variables on systems with 64-bit processors than on 32-bit systems, for example. Most frequently used languages (C, C++, Java, VB, and so on) also will define several much more specific variable types. There can be different integer types that allow different lengths, string types that accept different character sets, and so on. So, to learn the exact variable types, uses, and limitations for your language, you'll need to consult a reference specific to your language.