Names Design Issues

  1. Length

    • Too short cannot be connotative
  2. Special characters

    • PHP: all variables names must begin with $
    • Perl: all veriables names begin with special characters ($, @, %) that specify the variable’s type
  3. Case sensitivity

    • C, C++, Java, C#, Perl, Python, Ruby, Swift: sensitivity
    • VBA, Pascal, COBOL: not sensitivity
    • Prolog, Haskell, Go: the cases carry semantics
    • PHP: variable names are sensitivity but function names are not
    • metrics:
      • doge - variable
      • Doge - class
      • DOGE - constant
  4. Special words

    • increase the readability
    • most of them are reserved words (keywords) that cannot be redefined
    • Fortran can be redefined, it is unreadable for some cases
      1
      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
    5
    int 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
    13
    void 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 int is between [-32768, 32767]

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:

    1. Language design time

      • bind operator symbols to operations
    2. Language implementation time

      • bind floating point type to a representation
    3. Complie time

      • bind a variable to a type in C or Java
    4. Load time

      • bind a C or C++ static variable to a memory cell
    5. Runtime

      • bind a nonstatic local variable to a memory cell
  • Noted:

    • 1 - 4 is static, which binds before the runtime
    • 5 is 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
    2
    int 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
    2
    list = [2, 4.33, 6, 8]
    list = 17.3
  • Evaluation:

    • Advantages:
      1. flexibility (code generic)
    • Disadvantages:
      1. high cost
      2. type error detection by compoler is difficult

Lifetime

  • Storage Binding

    1. Allocation:

      • getting an memory cell from some pool (stack/heap) of available cells
    2. 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
    7
    int 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:

      1. Efficiency (direct addressing)
      2. History-sensitive subprogram support
    • Disadvantages:

      1. Lack of flexibility (cannot use for recursion)
      2. 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
    9
    int 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:

      1. Can use for recursion
      2. Conversation of storage
    • Disadvantages:

      1. Overhead of allocation and deallocation
      2. 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
    9
    int 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:

      1. Flexibility
      2. Storage efficiency (Dynamic storage management)
    • Disadvantages:

      1. Time-inefficient
      2. 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:

      1. Flexibility
    • Disadvantages:

      1. Inefficient
      2. Loss of error detection