I'm slowly sliding toward a hot take that docstrings as usually implemented in eg Python and Lisp are a mistake in the large, because you have to put them between the function definition and the function code body. Make your docstring too big and you push them too far apart.

@cks huhhhhhhhhh

that's a very interesting point

@cks In Common Lisp you can set the documentation string outside of the function definition because documentation is setf'able.

(defun sqr (x) (* x x)) (setf (documentation 'sqr 'function) "square a number if you want, i guess")

So you get projects that write all user-facing documentation outside of the files that define them, like in 3d-math: https://codeberg.org/shinmera/3d-math/src/branch/main/dual-quaternions/documentation.lisp

As a user though you're never actually looking at that file though of course. You use your editors tools.

3d-math/dual-quaternions/documentation.lisp at main

3d-math - A library implementing the necessary linear algebra math for 2D and 3D computations

Codeberg.org
@cks emacs has a few ways to "fold" docstrings to hide them. i would be surprised if you couldn't do that in vscode or other editors.

@cks yes, that’s always been my view of the Python docstrings implementation. If it’s 1-2 lines, having it between the function declaration and body is tolerable. But to have meaningful documentation, you’ve now visually disassociated the function name/parameters and implementation.

(I also find it much much more readable if the description of the function context / purpose comes before the function, eg as a comment. JavaDoc or similar is much better.)

@cks that's a thought I hadn't considered. I'll bear that in mind.

I have a strong bias in favour of docstrings as they are implemented and used in emacs. The other replies to this post taught that Common Lisp lets docstrings be hoisted syntactically elsewhere; surely elisp uninterrupted would not let themselves be upstaged..?

And no, of course they wouldn't. Elisp has two different ways of doing this: https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Documentation.html#:~:text=You%20can%20also,evaluates%20to%20a%20string%2e

Now I have a reason to do this and knowledge about how. Thanks!

Function Documentation (GNU Emacs Lisp Reference Manual)

Function Documentation (GNU Emacs Lisp Reference Manual)

@gnomon Python can in theory attach docstrings to functions from outside the function (because of course), but doing that is ... very much not Python normal style, and people would look at you funny.

(And I'm not quite sure how well it works if you try to do it to methods in a class, as opposed to true functions.)

@cks What do you think of Go's approach of pre-function comments?

@carlana I think they're a generally good idea, especially because Go also allows package-level documentation and you can even put it in a completely separate file that may have no code in it¹. The one limitation is that if you get verbose in function/etc documentation it pushes the actual code apart, but there are tradeoffs everywhere.

(And Go's choice means you can naturally document non-function things too.)

¹ eg https://github.com/siebenmann/sinksmtp/blob/master/doc.go

sinksmtp/doc.go at master · siebenmann/sinksmtp

Sinksmtp is a 'sink' SMTP server, one that does nothing more than record incoming email. - siebenmann/sinksmtp

GitHub