What are common conventions for using namespaces in Clojure?
Asked Answered
H

2

54

I'm having trouble finding good advice and common practices for the use of namespaces in Clojure. I realize that namespaces are not the same as Java packages so I'm trying to tease out the conventions in Clojure, which seem surprisingly hard to determine.

I think I have a pretty good idea how to split functions into clj files and even roughly how I'd want to organize those files into directories. But beyond that I'm having trouble finding the mechanics for my dev environment. Some inter-related questions:

  1. Do I use the same uniqueness conventions for Clojure namespaces as I would normally use for Java packages? [ie backwards-company-domain.project.subsystem]
  2. Should I save my files in a directory structure that matches my namespaces? [ala Java]
  3. If I have multiple namespaces, do I need to compile all of my code into a jar and add it to my classpath to make it accessible?
  4. Should each namespace compile to one jar? Or should I create a single jar that contains clj code from many namespaces?

Thanks...

Hookah answered 8/2, 2010 at 16:44 Comment(1)
I retroactively give a hearty thumbs-up to this blog which would have helped me a lot when I wrote this question: blog.8thlight.com/articles/2010/12/6/…Hookah
C
43
  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.

Cuprous answered 8/2, 2010 at 17:6 Comment(0)
P
10
  1. Strictly speaking, not necessary, though many Java projects have dropped that convention as well, especially for internal projects or private APIs. Do avoid single-segment namespaces though, which would result in classfiles being generated in the default package.
  2. Yes.

Regarding 3 & 4, packaging and AOT compilation are entirely orthogonal to the question of namespace conventions.

Pickpocket answered 8/2, 2010 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.