[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Law of good style for CLOS



Thank you for your feedback. I would like to clarify a few points
in reply to your messages. The motivation for the Law is that it 
buys you the following interrelated qualities of good 
programming style: Reduced coupling between your methods, more
information restriction, localization of types, narrow interfaces,
easier maintanance, smaller programming complexity and easier 
correctness proofs by structural induction. The details
will be in the OOPSLA paper.
Your feedback has prompted me to the following improved formulation:
----- LAW OF DEMETER FOR CLOS (object version)----------
All function calls (whether generic or not) inside a method M must have
one of the following objects passed by their first argument:
    a value of M's parameters or
    an immediate part (the value of a slot) of the first argument.
(Objects created by the method and objects in global variables
are viewed as being transmitted by arguments.)
----------------------------------------------------------------
This Law does not exclude methods such as the one given by James Kempf:
(defmethod display ((view text-window) (controller keyboard)) ...
The Law does not restrict the number of arguments that can be 
specialized. It does not hinder the multi-dimensional implicit case
analysis of CLOS.

Nevertheless, the Law treats the first argument in a special way.
Our experience indicates that this is very useful from a software
engineering point of view. When you check your code for violations
of the Law, it is good to search for nested function calls
which have a nesting level of more than 2.

The formulation of the Law is subtle: we formulate it here in terms
of objects and not in terms of variables (as Gregor Kiczales
suggested in his message). That would be too restrictive.
We have a formulation in terms of types which is a little more
generous than the object version, but compile-time enforceable for 
a useful subset of CLOS. I look forward to see some of your
violations of the Law for which you can give a convincing argument
that the violation is justified.
-- Karl Lieberherr