Skip to content

Commit d93c91e

Browse files
committed
[Fix #201] Add a guideline about idiomatic namespace aliases
1 parent e43e474 commit d93c91e

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

README.adoc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,78 @@ In the `ns` form, sort your requirements and imports. This facilitates readabili
611611
[clojure.java.shell :as sh]))
612612
----
613613

614+
=== Use Idiomatic Namespace Aliases
615+
616+
Many core Clojure namespaces have idiomatic aliases that you're
617+
encouraged to use within your projects - e.g. the most common way to
618+
require `clojure.string` is: `[clojure.string :as str]`.
619+
620+
NOTE: This may appear to mask
621+
`clojure.core.str`, but it doesn't. It's expected that
622+
`clojure.core/str` and `clojure.string/*` to be used in a namespace as
623+
`str` and `str/whatever` without conflict.
624+
625+
[source,clojure]
626+
----
627+
;; good
628+
(ns ... (:require [clojure.string :as str] ...)
629+
630+
(str/join ...)
631+
632+
;; not as good - just be idiomatic and use as `str/`
633+
(ns ... (:require [clojure.string :as string] ...)
634+
635+
(string/join ...)
636+
----
637+
638+
Bellow are some common idiomatic aliases:
639+
640+
|===
641+
| Namespace | Idiomatic Alias
642+
| io
643+
| clojure.java.io
644+
| set
645+
| clojure.set
646+
| str
647+
| clojure.string
648+
| walk
649+
| clojure.walk
650+
| zip
651+
| clojure.zip
652+
| xml
653+
| clojure.data.xml
654+
| as
655+
| clojure.core.async
656+
| mat
657+
| clojure.core.matrix
658+
| edn
659+
| clojure.edn
660+
| pp
661+
| clojure.pprint
662+
| spec
663+
| clojure.spec.alpha
664+
| csv
665+
| clojure.data.csv
666+
| json
667+
| cheshire.core
668+
| time
669+
| java-time
670+
| http
671+
| clj-http.client
672+
| log
673+
| clojure.tools.logging
674+
| sql
675+
| hugsql.core
676+
| yaml
677+
| clj-yaml.core
678+
| sh
679+
| clojure.java.shell
680+
|===
681+
682+
=== Use Consistent Namespace Aliases
683+
684+
Across a project, it's good to be consistent with namespace aliases; e.g., don't require `clojure.string` as `str` in one namespace but `string` in another.
685+
614686
== Functions
615687

616688
=== Optional New Line After Function Name [[optional-new-line-after-fn-name]]

0 commit comments

Comments
 (0)