You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -730,6 +730,168 @@ Across a project, it's good to be consistent with namespace aliases; e.g., don't
730
730
If you follow the previous two guidelines you're basically covered, but if you opt for custom namespace aliasing scheme it's still important to apply it
731
731
consistently within your projects.
732
732
733
+
== Naming
734
+
735
+
[quote, Phil Karlton]
736
+
____
737
+
The only real difficulties in programming are cache invalidation and
Use `+->+` instead of `to` in the names of conversion functions.
804
+
805
+
[source,clojure]
806
+
----
807
+
;; good
808
+
(defn f->c ...)
809
+
810
+
;; not so good
811
+
(defn f-to-c ...)
812
+
----
813
+
814
+
=== Dynamic Vars [[naming-dynamic-vars]]
815
+
816
+
Use `*earmuffs*` for things intended for rebinding (ie. are dynamic).
817
+
818
+
[source,clojure]
819
+
----
820
+
;; good
821
+
(def ^:dynamic *a* 10)
822
+
823
+
;; bad
824
+
(def ^:dynamic a 10)
825
+
----
826
+
827
+
=== Constants [[naming-constants]]
828
+
829
+
Don't use a special notation for constants; everything is assumed a constant
830
+
unless specified otherwise.
831
+
832
+
=== Unused Bindings [[naming-unused-bindings]]
833
+
834
+
Use `+_+` for destructuring targets and formal argument names whose
835
+
value will be ignored by the code at hand.
836
+
837
+
[source,clojure]
838
+
----
839
+
;; good
840
+
(let [[a b _ c] [1 2 3 4]]
841
+
(println a b c))
842
+
843
+
(dotimes [_ 3]
844
+
(println "Hello!"))
845
+
846
+
;; bad
847
+
(let [[a b c d] [1 2 3 4]]
848
+
(println a b d))
849
+
850
+
(dotimes [i 3]
851
+
(println "Hello!"))
852
+
----
853
+
854
+
However, when it can help the understanding of your code, it can be useful to explicitly name unused arguments or maps you're destructuring from. In this case, prepend the name with an underscore to explicitly signal that the variable is supposed to be unused.
855
+
856
+
[source,clojure]
857
+
----
858
+
;; good
859
+
(defn myfun1 [context _]
860
+
(assoc context :foo "bar"))
861
+
862
+
(defn myfun2 [context {:keys [id]}]
863
+
(assoc context :user-id id))
864
+
865
+
;; better
866
+
(defn myfun1 [context _user]
867
+
(assoc context :foo "bar"))
868
+
869
+
(defn myfun2 [context {:keys [id] :as _user}]
870
+
(assoc context :user-id id))
871
+
----
872
+
873
+
=== Idiomatic Names [[idiomatic-names]]
874
+
875
+
Follow ``clojure.core``'s example for idiomatic names like `pred` and `coll`.
876
+
877
+
* in functions:
878
+
** `f`, `g`, `h` - function input
879
+
** `n` - integer input usually a size
880
+
** `index`, `i` - integer index
881
+
** `x`, `y` - numbers
882
+
** `xs` - sequence
883
+
** `m` - map
884
+
** `s` - string input
885
+
** `re` - regular expression
886
+
** `coll` - a collection
887
+
** `pred` - a predicate closure
888
+
** `& more` - variadic input
889
+
** `xf` - xform, a transducer
890
+
* in macros:
891
+
** `expr` - an expression
892
+
** `body` - a macro body
893
+
** `binding` - a macro binding vector
894
+
733
895
== Functions
734
896
735
897
=== Optional New Line After Function Name [[optional-new-line-after-fn-name]]
@@ -1544,168 +1706,6 @@ Be careful regarding what exactly you attach metadata to.
1544
1706
(meta #'a) ;=> nil
1545
1707
----
1546
1708
1547
-
== Naming
1548
-
1549
-
[quote, Phil Karlton]
1550
-
____
1551
-
The only real difficulties in programming are cache invalidation and
Use `+->+` instead of `to` in the names of conversion functions.
1618
-
1619
-
[source,clojure]
1620
-
----
1621
-
;; good
1622
-
(defn f->c ...)
1623
-
1624
-
;; not so good
1625
-
(defn f-to-c ...)
1626
-
----
1627
-
1628
-
=== Dynamic Vars [[naming-dynamic-vars]]
1629
-
1630
-
Use `*earmuffs*` for things intended for rebinding (ie. are dynamic).
1631
-
1632
-
[source,clojure]
1633
-
----
1634
-
;; good
1635
-
(def ^:dynamic *a* 10)
1636
-
1637
-
;; bad
1638
-
(def ^:dynamic a 10)
1639
-
----
1640
-
1641
-
=== Constants [[naming-constants]]
1642
-
1643
-
Don't use a special notation for constants; everything is assumed a constant
1644
-
unless specified otherwise.
1645
-
1646
-
=== Unused Bindings [[naming-unused-bindings]]
1647
-
1648
-
Use `+_+` for destructuring targets and formal argument names whose
1649
-
value will be ignored by the code at hand.
1650
-
1651
-
[source,clojure]
1652
-
----
1653
-
;; good
1654
-
(let [[a b _ c] [1 2 3 4]]
1655
-
(println a b c))
1656
-
1657
-
(dotimes [_ 3]
1658
-
(println "Hello!"))
1659
-
1660
-
;; bad
1661
-
(let [[a b c d] [1 2 3 4]]
1662
-
(println a b d))
1663
-
1664
-
(dotimes [i 3]
1665
-
(println "Hello!"))
1666
-
----
1667
-
1668
-
However, when it can help the understanding of your code, it can be useful to explicitly name unused arguments or maps you're destructuring from. In this case, prepend the name with an underscore to explicitly signal that the variable is supposed to be unused.
1669
-
1670
-
[source,clojure]
1671
-
----
1672
-
;; good
1673
-
(defn myfun1 [context _]
1674
-
(assoc context :foo "bar"))
1675
-
1676
-
(defn myfun2 [context {:keys [id]}]
1677
-
(assoc context :user-id id))
1678
-
1679
-
;; better
1680
-
(defn myfun1 [context _user]
1681
-
(assoc context :foo "bar"))
1682
-
1683
-
(defn myfun2 [context {:keys [id] :as _user}]
1684
-
(assoc context :user-id id))
1685
-
----
1686
-
1687
-
=== Idiomatic Names [[idiomatic-names]]
1688
-
1689
-
Follow ``clojure.core``'s example for idiomatic names like `pred` and `coll`.
0 commit comments