random-state.net

Nikodemus Siivola

<< next | top | previous >>

February 24th 2006 #
random, February 24th 2006

FOO-OF!

Everyone has their own style of naming accessors, but here's a meme I've occasionally tried to spread:

(defclass bar ()
  ((foo :accessor foo-of)))

This works like a charm, and with good reason. You are highly unlikely to ever want to name a function with arity other then 1 with the postfix -OF. It just doesn't make sense. In this way this style is superior to plain FOO, and to a lesser extent better then GET-FOO and friends.

If you are partial to BAR-FOO, then you are beyond help. The problems with this approach in conjuction with subclassing are amply documented, so nuff said.

If your objection is purely aesthetic then I can only assure you that the eye gets used to it. CLASS-OF and TYPE-OF provide sufficient precendent to my satisfaction.

If you find yourself needing either of the above as accessors, this is what shadowing and generic functions are there for!

(defpackage "EXAMPLE" 
  (:use "COMMON-LISP")
  (:shadow "TYPE-OF"))

(in-package "EXAMPLE")

(defgeneric type-of (instance)
  (:method (instance)    
    (cl:type-of instance)))

Done.