Hacker News new | ask | show | jobs
by mkremins 4331 days ago
Both :use and :require :refer :all slurp symbols into your namespace unqualified. This behavior can cause confusion when someone reading your code tries to figure out which symbols are defined where. Most of the time, more explicit alternatives are preferred:

  (:require [foo :as f])
gives the required namespace a short alias with which symbols defined in that namespace can be qualified, while

  (:require [foo :refer [bar baz quux]])
explicitly provides a set of symbols to import unqualified.

Generally speaking, :require :refer :all is also considered non-idiomatic. In fact, ClojureScript has explicitly avoided supporting both :require :refer :all and naked :use [1]. Backwards compatibility seems to be the only reason either is still supported by Clojure on the JVM.

[1] https://groups.google.com/forum/#!msg/clojurescript/SzYK08Od...

1 comments

I totally agree about requiring :as or :refer to specific functions, but it still seems like the clojure team prefers naked `use` over `require refer all`, so should the style guide be changed to allow naked `use` in some circumstances?

I guess a popular example is `(:use clojure.test)`

Makes sense to me, although I suppose there's probably an argument to be made in favor of the uniformity that comes with using :require for everything.

(:use clojure.test) is probably a worthy exception – 90% of the time I see :use in the wild, it's in exactly that context :)