Whether this is the best place for this topic is a little muddy, #dev:internals is a “best guess” from me, mostly from it not fitting cleanly into General Usage.
I’m currently putting together docstring linter, that goes beyond checking that public functions have docstrings to also perform checks like:
- Signatures actually match the function being documented
- All arguments in the signature are mentioned in the docstring
- Paragraphs consist of complete sentences.
- Docstrings start with a brief summary line.
- etc.
As part of this, I’m also checking whether verb voicing is imperative or descriptive. I think it’s worth pushing people to one style for consistency, but I’m not sure which is best.
Imperative mood
In the imperative mood, docstrings are a complete sentence that’s written as an instruction the computer follows.
Examples
- “Return the number of widgets in a set.”
- “Control access to the phlebotinum.”
- “Set the printer on fire.”
Advantages
- Clarity/conciseness: the imperative mood tends to be more direct and to the point.
- It’s grammatically a complete sentence, with no implicit subject.
Drawbacks
- Perceived abruptness
- Potential misinterpretation of the target of the directive (the computer)
Indicative mood
When the indicative mood is used with docstrings, the subject is implied, rather than explicit — forming an adjectival phrase rather than a complete sentence.
Examples
- “[This function] returns the number of widgets in a set.”
- “[This type] controls access to the phlebotinum.”
- “[This module] sets the printer on fire.”
Advantages
- More natural language flow: most people naturally describe things indicatively
- Flexible expression: an indicative mood accommodates a broader variety of sentence structures
Drawbacks
- Implicit context/grammatical issues: the description is an incomplete sentence with a dangling antecedent.
- Potential verbosity/stylistic inconsistency
Related links
- Moods of the Verb - My English Grammar
- https://stackoverflow.com/questions/59902102/why-is-imperative-mood-important-for-docstrings
- https://dpb.bitbucket.io/the-imperative-style-in-commits-and-docstrings.html
- styleguide | Style guides for Google-originated open-source projects
- How to Write Doc Comments for the Javadoc Tool
Choice quotes
Some of this reading has me leaning more towards pushing for an imperative style. Here are two particular excerpts:
In a computer program a function is ultimately an event and a process, even in most languages that are organized on functional or object-oriented principles. The docstring serves as a description of the effect of that process. If we read a docstring as a second-person command, we can take it to be directed not at the function itself (directing it to act in a certain way) but at the processor that carries out the function.
[With an imperative style] international contributors don’t have to do as much work to figure out if a simple present conjugation of a verb is irregular as it would be, for example, with the verb “to go”. The correct conjugation with my approach would be “Goes”, but English is weird so maybe let’s just be kind and let people use the simple unconjugated verb “Go” to start the docstring.
A poll
What do you think? I’d be interested in hear thoughts, as well as conducting a strawpoll on preferred grammatical mood.
- Imperative mood
- Indicative mood (descriptive)