I've only recently started real clojure development, so very much still 
learning what styles work and what don't.  I have a question about naming 
attribute getters...

Suppose I want to model "fruit" entities.  I will use a hash-map to 
represent the data for each such entity, and will defn a "make" function to 
construct fruit instances.  I will put "make" and any other fruit-related 
functions within a new namespace "myprog.fruit".

Because my data representation is a map, strictly speaking I don't need any 
attribute getter functions.  Because I could simply use the attribute 
keywords in the map as my getter functions, eg (:color fru).  But I will 
create actual getter functions within my namespace anyway, for a few 
reasons.

   1. It will signal to any users of my fruit library, which attributes are 
   "official" and are expected to be used / supported, versus attributes which 
   are more of an implementation detail.
   2. Some attributes will simply be (defn color [fru] (:color fru)), 
   whereas others are less direct, eg (defn red? [fru] (= :red fru)) or 
   another eg (defn packingvol [{:keys [size]}] (* size size size)).  
   3. Down the track I can change my data representation, and just 
   reimplement the getter functions.  Users of my fruit library who have stuck 
   to my getter functions interface will not need to change a thing.

This approach seems okay to me so far, though I am open to critiques and 
alternative suggestions.  But one issue has come to mind... leading to the 
question of this post...

At the end of 2. above, I have defined a local let symbol "size" as part of 
the map destructuring.  But it so happens I have already defined a size 
getter, namely (defn size [fru] (:size fru)).  So within the definition of 
packingvol my size getter is masked by the local symbol "size".  Now I'm 
not sure this masking causes much harm... I don't actually need to use the 
size getter here, and if I really needed it I could always use 
"myprog.fruit/size"...  But something still makes me feel uncomfortable 
about it, maybe because this masking could end up happening a lot within my 
fruit.clj source code file.

Now I could just switch to a new naming convention for my getters, ie 
"color-get", "red?-get", "size-get", "packingvol-get" etc.  But I'm not 
sure I like the extra verbosity.  And for users of my fruit library - who 
will be working in a different namespace - this problem mostly goes away as 
they will probably be namespace-qualifying access to my getters.

Is using self-named attribute getters a good idea?

Thanks,

Mark.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to