Variables
Names Design Issues
Length
- Too short cannot be connotative
Special characters
PHP: all variables names must begin with$Perl: all veriables names begin with special characters ($,@,%) that specify the variable’s type
Case sensitivity
C,C++,Java,C#,Perl,Python,Ruby,Swift: sensitivityVBA,Pascal,COBOL: not sensitivityProlog,Haskell,Go: the cases carry semanticsPHP: variable names are sensitivity but function names are not- metrics:
doge- variableDoge- classDOGE- constant
Special words
- increase the readability
- most of them are reserved words (keywords) that cannot be redefined
Fortrancan be redefined, it is unreadable for some cases1
2
3
4
5
6
7
8
9! unreadable example
if if .eq. then then if = else else then = if endif
! the actural meaning
if (if == then) then
if = else
else
then = if
end if
Address
the memory address with which it is associated
a variable can have no name
1
2
3
4
5int main(){
int* p = new int; // the variable has no name but a pointer points to it
std::cout << "Address of the nameless variable is " << p << std::endl;
std::cout << "Address of the pointer p is " << &p << std::endl;
}
- a variable may have different addresses at different times during execution
1
2
3
4
5
6
7
8
9
10
11
12
13void f(){
int c; // local variable
}
int main(){
f();
int x = 0;
f(); // the local variable may have a different address when the function is called
for(int i=0; i<10; i++){
int d; // the variable in the body of a loop is recreated in every iteraction
}
}
Value
- the contents of the location with which the variable is associated
a = 5- l-value (left value): the address of the variable
- r-value (right value): the value of the variable
Type
- the range of value of variavle and the set of operations that are defined for values of that type
- e.g. range of value: range of
intis between [-32768, 32767]
- e.g. range of value: range of
Binding
- an association between an entity(實體) and an attribute(屬性)
- e.g. between a variable and its type or value
- e.g. between an operation and a symbol
Binding Time
the time at which a binding takes place
Possible binding time:
Language design time
- bind operator symbols to operations
Language implementation time
- bind floating point type to a representation
Complie time
- bind a variable to a type in
CorJava
- bind a variable to a type in
Load time
- bind a
CorC++static variable to a memory cell
- bind a
Runtime
- bind a nonstatic local variable to a memory cell
Noted:
1 - 4is static, which binds before the runtime5is dynamic, which binds at the runtime- We prefer that do the binding before runtime, because it is more efficiency.
Static Binding
it first occurs before run time and remains unchanged (the memory location) throughout program execution
explicit declaration顯性: program statement for declaration (with types of variable)
1
2int x = 999;
String str = "Hello World"implicit declaration隱性: default mechanism for specifying types of variables (
Fortran)1
2! i, j, k are assumed to be integers
! r, s, t are assumed to be reals
Dynamic Binding
it first occurs during execution or can change during execution of the program (
JavaScript,Python,PHP)1
2list = [2, 4.33, 6, 8]
list = 17.3Evaluation:
- Advantages:
- flexibility (code generic)
- Disadvantages:
- high cost
- type error detection by compoler is difficult
- Advantages:
Lifetime
Storage Binding
Allocation:
- getting an memory cell from some pool (stack/heap) of available cells
Deallocation:
- putting a cell back to the pool
Lifetime is the time period when it is bound to a particular memory cell (from allocated to deallocated).
Static Variables
Bound to memory cells before execution begins and remains bound to the same memory cell throughout execution
1
2
3
4
5
6
7int x = 0; // global variable (static)
static int y = 50; // global variable (this static doesn't mean static variable)
int main(){
int z = 100; // local variable
return 0;
}Evaluation:
Advantages:
- Efficiency (direct addressing)
- History-sensitive subprogram support
Disadvantages:
- Lack of flexibility (cannot use for recursion)
- No sharing of storage
Stack-Dynamic Variables
Bound to stack memory cells when the function is called and remains bound to the same memory cell until the function is returned
1
2
3
4
5
6
7
8
9int f(){
int x = 999; // stack-dynamic variable
return x;
}
int main(){
f();
f(); // has the same address (the memory cells for pervious function was removed)
}Evaluation:
Advantages:
- Can use for recursion
- Conversation of storage
Disadvantages:
- Overhead of allocation and deallocation
- Inefficient references (indirect addressing)
Explicit Heap-Dynamic Variables
- Allocated and deallocated by explicit directives, specified by the programmer, which can take effect during execution
- Nameless: can only access by pointers or references
1
2
3
4
5
6
7
8
9int main(){
// *
int* p = new int(88); // one single integer
int* a = new int[size]; // an array of size integers
// **
int** pp = &p; // pp -> p -> int(88)
Ingredient** ingredientStorage = new Ingredient*[size]; // Assignment 1
}
Evaluation:
Advantages:
- Flexibility
- Storage efficiency (Dynamic storage management)
Disadvantages:
- Time-inefficient
- Unreliable (dealing with pointers)
Implicit Heap-Dynamic Variables
Allocated and deallocated by assignment statement, specified by the programmer, which can take effect during execution
1
let arr = [1, "Hello", 1.234, "World"]
Evaluation:
Advantages:
- Flexibility
Disadvantages:
- Inefficient
- Loss of error detection


