go: all: avoid "Func always returns Value" in godocs

This is a generic issue after a specific case was reported, #26267.

As an example:

// ...
// ListenAndServe always returns a non-nil error.
func ListenAndServe(addr string, handler Handler) error

What is meant here is that, when the func returns, the error is always non-nil. However, the wording can be misinterpreted, and one may infer that the func will always return, or that it will return shortly or without blocking.

In general, none of those are meant. For example, if calling a function ends up in a panic or an os.Exit call, the function may not return any values. And some functions block indefinitely until stopped, such as http.ListenAndServe.

I propose that we reword all the “always return” sentences in godocs, to remove ambiguity:

// The returned error is always non-nil.

We already use this form in some cases:

src/bytes/buffer.go:262:// The returned error is always nil, but is included to match bufio.Writer's
src/bytes/reader.go:34:// The returned value is always the same and is not affected by calls
src/math/big/rat.go:399:// Denom returns the denominator of x; it is always > 0.
src/runtime/malloc.go:508:// h.arenas metadata. The returned size is always a multiple of
src/strings/builder.go:89:// The returned error is always nil.
src/strings/reader.go:33:// The returned value is always the same and is not affected by calls

There are quite a few likely cases:

$ git grep '^//.*always return' src | wc -l
42

Happy to look into this if we agree that the wording change is an improvement.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 15 (14 by maintainers)

Most upvoted comments

I’m not convinced - I still find that documenting pre and post conditions can be useful. In this particular case, it means that one can simply handle the error directly, without first checking that it’s non-nil.

Changing active to passive voice does not seem like an improvement. The modifier “always” is also useful. I don’t see why we have to throw away all instances of “always returns”. Reading the output of git grep '^//.*always return' these look fine to me.

With thanks for the suggestion, I think we should leave these as is, and remember in the future that always != immediately.