Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting doc-strings on data vars, is it considered idiomatic?

So I have defined some vars to hold state data in my clojure code. I have just discovered I can add a doc-string to those vars e.g.:

(def ^{:doc "Documentation for *my-var*"}
        *my-var*)

That lets me call (doc *my-var*) at the REPL. This seems like a valid and useful thing to do but it doesn't seem like common practice in the (limited) code I have read.

Is this considered idiomatic clojure?

like image 498
Alex Stoddard Avatar asked Oct 14 '10 21:10

Alex Stoddard


People also ask

What are docstring explain with example?

As mentioned above, Python docstrings are strings used right after the definition of a function, method, class, or module (like in Example 1). They are used to document our code. We can access these docstrings using the __doc__ attribute.

What are doc strings in Python?

A Python docstring is a string used to document a Python module, class, function or method, so programmers can understand what it does without having to read the details of the implementation. Also, it is a common practice to generate online (html) documentation automatically from docstrings.

When to use docstrings Python?

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Docstrings are for people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation.

What are docstrings how are they useful?

A docstring is just a regular python triple- quoted string that is the first thing in a function body or a module or a class. When executing a functionbody the docstring does not do anything like comments but Python stores it as part of the function documen-tation.


2 Answers

Since clojure 1.3, def has allowed an optional docstring.

(def *my-var*
  "My var does cool things (it really doesn't)."
  nil)
like image 147
deadghost Avatar answered Oct 14 '22 19:10

deadghost


Also used in Clojure namespaces (like clojure.pprint):

(def
 ^{:doc "The base to use for printing integers and rationals."
   :added "1.2"}
 *print-base* 10)

You may wan't to use a convenience macro from clojure.contrib.def:

(defvar *my-var*
  nil
  "Documentation for *my-var*")
like image 45
Jürgen Hötzel Avatar answered Oct 14 '22 20:10

Jürgen Hötzel