\section{Compile Time Checking of the Law}

It would be helpful to be able to check for observance of the Law of Demeter
at compile time.  This checking requires that the class dictionary for the
application be available at compile time since it contains all the necessary
type information.  The compile time check requires that all the arguments and
return values of methods be typed.  This can be done at compile time using 
information from the class dictionary as follows:

\begin{itemize}

\item
Determine the possible return types for all of the methods in the application.
This can be ascertained by inspection of the methods themselves.

\item
Construct a graph containing the potential origins of all method arguments.
This will give us a list of potential types for each argument.

\item
Assign a type list to the first argument of each message send.  The first
argument in a message send will either be an argument of the method, an
instance variable of the class to which the method is attached, an inherited
instance variable of the class to which the method is attached, a global
variable (really an argument to the method in a convenient form), or
a local variable.  The types of all these can be ascertained at compile time.
The instance variable types are found in the class dictionary, the
types of the arguments are found in the graph constructed previously, and
the types of global and local variables are available when they were created.

\item
The actual test if the application adheres to the constraints of the Law
of Demeter is trivial.  Simple compare the type of the first argument in each
message send to the argument and instance variable types. If it is not in
that list of types then the Law has been violated.  The Strong Law can be
enforced by not including the inherited instance variable types in the list
of legal types.

\end{itemize}

There are several cases which cannot be checked at compile time.  These cases
do not occur in some languages.

\begin{itemize}

\item
Class and/or Method Definitions at Run Time

This occurs in some interpreted languages such as Lisp/Flavors and CLOS.
Since these are defined at run time it is impossible to check them for 
consistency with the Law of Demeter at compile time.

\item
Passing a Message as an Argument

The following example demonstrates the passing of a method as an argument to
another method.  In the receiver method, the passed message is sent to an 
object ``o''.

\begin{verbatim}

   (defmethod (X :xyz) (m)
      (send o m))

\end{verbatim}

Since we have computed the signatures of all the methods (i.e. the argument and
return types) it is possible to perform the compile time check by examining all
possible signatures of ``m'' and testing for Law violations against the object
``o''.

\item
Dynamic Message Name Calculation

The following example demonstrates a case which cannot be tested at compile
time.

\begin{verbatim}

   (send (send o (concat ': some-string)) ':m)

\end{verbatim}

The string named ``some-string'' could be read in at run time in which case
there is no way of knowing the signature of the message sent to ``o''.  
If the user places a restriction on the signature of ``(concat ': some-string)''
then a compile time check could be implemented.

\end{itemize}





