On Mon, Dec 29, 2008 at 3:40 PM, Mark Volkmann
<r.mark.volkm...@gmail.com> wrote:
>
> On Mon, Dec 29, 2008 at 2:24 PM, Brian Doyle <brianpdo...@gmail.com> wrote:
>> Looking at this code the uppercase variables stands out.
>> This isn't idiomatic is it?
>>
>> (def GRID_SIZE 10)
>> (def HEIGHT 600)
>> (def MARGIN 50)
>
> I don't know.

I believe the idiom for global values like this is to place asterisks
around the name.  Underscores (and CamelCase) should only be used when
required for Java interop:

(def *grid-size* 10)
(def *height* 600)
(def *margin* 50)
(def *x-index* 0)
(def *y-index* 1)

> I was following Java conventions of making constants all
> uppercase. Is there a convention for this in Clojure? Is there a way I
> could prevent them from being changed later?

I'm not aware of anyway to make a global constant.  By using a Var as
you've done, the only way to change them is to use 'def' again (which
is a big hammer and very much discouraged inside regular functions) or
to use 'binding' to temporarily change their values within a
particular thread.  I think what  you have is sufficient for
communicating your intended meaning.

On a more general point, I'd personally recommend being wary of
over-investing in comments.  This is not a radical recommendation, but
I'll bring up again anyway that thought that it's better to write code
in such a way that it explains itself than to add comments to code
that doesn't.  Only when the former is insufficient should more
comments be added.  Every line of comment is another line of code that
must be maintained, and worse than that it's a line that no compiler
or unit test is ever going to indicate as incorrect.  When adding a
comment, I think it's appropriate to be sure that the maintenance cost
of the comment itself outweighs the maintenance cost of having no
comment (or a shorter or more general comment).

An example of this point -- Abhishek's original code used a hash
with:x and :y keys to indicate coordinates.  I changed this to a
two-element vector only in pursuit of this particular puzzle's goals,
namely fewer lines of code.  This is a very different goal from most
code, which should be maintainability, and also different from
tutorial code, which should be about teaching Clojure to someone who
doesn't already know it.  For either of those latter goals, I would
contend Abhishek's solution was a better one -- using :x and :y help
indicate what's going on without global index names (like *x-index*)
or much extra commenting.

Another example -- if you find a particular use of #() to be too
confusing on its own, consider replacing it with a (fn ...), which
allows the naming of each argument as well as destructuring. This can
again improve readability without require more comments.

I think your expanded version of the snake program may be very
beneficial to some, though with a different purpose than the original
version, so thanks for providing it.  Personally, I wouldn't want to
maintain a large codebase that was written using either style, as both
are a bit extreme in opposite ends of the verbosity scale. As I've
said elsewhere "golfing" is fun, if not broadly useful.  Conversely,
tutorial-style code may be very useful in appropriate contexts, but is
hardly ever fun to write.

...and now this post is at an extreme of the verbosity scale.  Sorry
all, I'll quit now before I get any further behind.

--Chouser

--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to