Tech behind Tech

Raw information. No finesse :)

Clojure – good coding guidelines

with 6 comments

Naming :

– only lower case letters separated by hyphens except types and protocols should be named in camelcase
– predicates ends with ?
– destructive functions ends with !
– variables that are meant for be re-binding should have earmuffs
– use “_” for names that will be ignored by the code

Code Structure :

– when there is only “then” clause in a conditional statement use “when” instead of “if”
– put all trailing parenthesis in one line
– for dependency use “:only” for “use” and “require”
– use two spaces for indentation
– destructure arg list only when you want to explain the structure of arg to caller otherwise destructure in first let binding

Coding Style:

– try to solve a problem by using function before trying with macro
– be lazy when possible
– use keywords as keys for map entries
– use keyword-first syntax to access properties on objects
– prefer sequence library functions instead of loop/recurr
– only use anonymous functions for short function definitions that fit comfortably on a single line. Otherwise create a private, named function.
– short functions

Written by Siva Jagadeesan

December 9, 2010 at 11:04 pm

Posted in Clojure

Tagged with ,

6 Responses

Subscribe to comments with RSS.

  1. Nice, one remark though:
    A rule I know is “use when if you’re doing wide effects only” which contradicts your if-rule. So which one is better?


    December 10, 2010 at 1:53 am

    • The reason for if-rule is it makes it easy to read the code. By looking at code, we could easily differentiate when there is “else” clause and when there is no “else” clause. Could you explain what do you mean by “wide effects”?

      Siva Jagadeesan

      December 10, 2010 at 8:48 am

      • Sorry, I meant to write “side effects” 🙂
        I thought I remember it from Common Lisp, but everything I googled now agrees with you so either I remember it wrong or it was just one or two authors. So I will now use when even without side effects…

        P.S. Man, your blog just made me think my monitor’s damaged =)


        December 11, 2010 at 4:02 am

  2. These are just guidelines … if you think using “when” only when there is side effect works better for you, go for it..

    snow flakes are going to be till jan 4th 🙂

    Siva Jagadeesan

    December 11, 2010 at 1:44 pm

  3. What’s the rationale for destructuring with let rather than in the arg list? Isn’t the latter more succinct?

    Ray Miller

    December 13, 2010 at 3:21 am

    • Hi Ray:

      It makes sense to de-structure in arg list when you want to explain to caller what kind of data the function is expecting. When you do not need to be explicit, it is better not to have de-structure in arg list as it will make it difficult to read the code.

      — Siva Jagadeesan

      Siva Jagadeesan

      December 13, 2010 at 4:51 pm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: