When to use aliases and when to use :refer in Clojure/ClojureScript :require? -
when using :require
in clojure or clojurescript, when should use aliases , when should cherry-pick functions use?
examples:
using alias
(:require [some-package.sub some]) (some/do-stuff "xyz")
using
:refer
(:require [some-package.sub :refer [do-stuff]]) (do-stuff "xyz")
using alias seems more handy if dependency has plenty of functions want use or use of functions (however many there are), since clojurescript (intentionally) doesn't support :refer :all
. on other hand, using :refer
seems more "clean" approach, when using specific functions dependency.
are there other things 1 should consider when choosing between 2 (and valid reason in first place)?
another thing think of if have lots of dependencies and/or loads of own functions defined in file, might beneficial have alias prefixes in function calls make clearer functions located, if use small subset of functions offered dependency.
how should choose 1 use? or should decide within dev team , fine long stick 1 approach?
using aliases makes clear reading code particular variable defined, when standardize use of aliases across of project.
the unqualified name my-fn
refer to:
- a var
my-fn
defined in current namespace. - a var
my-fn
defined in other namespace, aliased current namespace using:require :refer
- a local lexical binding (i.e. function parameter or let-bound variable) named
my-fn
.
the qualified name a/my-fn
can refer to:
- a var
my-fn
in namespace aliaseda
(or namespacea
if you're being naughty , using single-segment namespaces). - less commonly, static method or field named
my-fn
in java classa
.
a qualified name can't confused defined in current namespace, , because can't use qualified names function parameters or in let
bindings, there's no risk of being shadowed local lexical bindings. using :require :refer
gives clarity without giving in return, , in opinion should used sparingly.
Comments
Post a Comment