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

Comments

Projects
None yet
@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

This comment has been minimized.

Copy link
Member

steveklabnik commented Jan 5, 2013

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

@sophiebits

This comment has been minimized.

Copy link
Contributor

sophiebits commented Jan 6, 2013

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

This comment has been minimized.

Copy link

kud1ing commented Jan 6, 2013

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

@bblum

This comment has been minimized.

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
 */
@cmr

This comment has been minimized.

Copy link
Member

cmr 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

This comment has been minimized.

Copy link
Contributor

msullivan commented Aug 23, 2013

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

@kud1ing

This comment has been minimized.

Copy link

kud1ing commented Jan 28, 2014

Descriptive style:

Imperative style:

@kud1ing

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

Armavica commented Jan 28, 2014

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

huonw commented Jan 29, 2014

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

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Feb 6, 2014

P-low.

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Mar 16, 2014

cc #12928

@lkuper

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

lilyball commented Jun 30, 2014

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

This comment has been minimized.

Copy link
Member

steveklabnik commented Aug 5, 2014

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

auto merge of #16241 : P1start/rust/doc-fixes, r=alexcrichton
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

This comment has been minimized.

Copy link
Member

steveklabnik commented Dec 8, 2014

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

steveklabnik added a commit to steveklabnik/rust that referenced this issue Feb 19, 2015

TRPL: Documentation
This chapter covers writing documentation in depth.

Fixes rust-lang#4361
Fixes rust-lang#12862
Fixes rust-lang#14070
Fixes rust-lang#14967

steveklabnik added a commit to steveklabnik/rust that referenced this issue Feb 23, 2015

TRPL: Documentation
This chapter covers writing documentation in depth.

Fixes rust-lang#4361
Fixes rust-lang#12862
Fixes rust-lang#14070
Fixes rust-lang#14967

steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 4, 2015

TRPL: Documentation
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

Auto merge of #22549 - steveklabnik:doc_documentation, r=huonw
This chapter covers writing documentation in depth.

Fixes #4361
Fixes #12862
Fixes #14070
Fixes #14967

@bors bors closed this in #22549 Mar 7, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.