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

Re: Law of good style for CLOS




Karl:

	After reading your IEEE Computer article, I must unfortunately
take strong exception to your Law of Demeter for CLOS. 

	The reason is your law would prohibit functional composition,
one of the foundations of Lisp. Indeed, the Law of Demeter, if applied
to regular Lisp code, would prohibit functional composition for
built-in generic functions (such as +). For example, the Law of Demeter 
says that the following code is in "poor" style:

	(defmethod sum-of-sqrt-of-difference ((x float) (y float))
	  (+ (sqrt (- x y)) y))

since the result of applying the built-in generic function - is an argument
to the SQRT generic function, as is the result of SQRT to +.
The result of applying - is neither a formal 
parameter to the function nor is it generated within the function body itself.
This is exactly analogous to a user defined generic function in CLOS, since
different implementations of the square root and addition algorithms will
be run depending on the result of the subtraction and square root, respectively,
so the actual parameters are involved in selecting the implementation.

	As a less trivial example, consider the following. In Unix, the
pipe construct has the semantics of functional composition. Thus, to
format a troff document and send it to the line printer the following
command line is used:

	troff <switches> <filename> | lpr -P<printer>

Using functional composition in Lisp/CLOS, we have:

	(defmethod output-document 
	  ((switches string) (filename pathname) (printer string))

	  (lpr printer (troff switches filename)))

	(defmethod lpr ((printer string) (document stream))
	   <<code to send document to the printer>>
	)

	(defmethod troff ((switches string) (filename pathname))
	  <<code to do troff, might be in Lisp or a call to Unix>>
	)

	The functional programming style is a useful and powerful one,
though, as with all programming styles, it has its limitations. One of
the advantages of CLOS is that it makes a combination of functional
and object-oriented styles possible, allowing side effect producing
operations to be limited in extent, which, in turn, makes proofs of
correctness easier. By eliminating functional composition for generic
functions, the Law of Demeter puts unnecessary restrictions on the
programmer.

		jak