\section{Black boxes}

A black box is a message which takes inputs and returns outputs.

\bv
;;; C = .
(defmethod (C :m) (p :type Complex)
  (send (send p ':imaginary-part) ':square))
\end{verbatim}

In this example, imaginary-part is a message which takes as input
a Complex number and returns as output the imaginary part which
belongs to class Real. This
is a violation of the Law since class C is not allowed to
use the protocol of class Real. Observe that C is not a preferred
client of Real. C is a preferred client of class Complex only.

Violations of the Law are justified as long as the following
conditions are satisfied:

\begin{itemize}
\item
Classes which are stable can expose their structure. In the above example,
class Complex has been studied for centuries and the imaginary
part of a complex number is a stable concept.

\item
The dependencies are clearly documented. In the above example, we have to
document that
Class C depends on Real since C uses the protocol of Real.

\item
We are willing to keep messages upward compatible.
Consider:
\bv
(defmethod (C :M) (p)
  (send (send p ':F1) ':F2 self))
\end{verbatim}
If we are willing to keep F1 so that it will always return an object which
responds to F2.
\item
Efficiency: To gain efficiency we might want to violate the Law.
Consider the problem of multiplying a matrix by a vector.

Vector(n) = <elements> Array(n, Number).
Matrix(m,n) = <elements> List(Vector).


\end{itemize}

If we rewrite the above example in good style we get:


\bv
;;; C = .
(defmethod (C :m) (p :type Complex)
  (send p ':square-imaginary-part))

;;; Complex = <real-part> Real <imaginary-part> Real.
(defmethod (Complex ':square-imaginary-part)()
  (send (send self imaginary-part) ':square)
\end{verbatim}

or

\bv
;;; C = .
(defmethod (C :m) (p :type Complex)
  (send self ':square-argument (send p ':imaginary-part))

(defmethod (C :square-argument) (p-real :type Real)
  (send p-real ':square))
\end{verbatim}

Both solutions are not satisfactory: The first one makes class Complex
too ``heavy'' in that it defines an unnatural method for the class.
The second one makes class C too ``heavy''.
Therefore it is justified to violate the Law and expose the structure
of class Complex.

It is important to notice that stable classes are more the exception
than the rule. Each application needs its own classes and these classes
tend to evolve as the application evolves. Therefore the Law
of Demeter is important.

Each black box introduces a constraint for the programmer in that 
the black boxes have to be maintained in an upward compatible way.
With many black boxes around, the programmer gets forced to
write the software in inappropriate ways when changes have
to be made. With a minimal number of black boxes, the programmer
has much more freedom to keep the software optimally tuned to the
current needs of the application.


