Why do you prefer: require: refer: all over: use in clojure

In her Clojure style guide, the author writes the following,

Prefer using :require :refer :all over :use in ns macro

He gives no explanation why this is a good idea. Here's a good reason to avoid using: use ns in a macro?

+4
source share
5 answers

My impression is that there are two reasons. 1) In doing so, you are likely to avoid any name conflicts and / or accidental shadowing. 2) This practice provides greater transparency regarding the final source of functions and variables used in the code. The first one is pretty practical, and Chiron's answer gives an excellent demonstration.

, , . , :

(ns your-namespace
  (:use [library-one]
        [library-two]
        [library-three]
        [library-four]
        [library-five]))

-, , :

(important-function some-arg some-other-arg)

important-function , , library-three, ( , ) , , - , , ! , , REPL, :

your-namespace> `important-function
> library-three/important-function

, , ( ), . , :require d . :

(ns your-namespace
  (:require [library-one]
            [library-two]
            [library-three]
            [library-four]
            [library-five]))

...

(library-three/important-function some arg some-other-arg)

, important-function . , :refer :all. :refer :all , , , .

, ... , , , . , vars, :use :only. :

(ns your-namespace
  (:use [library-one :only []]
        [library-two :only []]
        [library-three :only [important-function]]
        [library-four :only []]
        [library-five :only []]))

...

(important-function some arg some-other-arg)

, :use :only , , :use , . :require , :as. :

(ns your-namespace
  (:require [library-three :as L3]))

(L3/important-function some-arg some-other-arg)

. , Clojure.

+4

, . . :

ns . , , : , , , . , : , . : , , : use.

:

(ns mork.test
  (:use [mork.stats :only [summarize-group]]
        [mork.utils :only [strip resolve-fn]]
        [clojure.test])
  (:require [mork.view :as view]
            [clojure.java.io :as io]
            [cheshire.core :as json])
  (:import (java.io PushbackReader))

:

(ns mork.test
  (:require [mork.stats :refer [summarize-group]]
            [mork.utils :refer [strip resolve-fn]]
            [clojure.test :refer :all]
            [mork.view :as view]
            [clojure.java.io :as io]
            [cheshire.core :as json])
  (:import (java.io PushbackReader))

, : : , , Clojure vars Java.

, vars ( , clojure.test ), , , : refer: all , .

+4

:require , .

(:require [clojure.json :as json])
(:require [clojure.xml  :as xml])

(json/read "file")
(xml/read  "file")

:use

(read "file")

, read? , ns read use .

+1

Separation of problems. requireallows you to load namespaces, referallows you to determine how you feel about vars in these namespaces. useposes two problems.

+1
source

With :requireyou, there is only one place where you define the import of namespace names, and you can easily switch from a link to all functions to pass multiple functions or just require a namespace (or mix and match as you like).

(:require [foo.bar]
          [foo.baz :refer :all :as baz]
          [foo.foobar :as fb :refer [bazbaz]])
0
source

Source: https://habr.com/ru/post/1535890/


All Articles