What are common conventions for using namespaces in Clojure?

  1. I guess it’s ok if you think it helps, but many Clojure projects don’t do so — cf. Compojure (using a top-level compojure ns and various compojure.* ns’s for specific functionality), Ring, Leiningen… Clojure itself uses clojure.* (and clojure.contrib.* for contrib libraries), but that’s a special case, I suppose.

  2. Yes! You absolutely must do so, or else Clojure won’t be able to find your namespaces. Also note that you musn’t use the underscore in namespace names or the hyphen in filenames and wherever you use a hyphen in a namespace name, you must use an underscore in the filename (so that the ns my.cool-project is defined in a file called cool_project.clj in a directory called my).

  3. You need to make sure all your stuff is on the classpath, but it doesn’t matter if it’s in a jar, multiple jars, a mixture of jars and directories on the filesystem… As long as it obeys the correct naming conventions (your point no. 2) you should be fine.

    However, do not compile things ahead-of-time if there’s no particular reason to do so — this may prevent your code from being portable across various versions of Clojure without providing any benefits besides a slightly improved loading time.

    You’ll still need to use AOT compilation sometimes, notably in some Java interop scenarios — the documentation of the relevant functions / macros always mentions that. There are examples of things requiring AOT in clojure.contrib; I’ve never needed it, so I can’t provide much in the way of details.

  4. I’d say you should use jars for functional units of code. E.g. Compojure and Ring get packaged as single jars containing many namespaces which together compose the whole package. Also, clojure.contrib is notably packaged as a single jar with multiple unrelated libraries; but that again may be a special case.

    On the other hand, a single jar containing all of your project’s code together with its dependencies might occasionally be useful for deployment. Check out the Leiningen build tool and its ‘uberjar’ facility if you think that sort of thing may be useful to you.

Leave a Comment