aspose file tools*
The moose likes Clojure and the fly likes Code style in Clojure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Clojure
Bookmark "Code style in Clojure" Watch "Code style in Clojure" New topic
Author

Code style in Clojure

Stian Almaas
Ranch Hand

Joined: Sep 22, 2005
Posts: 33
Hello everybody!

I wonder if there is an official or de facto code style for clojure. I am trying to pick it up from bits I see on the net and one of the things that I notice is that everyone uses very short names on functions. It seems customary to prefer fewer characters where possible. (Examples: fib rather than fibonacci, str rather then string)

This is the opposite of the trend I have observed in Java over the latest years where the preferance is for long and descriptive naming, helped by auto-completion in the IDEs.

I can decide for myself of course, but is it more usual to have shorter names in clojure?
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

http://www.assembla.com/wiki/show/clojure/Clojure_Library_Coding_Standards
Hope this help.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Of course the link talks about coding standards for Clojure libraries, but it is useful any way.
I noticed Clojure coding looks like English, for example:
search-city , walk-the-graph , clean-and-build
IMHO, this is better than:
searchCity, walkTheGraph, cleanAndBuild
Personally, I don't prefer short as the code will looks obfuscated.
Prefer fibonacci instead of fibo, walk-the-graph instead of walk.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

You might also want to check this:
http://en.wikibooks.org/wiki/Learning_Clojure/Coding_Conventions
http://techbehindtech.com/2010/12/09/clojure-good-coding-guidelines/
Stian Almaas
Ranch Hand

Joined: Sep 22, 2005
Posts: 33
Thanks John!

That was actually more than I asked for, which is wonderful!

The link to the assembla-wiki was perfect. Exactly what I was looking for. Copying what the api developers are doing is probably best. And I can see that they do have a preferance for short names, like x,y for numbers, pred for predicates etc.

I also prefer longer names for functions and vars. I am used to camel case so I don't read that any differently than the dashes used in Clojure. For me it would have been just as easy to go with camel case but I guess it is nice to explicitly see where you call down to Java code.
 
jQuery in Action, 2nd edition
 
subject: Code style in Clojure