Dereferencing

From Director Online Wiki
Jump to: navigation, search

Dereferencing is the process of finding an object within a hierarchy of container objects, or parents.

For example, you might access the front right wheel of a "car" object like so:

   wheel = car.frontAxle.wheels.right

In this example, each dot? represents an object dereference. The wheel object "right" is a sub-object of the container, "wheels", which is a sub-object of the axle, "frontAxle", which is a sub-object of our car.

Every time you dereference something Director has to look up a table of references in the parent object, like looking up a number in a phone book. This takes some time to process, so if I wanted to perform a lot of operations on a particular wheel, I would write:

   wheel = car.frontAxle.wheels.right
   wheel.inflate()
   wheel.checkPressure()
   wheel.checkTreads()

Due to the number of dereferences involved, this is much more efficient than writing it out longhand:

   car.frontAxle.wheels.right.inflate()
   car.frontAxle.wheels.right.checkPressure()
   car.frontAxle.wheels.right.checkTreads()

As you can see, the former is also cleaner to read.

It is particularly important to create a temporary variable in this way if it is to be used inside a repeat loop.