2015-01-03 04:56:07 +01:00
|
|
|
==============
|
|
|
|
Hy Style Guide
|
|
|
|
==============
|
|
|
|
|
|
|
|
The Hy style guide intends to be a set of ground rules for the Hyve
|
|
|
|
(yes, the Hy community prides itself in appending Hy to everything)
|
|
|
|
to write idiomatic Hy code. Hy derives a lot from Clojure & Common
|
2015-12-08 14:43:47 +01:00
|
|
|
Lisp, while always maintaining Python interoperability.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2015-01-03 04:58:46 +01:00
|
|
|
|
2015-01-03 04:56:07 +01:00
|
|
|
Layout & Indentation
|
2015-01-03 04:58:46 +01:00
|
|
|
====================
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-11 07:03:05 +02:00
|
|
|
The #1 complaint about Lisp?
|
2018-06-21 08:43:36 +02:00
|
|
|
|
|
|
|
*It's too weird looking with all those parentheses! How do you even* **read** *that?*
|
|
|
|
|
|
|
|
And, they're right! Lisp was originally much too hard to read.
|
|
|
|
Then they figured out layout and indentation. And it was glorious.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
|
|
|
The Three Laws
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Here's the secret: *Real Lispers don't count the brackets.*
|
2018-06-21 08:43:36 +02:00
|
|
|
They fade into the background.
|
|
|
|
When reading Lisp, disregard the trailing closing brackets---those are for the computer, not the human.
|
2018-06-11 07:03:05 +02:00
|
|
|
As in Python, read the code structure by indentation.
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Lisp code is made of trees---Abstract Syntax Trees---not strings.
|
|
|
|
S-expressions are very direct textual representation of AST.
|
|
|
|
That's the level of *homoiconicity*---the level Lisp macros operate on.
|
|
|
|
It's not like the C-preprocessor or Python's interpolated eval-string tricks that see code as just letters.
|
|
|
|
That's not how to think of Lisp code; think tree structure, not delimiters.
|
|
|
|
|
|
|
|
1. Closing brackets must NEVER be left alone, sad and lonesome on their own line.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
.. code-block:: clj
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
(defn fib [n]
|
|
|
|
(if (<= n 2)
|
|
|
|
n
|
|
|
|
(+ (fib (- n 1))
|
2018-06-21 08:43:36 +02:00
|
|
|
(fib (- n 2))))) ; Lots of Irritating Superfluous Parentheses
|
|
|
|
; L.I.S.P. ;))
|
|
|
|
|
|
|
|
;; How the experienced Lisper sees it. Indented trees. Like Python.
|
|
|
|
(defn fib [n
|
|
|
|
(if (<= n 2
|
|
|
|
n
|
|
|
|
(+ (fib (- n 1
|
|
|
|
(fib (- n 2
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
|
|
|
;; We're trying to ignore them and you want to give them their own line?
|
|
|
|
;; Hysterically ridiculous.
|
2018-06-11 07:03:05 +02:00
|
|
|
(defn fib [
|
|
|
|
n
|
2018-06-21 08:43:36 +02:00
|
|
|
] ; My eyes!
|
2018-06-11 07:03:05 +02:00
|
|
|
(if (<= n 2)
|
|
|
|
n
|
|
|
|
(+ (fib (- n 1)) (fib (- n 2)))
|
|
|
|
)
|
2018-06-21 08:43:36 +02:00
|
|
|
) ; GAH, BURN IT WITH FIRE!
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
2. New lines must ALWAYS be indented past their parent opening bracket.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
.. code-block:: clj
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
(foo (, arg1
|
|
|
|
arg2))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. And evil.
|
|
|
|
;; Same bracket structure as above, but not enough indent.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foo (, arg1
|
2018-06-21 08:43:36 +02:00
|
|
|
arg2))
|
|
|
|
|
|
|
|
;; PREFERRED. Same indent as above, but now it matches the brackets.
|
|
|
|
(fn [arg]
|
|
|
|
arg)
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Remember, when reading Lisp, you ignore the trailing brackets.
|
|
|
|
;; Look at what happens if we remove them.
|
|
|
|
;; Can you tell where they should go by the indentation?
|
2018-06-11 07:03:05 +02:00
|
|
|
|
|
|
|
(foo (, arg1
|
|
|
|
arg2
|
|
|
|
|
|
|
|
(foo (, arg1
|
|
|
|
arg2
|
|
|
|
|
|
|
|
(fn [arg
|
|
|
|
arg
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; See how the structure of those last two became indistinguishable?
|
|
|
|
|
|
|
|
;; Reconstruction of the bad example by indent.
|
|
|
|
;; Not what we started with, is it?
|
|
|
|
(foo (, arg1)
|
|
|
|
arg2)
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Beware of brackets with reader syntax.
|
|
|
|
;; You still have to indent past them.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
`#{(foo)
|
|
|
|
~@[(bar)
|
|
|
|
1 2]}
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Above, no trail.
|
|
|
|
`#{(foo
|
|
|
|
~@[(bar
|
|
|
|
1 2
|
|
|
|
|
|
|
|
;; Reconstruction. Is. Wrong.
|
|
|
|
`#{(foo)}
|
|
|
|
~@[(bar)]
|
|
|
|
1 2
|
|
|
|
|
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
`#{(foo)
|
|
|
|
~@[(bar)
|
|
|
|
1
|
|
|
|
2]}
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
|
|
|
;; A string is an atom, not a HySequence.
|
|
|
|
(foo "abc
|
|
|
|
xyz")
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Still readable without trailing brackets.
|
|
|
|
(foo "abc
|
|
|
|
xyz" ; Double-quote isn't a closing bracket. Don't ignore it.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
3. New lines must NEVER be indented past the previous element's opening bracket.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
((get-fn q)
|
|
|
|
x
|
|
|
|
y)
|
|
|
|
|
|
|
|
;; The above with trailing brackets removed. See the problem?
|
|
|
|
((get-fn q
|
|
|
|
x
|
|
|
|
y
|
|
|
|
|
|
|
|
;; By indentation, this is where the brackets should go.
|
|
|
|
((get-fn q
|
|
|
|
x
|
|
|
|
y))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
|
|
|
((get-fn q) x
|
2018-06-11 07:03:05 +02:00
|
|
|
y)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; The above without trailing brackets. Still OK (for humans).
|
|
|
|
((get-fn q) x ; The ) on this line isn't trailing!
|
|
|
|
y
|
|
|
|
|
|
|
|
;; PREFERRED, since the ) should end the line.
|
2018-06-11 07:03:05 +02:00
|
|
|
((get-fn q)
|
|
|
|
x
|
|
|
|
y)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Limits
|
|
|
|
------
|
|
|
|
|
|
|
|
Follow PEP 8 rules for line limits, viz.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
+ 72 columns max for text (docstrings and comments).
|
|
|
|
+ 79 columns max for other code, OR
|
|
|
|
+ 99 for other code if primarily maintained by a team that can agree to 99.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Whitespace
|
|
|
|
----------
|
2018-06-12 04:09:38 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
AVOID trailing spaces. They suck!
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
AVOID tabs in code. Indent with spaces only.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
PREFER the ``\t`` escape sequence to literal tab characters in one-line string literals.
|
|
|
|
|
|
|
|
+ Literal tabs are OK inside multiline strings if you also add a warning comment.
|
|
|
|
+ But ``\t`` is still PREFERRED in multiline strings.
|
|
|
|
+ The comment should PREFERABLY appear just before the string.
|
|
|
|
+ But a blanket warning at the top of a function, class, or file is OK.
|
|
|
|
|
|
|
|
Alignment
|
|
|
|
---------
|
|
|
|
|
|
|
|
Line up arguments to function calls when splitting over multiple lines.
|
|
|
|
|
|
|
|
+ The first argument PREFERABLY stays on the first line with the function name,
|
|
|
|
+ but may instead start on the next line indented one space past its parent bracket.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. All args aligned with first arg.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction arg1
|
|
|
|
(barfunction bararg1
|
|
|
|
bararg2
|
2018-06-21 08:43:36 +02:00
|
|
|
bararg3) ; Aligned with bararg1.
|
2018-06-11 07:03:05 +02:00
|
|
|
arg3)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction arg1
|
|
|
|
(barfunction bararg1
|
2018-06-21 08:43:36 +02:00
|
|
|
bararg2 ; Wrong. Looks like a macro body.
|
|
|
|
bararg3) ; Why?!
|
2018-06-11 07:03:05 +02:00
|
|
|
arg3)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. Args can all go on one line if it fits.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction arg1
|
2018-06-21 08:43:36 +02:00
|
|
|
(barfunction bararg1 bararg2 bararg3)
|
2018-06-11 07:03:05 +02:00
|
|
|
arg3)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Args not on first line, but still aligned.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction
|
2018-06-21 08:43:36 +02:00
|
|
|
arg1 ; Indented one column past parent (
|
2018-06-11 07:03:05 +02:00
|
|
|
(barfunction
|
2018-06-21 08:43:36 +02:00
|
|
|
bararg1 ; Indent again.
|
|
|
|
bararg2 ; Aligned with bararg1.
|
2018-06-11 07:03:05 +02:00
|
|
|
bararg3)
|
2018-06-21 08:43:36 +02:00
|
|
|
arg3) ; Aligned with arg1.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Hold it Open
|
|
|
|
------------
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
If you need to separate a bracket trail use a ``#_ /`` comment to hold it open.
|
|
|
|
This avoids violating law #1.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
.. code-block:: clj
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
[(foo)
|
|
|
|
(bar)
|
|
|
|
(baz)]
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK, especially if the list is long. (Not that three is long.)
|
2018-06-11 07:03:05 +02:00
|
|
|
;; This is better for version control line diffs.
|
2018-06-21 08:43:36 +02:00
|
|
|
[ ; Opening brackets can't be "trailing closing brackets" btw.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foo)
|
|
|
|
(bar)
|
|
|
|
(baz)
|
2018-06-21 08:43:36 +02:00
|
|
|
#_ /] ; Nothing to see here. Move along.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Examples of commenting out items at the end of a list follow.
|
|
|
|
;; As with typing things in the REPL, these cases are less important
|
|
|
|
;; if you're the only one that sees them. But even so, maintaining
|
|
|
|
;; good style can help prevent errors.
|
2018-06-12 04:09:38 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD and a syntax error. Lost a bracket.
|
2018-06-11 07:03:05 +02:00
|
|
|
[(foo)
|
|
|
|
;; (bar)
|
|
|
|
;; (baz)]
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. Broke law #1.
|
2018-06-11 07:03:05 +02:00
|
|
|
[(foo)
|
|
|
|
;; (bar)
|
|
|
|
;; (baz)
|
|
|
|
]
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
|
|
|
;; The discard syntax respects code structure,
|
|
|
|
;; so it's less likely to cause errors.
|
2018-06-11 07:03:05 +02:00
|
|
|
[(foo)
|
|
|
|
#_(bar)
|
|
|
|
#_(baz)]
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Adding a final discarded element makes line comments safer.
|
2018-06-11 07:03:05 +02:00
|
|
|
[(foo)
|
|
|
|
;; (bar)
|
|
|
|
;; (baz)
|
|
|
|
#_ /]
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Snuggle
|
|
|
|
-------
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Brackets like to snuggle, don't leave them out in the cold!
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
[1 2 3]
|
|
|
|
(foo (bar 2))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
[ 1 2 3 ]
|
|
|
|
( foo ( bar 2 ) )
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. And ugly.
|
2018-06-11 07:03:05 +02:00
|
|
|
[ 1 2 3]
|
|
|
|
(foo( bar 2) )
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Grouping
|
|
|
|
--------
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Use whitespace to show implicit groups, but be consistent within a form.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Older Lisps would typically wrap such groups in even more parentheses.
|
|
|
|
;; (The Common Lisp LOOP macro was a notable exception.)
|
2018-06-11 07:03:05 +02:00
|
|
|
;; But Hy takes after Clojure, which has a lighter touch.
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. Can't tell key from value without counting
|
|
|
|
{1 9 2 8 3 7 4 6 5 5}
|
|
|
|
|
|
|
|
;; PREFERRED. This can fit on one line. Clojure would have used commas
|
|
|
|
;; here, but those aren't whitespace in Hy. Use extra spaces instead.
|
|
|
|
{1 9 2 8 3 7 4 6 5 5}
|
|
|
|
|
|
|
|
;; OK. And preferred if it couldn't fit on one line.
|
2018-06-11 07:03:05 +02:00
|
|
|
{1 9
|
|
|
|
2 8
|
|
|
|
3 7
|
|
|
|
4 6
|
2018-06-21 08:43:36 +02:00
|
|
|
5 5} ; Newlines show key-value pairs in dict.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
;; This grouping makes no sense.
|
|
|
|
#{1 2
|
|
|
|
3 4} ; It's a set, so why are there pairs?
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
|
|
|
;; This grouping also makes no sense. But, it could be OK in a macro or
|
|
|
|
;; something if this grouping was somehow meaningful there.
|
2018-06-11 07:03:05 +02:00
|
|
|
[1
|
|
|
|
1 2
|
|
|
|
1 2 3] ; wHy do you like random patterns? [sic pun, sorry]
|
|
|
|
|
|
|
|
;; Be consistent. Separate all groups the same way in a form.
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
{1 9 2 8
|
|
|
|
3 7 4 6 5 5} ; Pick one or the other!
|
2018-06-21 08:43:36 +02:00
|
|
|
|
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
{1 9 2 8 3 7 4 6 5 5} ; You forgot something.
|
|
|
|
|
|
|
|
;; Groups of one must also be consistent.
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-12 04:09:38 +02:00
|
|
|
(foo 1 2 3) ; No need for extra spaces here.
|
2018-06-21 08:43:36 +02:00
|
|
|
|
|
|
|
;; OK, but you could have fit this on one line.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foo 1
|
|
|
|
2
|
2018-06-21 08:43:36 +02:00
|
|
|
3)
|
|
|
|
|
|
|
|
;; OK, but you still could have fit this on one line.
|
2018-06-11 07:03:05 +02:00
|
|
|
[1
|
2018-06-21 08:43:36 +02:00
|
|
|
2]
|
|
|
|
|
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
(foo 1 2 ; This isn't a pair?
|
|
|
|
3) ; Lines or spaces--pick one or the other!
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRRED
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction (make-arg)
|
|
|
|
(get-arg)
|
|
|
|
#tag(do-stuff) ; Tags belong with what they tag.
|
|
|
|
#* args ; #* goes with what it unpacks.
|
2018-06-21 08:43:36 +02:00
|
|
|
:foo spam
|
|
|
|
:bar eggs ; Keyword args are also pairs. Group them.
|
2018-06-11 07:03:05 +02:00
|
|
|
#** kwargs)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. Spaces divide groups on one line.
|
|
|
|
(quux :foo spam :bar eggs #* with-spam)
|
|
|
|
{:foo spam :bar eggs}
|
|
|
|
|
|
|
|
;; OK. The colon is still enough to indicate groups.
|
|
|
|
(quux :foo spam :bar eggs #* with-spam)
|
|
|
|
{:foo spam :bar eggs}
|
|
|
|
;; OK.
|
|
|
|
("foo" spam "bar" eggs}
|
|
|
|
|
|
|
|
;; BAD. Can't tell key from value.
|
|
|
|
(quux :foo :spam :bar :eggs :baz :bacon)
|
|
|
|
{:foo :spam :bar :eggs :baz :bacon}
|
|
|
|
{"foo" "spam" "bar" "eggs" "baz" "bacon"}
|
|
|
|
|
|
|
|
;; PREFERRED
|
|
|
|
(quux :foo :spam :bar :eggs :baz :bacon)
|
|
|
|
{:foo :spam :bar :eggs :baz :bacon}
|
|
|
|
{"foo" "spam" "bar" "eggs" "baz" "bacon"}
|
|
|
|
|
|
|
|
;; OK. Yep, those are pairs too.
|
2018-06-11 07:03:05 +02:00
|
|
|
(setv x 1
|
|
|
|
y 2)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. This fits on one line.
|
|
|
|
(setv x 1 y 2)
|
|
|
|
|
|
|
|
;; BAD. Doesn't separate groups.
|
|
|
|
(print (if (< n 0.0)
|
|
|
|
"negative"
|
|
|
|
(= n 0.0)
|
|
|
|
"zero"
|
|
|
|
(> n 0.0)
|
|
|
|
"positive"
|
|
|
|
"not a number"))
|
|
|
|
|
|
|
|
;; BAD. And evil. Broke law #3. Shows groups but args aren't aligned.
|
|
|
|
(print (if (< n 0.0)
|
|
|
|
"negative"
|
|
|
|
(= n 0.0)
|
|
|
|
"zero"
|
|
|
|
(> n 0.0)
|
|
|
|
"positive"
|
|
|
|
"not a number"))
|
|
|
|
|
|
|
|
;; BAD. Shows groups but args aren't aligned.
|
|
|
|
;; If the then-parts weren't atoms, this would break law #3.
|
|
|
|
(print (if (< n 0.0)
|
|
|
|
"negative"
|
|
|
|
(= n 0.0)
|
|
|
|
"zero"
|
|
|
|
(> n 0.0)
|
|
|
|
"positive"
|
|
|
|
"not a number"))
|
|
|
|
|
|
|
|
;; OK. Redundant (do) forms allow extra indent to show groups
|
|
|
|
;; without violating law #3.
|
|
|
|
(print (if (< n 0.0)
|
|
|
|
(do
|
|
|
|
"negative")
|
|
|
|
(= n 0.0)
|
|
|
|
(do
|
|
|
|
"zero")
|
|
|
|
(> n 0.0)
|
|
|
|
(do
|
|
|
|
"positive")
|
|
|
|
"not a number"))
|
|
|
|
|
|
|
|
Separate toplevel forms (including toplevel comments not about a particular form)
|
|
|
|
with a single blank line, rather than two as in Python.
|
|
|
|
|
|
|
|
+ This can be omitted for tightly associated forms.
|
|
|
|
|
|
|
|
Methods within a defclass need not be separated by blank line.
|
|
|
|
|
|
|
|
Special Arguments
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Macros and special forms are normally indented one space past the parent bracket,
|
|
|
|
but can also have "special" arguments that are indented like function arguments.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
+ Macros with an ``&rest body`` argument contain an implicit ``do``.
|
|
|
|
+ The body is never special, but the arguments before it are.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
(assoc foo ; foo is special
|
|
|
|
"x" 1 ; remaining args are not special. Indent 2 spaces.
|
|
|
|
"y" 2)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
;; The do form has no special args. Indent like a function call.
|
|
|
|
(do (foo)
|
|
|
|
(bar)
|
|
|
|
(baz))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
|
|
|
;; No special args to distinguish. This is also valid function indent.
|
2018-06-11 07:03:05 +02:00
|
|
|
(do
|
|
|
|
(foo)
|
|
|
|
(bar)
|
|
|
|
(baz))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
|
|
|
(defn fib [n]
|
|
|
|
(if (<= n 2)
|
|
|
|
n
|
|
|
|
(+ (fib (- n 1))
|
|
|
|
(fib (- n 2)))))
|
|
|
|
|
|
|
|
;; OK
|
|
|
|
(defn fib
|
|
|
|
[n] ; name and argslist are special. Indent like function args.
|
|
|
|
;; The defn body is not special. Indent 1 space past parent bracket.
|
|
|
|
(if (<= n 2)
|
|
|
|
n
|
|
|
|
(+ (fib (- n 1)) ; Emacs-style else indent.
|
|
|
|
(fib (- n 2)))))
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Removing Whitespace
|
|
|
|
-------------------
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Removing whitespace can also make groups clearer.
|
2015-01-03 06:05:02 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
.. code-block:: clj
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; lookups
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
2018-06-11 07:03:05 +02:00
|
|
|
(. foo ["bar"])
|
2018-06-21 08:43:36 +02:00
|
|
|
|
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
(. foo["bar"])
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. Doesn't show groups clearly.
|
2018-06-11 07:03:05 +02:00
|
|
|
(import foo foo [spam :as sp eggs :as eg] bar bar [bacon])
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Extra spaces show groups.
|
2018-06-11 07:03:05 +02:00
|
|
|
(import foo foo [spam :as sp eggs :as eg] bar bar [bacon])
|
2018-06-21 08:43:36 +02:00
|
|
|
|
|
|
|
;; PREFERRED. Removing spaces is even clearer.
|
2018-06-11 07:03:05 +02:00
|
|
|
(import foo foo[spam :as sp eggs :as eg] bar bar[bacon])
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Newlines show groups.
|
2018-06-11 07:03:05 +02:00
|
|
|
(import foo
|
|
|
|
foo [spam :as sp
|
|
|
|
eggs :as eg]
|
|
|
|
bar
|
|
|
|
bar [bacon])
|
2018-06-21 08:43:36 +02:00
|
|
|
|
|
|
|
;; PREFERRED, It's more consistent with the preferred one-line version.
|
2018-06-11 07:03:05 +02:00
|
|
|
(import foo
|
|
|
|
foo[spam :as sp
|
|
|
|
eggs :as eg]
|
|
|
|
bar
|
|
|
|
bar[bacon])
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Avoid whitespace after tags.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
|
|
|
;; Note which shows groups better.
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction #tag "foo" #tag (foo) #* (get-args))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
|
|
|
(foofunction #tag "foo" #tag (foo) #* (get-args))
|
|
|
|
|
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction #tag"foo" #tag(foo) #*(get-args))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
|
|
|
;; Can't group these by removing whitespace. Use extra spaces instead.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction #x foo #x bar #* args)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
|
|
|
;; Same idea, but this could have fit on one line.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction #x foo
|
|
|
|
#x bar
|
|
|
|
#* args)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK, but you don't need to separate function name from first arg.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction #x foo #x bar #* args)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. But same idea.
|
|
|
|
;; No need to separate the first group from the function name.
|
2018-06-11 07:03:05 +02:00
|
|
|
(foofunction
|
|
|
|
#x foo
|
|
|
|
#x bar
|
|
|
|
#* args)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. It's still clear what this is tagging.
|
|
|
|
;; And you don't have to re-indent.
|
2018-06-11 07:03:05 +02:00
|
|
|
#_
|
|
|
|
(def foo []
|
|
|
|
stuff)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK, but more work.
|
2018-06-11 07:03:05 +02:00
|
|
|
#_(def foo []
|
|
|
|
stuff)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD, you messed up the indent and broke law #2.
|
2018-06-11 07:03:05 +02:00
|
|
|
#_(def foo []
|
|
|
|
stuff)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD, keep the tag grouped with its argument.
|
2018-06-11 07:03:05 +02:00
|
|
|
#_
|
|
|
|
|
|
|
|
(def foo []
|
|
|
|
stuff)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Close Bracket, Close Line
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
A *single* closing bracket SHOULD end the line,
|
|
|
|
unless it's in the middle of an implicit group.
|
|
|
|
|
|
|
|
+ If the forms are small and simple you can maybe leave them on one line.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
A *train* of closing brackets MUST end the line.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-11 07:03:05 +02:00
|
|
|
;; One-liners are overrated.
|
|
|
|
;; Maybe OK if you're just typing into the REPL.
|
2018-06-21 08:43:36 +02:00
|
|
|
;; But even then, maintaining good style can help prevent errors.
|
|
|
|
|
|
|
|
;; BAD. One-liner is too hard to read.
|
|
|
|
(defn fib [n] (if (<= n 2) n (+ (fib (- n 1)) (fib (- n 2)))))
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. Getting better, but the first line is still too complex.
|
|
|
|
(defn fib [n] (if (<= n 2) n (+ (fib (- n 1))
|
|
|
|
(fib (- n 2)))))
|
|
|
|
;; OK. Barely.
|
2015-01-03 06:05:02 +01:00
|
|
|
(defn fib [n]
|
2018-06-21 08:43:36 +02:00
|
|
|
(if (<= n 2) n (+ (fib (- n 1)) ; This line is pushing it.
|
|
|
|
(fib (- n 2)))))
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
2018-06-11 07:03:05 +02:00
|
|
|
(defn fib [n] ; Saw a "]", newline.
|
2018-06-21 08:43:36 +02:00
|
|
|
(if (<= n 2) ; OK to break here, since there's only one pair.
|
|
|
|
n
|
|
|
|
(+ (fib (- n 1)) ; Whitespace separation (Emacs else-indent).
|
|
|
|
(fib (- n 2)))))
|
|
|
|
|
|
|
|
;; OK
|
|
|
|
(defn fib [n] ; Saw a "]", end line. (Margin comments don't count.)
|
|
|
|
(if (<= n 2) n ; Saw a ")", but it's in a pair starting in this line.
|
|
|
|
(+ (fib (- n 1)) ; Saw a "))" MUST end line.
|
2018-06-11 07:03:05 +02:00
|
|
|
(fib (- n 2)))))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Pairs.
|
|
|
|
(print (if (< n 0.0) "negative" ; Single ) inside group. No break.
|
2018-06-11 07:03:05 +02:00
|
|
|
(= n 0.0) "zero"
|
|
|
|
(> n 0.0) "positive"
|
2018-06-21 08:43:36 +02:00
|
|
|
:else "not a number")) ; :else is not magic; True works too.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Avoided line breaks at single ) to show pairs.
|
2018-06-11 07:03:05 +02:00
|
|
|
(print (if (< n 0.0) "negative"
|
|
|
|
(= n 0.0) "zero"
|
2018-06-21 08:43:36 +02:00
|
|
|
(> n 0.0) (do (do-foo) ; Single ) inside group. No break.
|
2018-06-11 07:03:05 +02:00
|
|
|
(do-bar)
|
|
|
|
"positive")
|
2018-06-21 08:43:36 +02:00
|
|
|
"not a number")) ; Implicit else is PREFERRED.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
(print (if (< n 0.0) "negative"
|
|
|
|
(= n 0.0) "zero"
|
|
|
|
(and (even? n)
|
2018-06-21 08:43:36 +02:00
|
|
|
(> n 0.0)) "even-positive" ; Bad. "))" must break.
|
2018-06-11 07:03:05 +02:00
|
|
|
(> n 0.0) "positive"
|
|
|
|
"not a number"))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
(print (if (< n 0.0) "negative"
|
|
|
|
(= n 0.0) "zero"
|
|
|
|
(and (even? n)
|
2018-06-21 08:43:36 +02:00
|
|
|
(> n 0.0)) (do (do-foo) ; Y U no break?
|
2018-06-11 07:03:05 +02:00
|
|
|
(do-bar)
|
|
|
|
"even-positive")
|
|
|
|
(> n 0.0) "positive"
|
|
|
|
"not a number"))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Blank line separates multiline groups.
|
2018-06-11 07:03:05 +02:00
|
|
|
(print (if (< n 0.0) "negative"
|
|
|
|
|
|
|
|
(= n 0.0) "zero"
|
|
|
|
|
|
|
|
(and (even? n)
|
|
|
|
(> n 0.0))
|
|
|
|
(do (do-foo)
|
|
|
|
(do-bar)
|
|
|
|
"even-positive")
|
|
|
|
|
|
|
|
(> n 0.0) "positive"
|
|
|
|
|
|
|
|
"not a number"))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. Groups are not separated consistently.
|
2018-06-11 07:03:05 +02:00
|
|
|
(print (if (< n 0.0) "negative"
|
|
|
|
(= n 0.0) "zero"
|
|
|
|
|
|
|
|
(> n 0.0)
|
|
|
|
(do (do-foo)
|
|
|
|
"positive")
|
|
|
|
|
|
|
|
"not a number"))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Single )'s and forms are simple enough.
|
|
|
|
(with [f (open "names.txt")]
|
|
|
|
(-> (.read f) .strip (.replace "\"" "") (.split ",") sorted)))
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. Even so, this version is much clearer.
|
|
|
|
(with [f (open "names.txt")]
|
|
|
|
(-> (.read f)
|
|
|
|
.strip
|
|
|
|
(.replace "\"" "")
|
|
|
|
(.split ",")
|
|
|
|
sorted)))
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-11 07:03:05 +02:00
|
|
|
Comments
|
|
|
|
--------
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Prefer docstrings to comments where applicable---in ``fn``, ``defclass``,
|
|
|
|
at the top of the module, and in any other macros derived from these that can take a docstring
|
|
|
|
(e.g. ``defmacro/g!``, ``deftag``, ``defn``).
|
|
|
|
|
|
|
|
Docstrings contents follow the same conventions as Python.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-11 07:03:05 +02:00
|
|
|
The ``(comment)`` macro is still subject to the three laws.
|
|
|
|
If you're tempted to violate them, consider discarding a string instead with ``#_``.
|
|
|
|
|
2018-06-12 04:09:38 +02:00
|
|
|
Semicolon comments always have one space between the semicolon and the start of the comment.
|
2018-06-11 07:03:05 +02:00
|
|
|
Also, try to not comment the obvious.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Comments with more than a single word should start with a capital letter and use punctuation.
|
|
|
|
|
|
|
|
Separate sentences with a single space.
|
|
|
|
|
2015-01-03 04:56:07 +01:00
|
|
|
.. code-block:: clj
|
|
|
|
|
2018-06-12 04:09:38 +02:00
|
|
|
;; This commentary is not about a particular form.
|
2018-06-21 08:43:36 +02:00
|
|
|
;; These can span multiple lines. Limit them to column 72, per PEP 8.
|
2018-06-12 04:09:38 +02:00
|
|
|
;; Separate them from the next form or form comment with a blank line.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED.
|
|
|
|
(setv ind (dec x)) ; Indexing starts from 0,
|
|
|
|
; margin comment continues on new line.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
2018-06-11 07:03:05 +02:00
|
|
|
;; Style-compliant but just states the obvious.
|
2018-06-21 08:43:36 +02:00
|
|
|
(setv ind (dec x)) ; Sets index to x-1.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
(setv ind (dec x));typing words for fun
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-11 07:03:05 +02:00
|
|
|
;; Comment about the whole foofunction call.
|
|
|
|
;; These can also span mulitple lines.
|
|
|
|
(foofunction ;; Form comment about (get-arg1). Not a margin comment!
|
|
|
|
(get-arg1)
|
|
|
|
;; Form comment about arg2. The indent matches.
|
|
|
|
arg2)
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-12 04:09:38 +02:00
|
|
|
Indent form comments at the same level as the form they're commenting about;
|
2018-06-11 07:03:05 +02:00
|
|
|
they must always start with exactly two semicolons ``;;``.
|
|
|
|
Form comments appear directly above what they're commenting on, never below.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-12 04:09:38 +02:00
|
|
|
General toplevel commentary is not indented;
|
|
|
|
these must always start with exactly two semicolons ``;;``
|
2018-06-11 07:03:05 +02:00
|
|
|
and be separated from the next form with a blank line.
|
|
|
|
For long commentary, consider using a ``#_`` applied to a string for this purpose instead.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-12 04:09:38 +02:00
|
|
|
Margin comments start two spaces from the end of the code; they
|
2018-06-11 07:03:05 +02:00
|
|
|
must always start with a single semicolon ``;``.
|
|
|
|
Margin comments may be continued on the next line.
|
|
|
|
|
|
|
|
When commenting out entire forms, prefer the ``#_`` syntax.
|
2018-06-12 04:09:38 +02:00
|
|
|
But if you do need line comments, use the more general double-colon form.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
|
|
|
Coding Style
|
|
|
|
============
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Pythonic Names
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Use Python's naming conventions where still applicable to Hy.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
+ The first parameter of a method is ``self``,
|
|
|
|
+ of a classmethod is ``cls``.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Threading Macros
|
|
|
|
----------------
|
|
|
|
|
|
|
|
PREFER the threading macro or the threading tail macros when encountering
|
|
|
|
deeply nested s-expressions. However, be judicious when using them. Do
|
|
|
|
use them when clarity and readability improves; do not construct
|
|
|
|
convoluted, hard to understand expressions.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
;; BAD. Not wrong, but could be much clearer with a threading macro.
|
|
|
|
(setv NAMES
|
2018-06-11 07:03:05 +02:00
|
|
|
(with [f (open "names.txt")]
|
|
|
|
(sorted (.split (.replace (.strip (.read f))
|
|
|
|
"\""
|
|
|
|
"")
|
|
|
|
","))))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. This compiles exactly the same way as the above.
|
|
|
|
(setv NAMES
|
2018-06-11 07:03:05 +02:00
|
|
|
(with [f (open "names.txt")]
|
|
|
|
(-> (.read f)
|
|
|
|
.strip
|
|
|
|
(.replace "\"" "")
|
|
|
|
(.split ",")
|
|
|
|
sorted)))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD. Probably. The macro makes it less clear in this case.
|
2018-06-12 04:09:38 +02:00
|
|
|
(defn square? [x]
|
2018-06-11 07:03:05 +02:00
|
|
|
(->> 2
|
|
|
|
(pow (int (sqrt x)))
|
|
|
|
(= x)))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Much clearer that the previous example above.
|
2018-06-12 04:09:38 +02:00
|
|
|
(defn square? [x]
|
2018-06-11 07:03:05 +02:00
|
|
|
(-> x
|
|
|
|
sqrt
|
|
|
|
int
|
|
|
|
(pow 2)
|
|
|
|
(= x))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED. Judicious use.
|
|
|
|
;; You don't have to thread everything if it improves clarity.
|
2018-06-12 04:09:38 +02:00
|
|
|
(defn square? [x]
|
2018-06-11 07:03:05 +02:00
|
|
|
(= x (-> x sqrt int (pow 2))))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK. Still clear enough with no threading macro this time.
|
2018-06-12 04:09:38 +02:00
|
|
|
(defn square? [x]
|
2018-06-21 08:43:36 +02:00
|
|
|
(= x (pow (int (sqrt x)) ; saw a "))", break.
|
|
|
|
2)) ; aligned with first arg to pow
|
|
|
|
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Method Calls
|
|
|
|
------------
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Clojure-style dot notation is PREFERRED over the direct call of
|
|
|
|
the object's method, though both will continue to be supported.
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
.. code-block:: clj
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2015-01-03 06:05:02 +01:00
|
|
|
(with [fd (open "/etc/passwd")]
|
2015-01-03 04:56:07 +01:00
|
|
|
(print (.readlines fd)))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
2015-01-03 06:05:02 +01:00
|
|
|
(with [fd (open "/etc/passwd")]
|
2015-01-03 04:56:07 +01:00
|
|
|
(print (fd.readlines)))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Use More Arguments
|
|
|
|
------------------
|
|
|
|
|
|
|
|
PREFER using multiple arguments to multiple forms.
|
|
|
|
But judicious use of redundant forms can clarify intent.
|
|
|
|
AVOID the separating blank line for toplevel forms in this case.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
;; BAD
|
|
|
|
(setv x 1)
|
|
|
|
(setv y 2)
|
|
|
|
(setv z 3)
|
|
|
|
(setv foo 9)
|
|
|
|
(setv bar 10)
|
|
|
|
|
|
|
|
;; OK
|
|
|
|
(setv x 1
|
|
|
|
y 2
|
|
|
|
z 3
|
|
|
|
foo 9
|
|
|
|
bar 10)
|
|
|
|
|
|
|
|
;; PREFERRED
|
|
|
|
(setv x 1
|
|
|
|
y 2
|
|
|
|
z 3)
|
|
|
|
(setv foo 9
|
|
|
|
bar 10)
|
|
|
|
|
|
|
|
Imports
|
|
|
|
-------
|
|
|
|
|
|
|
|
As in Python, group imports.
|
|
|
|
|
|
|
|
+ Standard library imports (including Hy's) first.
|
|
|
|
+ Then third-party modules,
|
|
|
|
+ and finally internal modules.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
PREFER one import form for each group.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
PREFER alphabetical order within groups.
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
Require macros before any imports and group them the same way.
|
|
|
|
|
|
|
|
But sometimes imports are conditional or must be ordered a certain way for programmatic reasons, which is OK.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
;; PREFERRED
|
|
|
|
(require hy.extra.anaphoric [%])
|
|
|
|
(require thirdparty [some-macro])
|
|
|
|
(require mymacros [my-macro])
|
|
|
|
|
|
|
|
(import json re)
|
|
|
|
(import numpy :as np
|
|
|
|
pandas :as pd)
|
|
|
|
(import mymodule1)
|
|
|
|
|
|
|
|
Underscores
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Prefer hyphens when separating words.
|
|
|
|
|
|
|
|
+ PREFERRED ``foo-bar``
|
|
|
|
+ BAD ``foo_bar``
|
|
|
|
|
|
|
|
Don't use leading hyphens, except for "operators" or symbols meant to be read as including one,
|
|
|
|
e.g. ``-Inf``, ``->foo``.
|
|
|
|
|
|
|
|
Prefix private names with an underscore, not a dash.
|
|
|
|
to avoid confusion with negated literals like ``-Inf``, ``-42`` or ``-4/2``.
|
|
|
|
|
|
|
|
+ PREFERRED ``_x``
|
|
|
|
+ BAD ``-x``
|
|
|
|
|
|
|
|
Write Python's magic "dunder" names the same as in Python.
|
|
|
|
Like ``__init__``, not ``--init--`` or otherwise,
|
|
|
|
to be consistent with the private names rule above.
|
|
|
|
|
|
|
|
Private names should still separate words using dashes instead of underscores,
|
|
|
|
to be consistent with non-private parameter names and such that need the same name sans prefix,
|
|
|
|
like ``foo-bar``, not ``foo_bar``.
|
|
|
|
|
|
|
|
+ PREFERRED ``_foo-bar``
|
|
|
|
+ BAD ``_foo_bar``
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
;; BAD
|
|
|
|
;; What are you doing?
|
|
|
|
(_= spam 2) ; Throwing it away?
|
|
|
|
(_ 100 7) ; i18n?
|
|
|
|
|
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
;; Clearly subtraction.
|
|
|
|
(-= spam 2)
|
|
|
|
(- 100 7)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
;; This looks weird.
|
|
|
|
(_>> foo bar baz)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
;; OH, it's an arrow!
|
|
|
|
(->> foo bar baz)
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; Negative x?
|
|
|
|
(setv -x 100) ; BAD. Unless you really meant that?
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
;; Oh, it's just a module private.
|
2018-06-21 08:43:36 +02:00
|
|
|
(setv _x 100)
|
2018-06-11 07:03:05 +02:00
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; BAD
|
2018-06-11 07:03:05 +02:00
|
|
|
(class Foo []
|
|
|
|
(defn __init-- [self] ...))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK
|
2018-06-11 07:03:05 +02:00
|
|
|
(class Foo []
|
|
|
|
;; Less weird?
|
|
|
|
(defn --init-- [self] ...))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; PREFERRED
|
2018-06-11 07:03:05 +02:00
|
|
|
(class Foo []
|
|
|
|
(defn __init__ [self] ...))
|
|
|
|
|
2018-06-21 08:43:36 +02:00
|
|
|
;; OK, but would be module private. (No import *)
|
2018-06-11 07:03:05 +02:00
|
|
|
(def ->dict [&rest pairs]
|
|
|
|
(dict (partition pairs)))
|
2015-01-03 04:56:07 +01:00
|
|
|
|
|
|
|
Thanks
|
2015-01-03 04:58:46 +01:00
|
|
|
======
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2018-06-11 07:03:05 +02:00
|
|
|
+ This guide is heavily inspired from `@paultag`_ 's blog post `Hy Survival Guide`_
|
2015-01-03 04:56:07 +01:00
|
|
|
+ The `Clojure Style Guide`_
|
2018-06-11 07:03:05 +02:00
|
|
|
+ `Parinfer`_ and `Parlinter`_ (the three laws)
|
|
|
|
+ The Community Scheme Wiki `scheme-style`_ (ending bracket ends the line)
|
2018-06-21 08:43:36 +02:00
|
|
|
+ `Riastradh's Lisp Style Rules`_ (Lisp programmers do not ... Azathoth forbid, count brackets)
|
2015-01-03 04:56:07 +01:00
|
|
|
|
2017-02-07 20:25:40 +01:00
|
|
|
.. _`Hy Survival Guide`: https://notes.pault.ag/hy-survival-guide/
|
2015-01-03 04:56:07 +01:00
|
|
|
.. _`Clojure Style Guide`: https://github.com/bbatsov/clojure-style-guide
|
|
|
|
.. _`@paultag`: https://github.com/paultag
|
2018-06-11 07:03:05 +02:00
|
|
|
.. _`Parinfer`: https://shaunlebron.github.io/parinfer/
|
|
|
|
.. _`Parlinter`: https://github.com/shaunlebron/parlinter
|
|
|
|
.. _`scheme-style`: http://community.schemewiki.org/?scheme-style
|
|
|
|
.. _`Comment-Tips`: https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html
|
|
|
|
.. _`Riastradh's Lisp Style Rules`: http://mumble.net/~campbell/scheme/style.txt
|