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:

  1. using alias

    (:require [some-package.sub some])  (some/do-stuff "xyz") 
  2. 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 aliased a (or namespace a if you're being naughty , using single-segment namespaces).
  • less commonly, static method or field named my-fn in java class a.

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

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -