Variables and Literals

In nearly every program, you will have occasion to manipulate or use some value that will change each time the program runs. For instance, in your ball:kick verb, you couldn't tell ahead of time who would be typing the command. Thus you had to use the variable "player" to represent whoever typed the command.

"player" and "this" are both examples of builtin variables: they come free with each program, and you don't need to define them yourself. For other variables, you must give them your own name and tell the program what value to put into them.

Naming Variables

For the most part, variables can be named whatever you want. However, there are some rules and principles to keep in mind.


  1. Variable names can only contain letters, numbers, and the underscore (_).
  2. Variable names cannot begin with a number.

Good Ideas

  1. Variable names should be easy to remember, and relevant to what you're doing. "nori", "ori", "oin", and "gloin" are not good variable names; they could be anything.
  2. You should keep variable names short, but different enough that you won't confuse them. "x1", "x2", "x3", and "x4" are not good names for four completely different values.

Declaring and Assigning Variables

Variables in MOO are not "declared" as such; they come into existence by assigning a value to them. Creating a variable is accomplished with a statement like the following:

number = 4;

"number" is now an integer variable containing the value 4. It can be used anywhere that the integer 4 can be used. For instance:

result = 20 / number;

After this statement, the variable "result" will contain the integer 5. Any variable type can be assigned in this manner.

hello_string = "Hello!";

These variables exist from the point where they are assigned until the program ends.


Literals are actual values used in code. For instance, consider this statement again:

result = 20 / number;

In this case, "result" and "number" are both variables, but 20 is a literal. Variables can potentially be any value, but there isn't any confusion about the value of 20.

It is often a bad idea to use literals, especially if you're using the same value for the same purpose in more than one location. Consider a program where you wanted to calculate the daily and monthly wages for an employee earning $9/hour:

wage_per_day = 8 * 9;
wage_per_week = 40 * 9;

One thing should be immediately obvious: it's unclear what literals mean. In the first statement, is 8 the number of hours, or the wage? You know which is which because you're the programmer, but if anyone else reads it, or if you come back to it after a month, will you remember?

The other issue is, what if the employee got a raise to $10/hour? It is easy to change 9 to 10 in two lines of code, but what if it was part of a hundred-line program?

The following statements would be a better way to accomplish the same thing:

hourly_wage = 9;
hours_per_day = 8;
hours_per_week = 40;
wage_per_day = hours_per_day * hourly_wage;
wage_per_week = hours_per_week * hourly_wage;

In this case, it is clear what the last two statements are accomplishing. Also, if the employee gets that raise, you only have to change 9 to 10 in the first line, and this will update the "hourly_wage" variable throughout the program.

In conclusion, variables are easy to use. Don't be afraid to use them. Avoid literals if you can.