Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop doc-comment style guide #4361

Closed
ghost opened this issue Jan 5, 2013 · 17 comments · Fixed by #22549
Closed

Develop doc-comment style guide #4361

ghost opened this issue Jan 5, 2013 · 17 comments · Fixed by #22549
Labels
P-low Low priority

Comments

@ghost
Copy link

ghost commented Jan 5, 2013

With a lot of library code expected to change before the 1.0 release, it doesn't make much sense to do this now, but the core and std libraries should use a consistent style for API documentation. The wiki makes some attempt at specifying conventions, but these need to be fleshed out and actually applied to the codebase.

Right now, some doc-comments are written in the third-person indicative:

pub fn map<T, U>(opt: &Option<T>, f: fn(x: &T) -> U) -> Option<U> {
    //! Maps a `some` value by reference from one type to another

and some are written in the imperative:

pub fn chain<T, U>(opt: Option<T>,
                   f: fn(t: T) -> Option<U>) -> Option<U> {
    /*!
     * Update an optional value by optionally running its content through a
     * function that returns an option.
     */

These two examples illustrate another inconsistency: Some summaries (the first line of the comment) have no terminal punctuation, while others end with a period.

One more thing that varies is the comment style itself. Some doc-comments look like

/*!
 * foo...
 * bar...
 * baz...
 */

while others look like

/*!
 foo...
 bar...
 baz...
 */

Once these rules (and others) are codified, I'd be happy to start going through the doc-comments and updating them.

@steveklabnik
Copy link
Member

👍 for discussing this. I want to contribute docs, but I don't want to make more work for you after it's done. ;)

@sophiebits
Copy link
Contributor

For comparison, standard Python style is to use the imperative "Return the …", which I find works well:

http://www.python.org/dev/peps/pep-0257/#one-line-docstrings

@kud1ing
Copy link

kud1ing commented Jan 6, 2013

Go has settled on the non-imperative style: http://golang.org/pkg/

@bblum
Copy link
Contributor

bblum commented Jul 3, 2013

Visiting for triage.

Personally, I prefer imperative verbs, and a period at the end. As for doc comment style, I don't think we should enforce any particular way over another, at least not while the language defines multiple ways to do it. Also, my preferred way is

/**
 * doc string
 */

@emberian
Copy link
Member

emberian commented Jul 7, 2013

I also prefer the imperative. Style also needs to include a unified syntax for refering to types and arguments, so rustdoc can properly annotate/hyperlink them. That's down the road though

@msullivan
Copy link
Contributor

Visiting for triage. I have very little opinion about this.

@kud1ing
Copy link

kud1ing commented Jan 28, 2014

Descriptive style:

Imperative style:

@kud1ing
Copy link

kud1ing commented Jan 28, 2014

I prefer the descriptive versions myself because a method definition does not do anything until i tell it to:

class Machine
{
    // Self-destructs the machine, if necessary.
    void self_destruct();
};

// Self-destruct!
if ( emergency )
  machine.self_destruct();

@Armavica
Copy link
Contributor

I am sensible to kud1ing's argument. Furthermore, if you read aloud a line the way it is presented in the doc:

zero Returns the additive identity, 0.

then the declarative form makes it a real sentence "zero" returns the additive identity, 0..

@kud1ing
Copy link

kud1ing commented Jan 28, 2014

@Armavica: Which is actually a short form of The method "zero" returns the additive identity, 0.

Documentating and interface is presenting something to the audience. I think the imperative form makes more sense, when you explain what/how/why something is happening in the implementation.

@huonw
Copy link
Member

huonw commented Jan 29, 2014

There's also #9403 about the style of the examples in documentation.

@pnkfelix
Copy link
Member

pnkfelix commented Feb 6, 2014

P-low.

@brson
Copy link
Contributor

brson commented Mar 16, 2014

cc #12928

@lkuper
Copy link
Contributor

lkuper commented Mar 16, 2014

From the comments here, descriptive style (e.g., "Converts a byte to a string.") seems more popular than imperative style ("Convert a byte to a string."). I don't much care which way we go, but it would be nice to have an official decision on this.

@lilyball
Copy link
Contributor

I think that calling this the imperative style is wrong. It's not imperative, because it's not instructing anyone to do anything. I think it's the first-person present indicative. I suspect that the urge to write

/// Frob the twaddle.
fn frob() {}

stems from the mindset of "I am the function. What do I do?", so it's the first-person present indicative.

However, when reading documentation, most readers will naturally treat the function as a third-person singular subject instead of a first-person subject. To that end, the documentation should be written in the third-person singular instead of the first-person.


The only reasonable argument I can see for considering this to be the imperative rather than the first-person present indicative is when the docstring describes a function declaration that the user is expected to implement. This is very common in OO languages, but in Rust that only applies to trait methods. This case suggests the use of the imperative because it's telling you what you must do in order to implement the method correctly.

But I don't consider this argument to be persuasive. The primary use-case for documentation is telling the reader how to use the API, not how to implement it. The number of uses of an API vastly outstrips the number of implementations (in all but the most esoteric of cases). Therefore, I believe that it makes more sense to use the present indicative in the third-person singular than it does to use the imperative, even for trait methods.

@steveklabnik
Copy link
Member

Another thing to deal with: hypens or en dashes:

https://en.wikipedia.org/wiki/Dash#Relationships_and_connections

The preference for an en dash instead of a hyphen in these coordinate/relationship/connection types of terms is a matter of style preference, not inherent orthographic "correctness"; both are equally "correct", and each is the preferred style in some style guides.

bors added a commit that referenced this issue Aug 19, 2014
For crates `alloc`–`collections`. This is mostly just updating a few function/method descriptions to use the indicative style. 

cc #4361; I’ve sort of assumed that the third-person indicative style has been decided on, but I could update this to use the imperative style if that’s preferred, or even update this to remove all function-style-related changes. (I think that standardising on one thing, even if it’s not the ‘best’ option, is still better than having no standard at all.) The indicative style seems to be more common in the Rust standard library at the moment, especially in the newer modules (e.g. `collections::vec`), more popular in the discussion about it, and also more popular amongst other languages (see #4361 (comment)).
@steveklabnik
Copy link
Member

I have submitted an RFC: rust-lang/rfcs#505

steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 4, 2015
This chapter covers writing documentation in depth.

Fixes rust-lang#4361
Fixes rust-lang#12862
Fixes rust-lang#14070
Fixes rust-lang#14967
bors added a commit that referenced this issue Mar 7, 2015
This chapter covers writing documentation in depth.

Fixes #4361
Fixes #12862
Fixes #14070
Fixes #14967
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-low Low priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.