Correct indentation level of code blocks

Also, remove hard tabs from code blocks.
This commit is contained in:
Kevin Yap 2015-01-02 21:05:02 -08:00
parent d684ea1eed
commit 19b55384f8

View File

@ -56,58 +56,58 @@ Layout & Indentation
+ Indentation shall be 2 spaces (no hard tabs), except when matching + Indentation shall be 2 spaces (no hard tabs), except when matching
the indentation of the previous line. the indentation of the previous line.
.. code-block:: clj .. code-block:: clj
;; Good (and preferred) ;; Good (and preferred)
(defn fib [n] (defn fib [n]
(if (<= n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
;; Still okay
(defn fib [n]
(if (<= n 2) n (+ (fib (- n 1)) (fib (- n 2)))))
;; Still okay
(defn fib [n]
(if (<= n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
;; Hysterically ridiculous
(defn fib [n]
(if (<= n 2) (if (<= n 2)
n ;; yes, I love randomly hitting the space key n
(+ (fib (- n 1)) (fib (- n 2))))) (+ (fib (- n 1)) (fib (- n 2)))))
;; Still okay
(defn fib [n]
(if (<= n 2) n (+ (fib (- n 1)) (fib (- n 2)))))
;; Still okay
(defn fib [n]
(if (<= n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
;; Hysterically ridiculous
(defn fib [n]
(if (<= n 2)
n ;; yes, I love randomly hitting the space key
(+ (fib (- n 1)) (fib (- n 2)))))
+ Parentheses must *never* be left alone, sad and lonesome on their own + Parentheses must *never* be left alone, sad and lonesome on their own
line. line.
.. code-block:: clj .. code-block:: clj
;; Good (and preferred) ;; Good (and preferred)
(defn fib [n] (defn fib [n]
(if (<= n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
;; Hysterically ridiculous
(defn fib [n]
(if (<= n 2) (if (<= n 2)
n n
(+ (fib (- n 1)) (fib (- n 2))) (+ (fib (- n 1)) (fib (- n 2)))))
;; Hysterically ridiculous
(defn fib [n]
(if (<= n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))
) )
) ; GAH, BURN IT WITH FIRE ) ; GAH, BURN IT WITH FIRE
+ Vertically align ``let`` blocks. + Vertically align ``let`` blocks.
.. code-block:: clj .. code-block:: clj
(let [[foo (bar)] (let [[foo (bar)]
[qux (baz)]] [qux (baz)]]
(foo qux)) (foo qux))
+ Inline comments shall be two spaces from the end of the code; they + Inline comments shall be two spaces from the end of the code; they
@ -132,21 +132,21 @@ Coding Style
+ As a convention, try not to use ``def`` for anything other than global + As a convention, try not to use ``def`` for anything other than global
variables; use ``setv`` inside functions, loops, etc. variables; use ``setv`` inside functions, loops, etc.
.. code-block:: clj .. code-block:: clj
;; Good (and preferred) ;; Good (and preferred)
(def *limit* 400000) (def *limit* 400000)
(defn fibs [a b] (defn fibs [a b]
(while true (while true
(yield a) (yield a)
(setv (, a b) (, b (+ a b))))) (setv (, a b) (, b (+ a b)))))
;; Bad (and not preferred) ;; Bad (and not preferred)
(defn fibs [a b] (defn fibs [a b]
(while true (while true
(yield a) (yield a)
(def (, a b) (, b (+ a b))))) (def (, a b) (, b (+ a b)))))
+ Do not use s-expression syntax where vector syntax is intended. + Do not use s-expression syntax where vector syntax is intended.
@ -154,15 +154,15 @@ Coding Style
is just because the compiler isn't overly strict. In reality, the is just because the compiler isn't overly strict. In reality, the
correct syntax in places such as this is the latter. correct syntax in places such as this is the latter.
.. code-block:: clj .. code-block:: clj
;; Bad (and evil) ;; Bad (and evil)
(defn foo (x) (print x)) (defn foo (x) (print x))
(foo 1) (foo 1)
;; Good (and preferred) ;; Good (and preferred)
(defn foo [x] (print x)) (defn foo [x] (print x))
(foo 1) (foo 1)
+ Use the threading macro or the threading tail macros when encountering + Use the threading macro or the threading tail macros when encountering
@ -170,34 +170,34 @@ Coding Style
use them when clarity and readability improves; do not construct use them when clarity and readability improves; do not construct
convoluted, hard to understand expressions. convoluted, hard to understand expressions.
.. code-block:: clj .. code-block:: clj
;; Preferred ;; Preferred
(def *names* (def *names*
(with [f (open "names.txt")] (with [f (open "names.txt")]
(-> (.read f) (.strip) (.replace "\"" "") (.split ",") (sorted)))) (-> (.read f) (.strip) (.replace "\"" "") (.split ",") (sorted))))
;; Not so good ;; Not so good
(def *names* (def *names*
(with [f (open "names.txt")] (with [f (open "names.txt")]
(sorted (.spilt "," (.replace "\"" "" (.strip (.read f))))))) (sorted (.spilt "," (.replace "\"" "" (.strip (.read f)))))))
;; Probably not a good idea ;; Probably not a good idea
(defn square? [x] (defn square? [x]
(->> 2 (pow (int (sqrt x))) (= x))) (->> 2 (pow (int (sqrt x))) (= x)))
+ Clojure-style dot notation is preferred over the direct call of + Clojure-style dot notation is preferred over the direct call of
the object's method, though both will continue to be supported. the object's method, though both will continue to be supported.
.. code-block:: clj .. code-block:: clj
;; Good ;; Good
(with [fd (open "/etc/passwd")] (with [fd (open "/etc/passwd")]
(print (.readlines fd))) (print (.readlines fd)))
;; Not so good ;; Not so good
(with [fd (open "/etc/passwd")] (with [fd (open "/etc/passwd")]
(print (fd.readlines))) (print (fd.readlines)))