I am new to Clojure and trying to get a handle on organizing a project’s namespaces. I am working on a solver for the knapsack problem. Currently, I have broken the modules into files, but everything lives in one namespace: my-project-name.core
. I am using load
to manage access across files and it seems like a bit of a kludge. I’ve drawn a digram of how I think I want the various parts to interface:
My goal is to be able to work on each of the nodes in the diagram independently. I am trying to come up with a strategy for rational namespace design somewhere between everything in one and each in its own.
In particular I want to be able to swap out the solver and estimating components since these are the most tunable.
bbatsov/clojure-style-guide suggests some good rules on how to use namespaces:
-
Use one namespace per file.
-
Use the
ns
form for representing namespace dependencies. You should almost never need to useload
in regular code. -
Prefer
:require :as
instead of:refer
or:use
. This makes it easier to see what you are using from which namespace, and your code ends up being easier to understand.
As for swapping out components, this is typically better solved using protocols or multimethods than namespaces.