I’m interested in how people structure their Clojure source code.
Being used to Java, I’m pretty familiar with the paradigm of one class per source code file, bundling all the data and method definitions with appropriate comments and annotations etc.
However Clojure offers a lot more flexibility, and I’m not sure how I should structure my project (likely to end up as a medium sized app, maybe 5,000 lines with three or four distinct subsystems)
In particular I’m wrestling with:
- What guidelines should I use to determine whether code should be in a single namespace vs. separated out into different namespaces?
- Should each protocol/datatype have it’s own namespace + source file with associated set of functions?
- When should I require vs. use other namespaces?
I’m from a Java background as well, along with quite a bit of Ruby and a little Go. Here’s what I’m doing at the moment, about a month into Clojure:
I have two conventions for namespaces vs files:
As a namespace example, I have a parser that reads a format and converts it to HTML. I have a single namespace for the parser (the semantic unit) and several files in the directory split on sub-functionality: Lexer, parser, HTML conversion and a main file that contains the primary public API for using the parser.
I wouldn’t automatically use one namespace per datatype, it depends on the scope of the datatype. If it’s a big one, perhaps. But for a datatype like Point, with two fields and a couple of functions, I’d rather subsume it into a more general namespace like Geometry.
Require vs. use: