Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ; and ;; in Clojure code comments?

What is the difference between ; and ;; when starting a comment in Clojure? I see that my text editor colours them differently, so I'm assuming there is notionally some difference.

I also see that Marginalia treats them differently:

; Stripped entirely ;; Appears in text section of marginalia (defn foobar []    ; Appears in code section of marginalia output    ;; Again, appears in code section of marginalia output    6) 
like image 480
pauldoo Avatar asked Feb 22 '11 21:02

pauldoo


People also ask

How do you comment on Clojure?

#_ is the comment reader macro that instructs the Clojure reader to completely ignore the next form, as if it had never been written. No value is returned, so this comment is safe to use within an expression.

How do you comment multiple lines in Clojure?

If you want to comment multiple lines, you need to prepend all the lines with ; , which are normally a hassle, but usually text editors will do it for you with a command after selecting multiple lines.


2 Answers

There is no difference as far as the interpreter is concerned. Think of ; ;; ;;; and ;;;; as different heading levels.

Here is my personal use convention:

;;;; Top-of-file level comments, such as a description of the whole file/module/namespace  ;;; Documentation for major code sections (i.e. groups of functions) within the file.  ;; Documentation for single functions that extends beyond the doc string (e.g. an explanation of the algorithm within the function)  ; In-line comments possibly on a single line, and possibly tailing a line of code 
like image 162
G__ Avatar answered Oct 13 '22 10:10

G__


Check out the official description of the meaning of ; vs ;; in elisp: since the Clojure indenter is basically the same, it will treat them similarly. Basically, use ; if you are writing a long sentence/description "in the margins" that will span multiple lines but should be considered a single entity. Their example is:

(setq base-version-list                 ; there was a base       (assoc (substring fn 0 start-vn)  ; version to which              file-version-assoc-list))  ; this looks like                                         ; a subversion 

The indenter will make sure those stay lined up next to each other. If, instead, you want to make several unrelated single-line comments next to each other, use ;;.

(let [x 99 ;; as per ticket #425       y "test"] ;; remember to test this   (str x y)) ;; TODO actually write this function 
like image 40
amalloy Avatar answered Oct 13 '22 10:10

amalloy