Names
Before thinking about variables, let’s think about names in general.
- What else (other than variables) gets named in a programming language?
-
The term identifier is often used interchangeably with name.
- Exist primarily for our convenience.
- Computers convert names into numbers – e.g., memory addresses
- Names were one of the first high-level features added as programming languages evolved from assembly.
- Key questions when designing a programming language:
- Which characters are allowed
- Can names begin with a number?
- Case sensitive?
- Is there a maximum length?
- Are any words prohibited (e.g., keywords)?
Consider the following
Integer Num_cars
Integer = 42
- What do you think about using keywords for variable names?
- What about allowing a name to be both a function and a variable?
Variables
- Not just (name, value).
- Completely described by six attributes What are they?
- name
- location / address
- value
- type
- lifetime
- scope
- How can a variable have different locations/addresses?
- A local variable gets a different address (position on the stack) each time the function is called.
- What is “type”? What does it determine?
- The range of values that can be held
- The operations that may be performed.
- Give an example of a “typeless” language. (What does that even mean?) What are the advantages and disadvantages?
- What is an “Alias”?
- How are aliases useful?
- How aliases they potentially harmful?
Binding
- “Binding” is associating a name/symbol with a specific value
- name -> value
- name -> function
- variable name -> address
- symbol -> underlying operation / code
- “Binding time” is when this association is fixed.
- Binding time may initially appear trivial; but, is important to understanding semantic behavior
- Changes in binding time can provide insight into a language’s high-level approach / perspective.
- In general, bindings can be “static” or “dynamic”
- “static” means fixed before the program begins running and doesn’t change.
- “dynamic” means can change during program execution.
- Possible binding times think of an example for each.
- language design time (e.g., binding “+” to integer addition, or binding “*” to pointer dereferencing)
- language implementation time (e.g., specific bit width of
int
in C) - compile time (associating a variable with a type in C)
- load/link time (associating variables with memory locations; associating function names with memory locations)
- run time (giving a variable a name)
- Binding rules are language dependant. For example,
- variable -> type bindings are permanent in C (i.e., can’t be changed once bound); but,
- variable -> type bindings can change in JavaScript.
- Advantages of being able to change a variable’s type?
- Flexibility in choosing names
- Ability to handle collections of mixed data
- Disadvantages of being able to change a variable’s type?
- Compiler can’t detect mistakes / typos.
- Assume
x
contains anint
andz
contains aString
. Compiler can’t detect thatx = z
is a typo if we are allowed to change type ofx
. - Performance is lower because each variable access must be double-checked for type.
- Type bindings can be either explicit or implicit. However, implicit does not imply dynamic.
var x = 14
implicitly makesx
anint
in C#; but, this binding cannot be changed later.
- Discuss explicit vs. implicit typing in terms of readability, writability, reliability, etc.