PFPL Chapter 4: Static and Dynamic Processing

Here are some examples of static and dynamic processing:

  • All languages include some kind of syntax error checking in static processing. For example, in Python it's an error to say 2 print) 17.$ because this doesn't even make sense!

  • All languages keep track of the values of variables as part of dynamic processing. Some languages, such as Java and Ruby, keep track of the type of each value. Others, such as C, do not.

  • C/C++: static processing is compile errors such as not allowing an int to be passed to strlen because strlen requires a char *. You see dynamic processing in action when you accidentally pass a null pointer to strlen and the program crashes. C++ also has extra type-related dynamic processing features such as "run time type information" (RTTI), which lets you find out limited information about a value at runtime, and dynamic_cast, which lets you tell the compiler that a value is a more specific type than it can infer (that's static processing), but you might get back a null pointer at runtime (that's dynamic processing).

  • Javascript hardly has any static processing at all aside from basic syntax checking, although in strict mode, it does throw an error if you declare a function with duplicate variable names, as in function f(x, x) { ... }. Dynamic type errors generally manifest themselves as errors like "TypeError: Cannot read property 'name' of undefined". Even accessing member objects is a runtime (dynamic) feature in Javascript, so you can do something like this:

        // Either "Hello, John" or "Hello, John Smith"
        var field = polite ? "fullName" : "firstName";
        return "Hello, " + user[field];
  • Java: Like C/C++, static checking includes compile errors when you try to pass an int to a function that wants a String. It also goes beyond that with generics - so it's an error to pass an ArrayList<String> where a List<User> is wanted. Java also has a lot of dynamic checking: you can find out the exact type of an object at runtime, as well as the names and types of its methods and fields.