Skip to content

proposal: Go2: rename "context" and (maybe) trim functionality #27987

Closed
@deanveloper

Description

@deanveloper

The problem with the name "context"

When I first started working with "context", I personally had a lot of trouble figuring out what the package even does. The name is not intuitive and suggests that it's purpose is for data transfer rather than cancelling tasks. While this is one of its purposes, after reading a number of articles, it really isn't good practice to use it. So really, "context" ends up just being used for cancellation of long-running tasks.

Why we should rename it

Instead, what we should have is a "cancel" package. The purpose is much more clear, and we can make names much more intuitive. I've personally made a quick mock-up of a "cancel" package, the implementation is MUCH simpler than "context", in fact, parent cancelers don't even need knowledge of their children. This may not be not important though as really implementation does not matter, as long as it is fast.

I find that the code is much more clear as to what it does, I've used it in a few internal projects at the company I work for. A lot of people there have never used Go before, here were my observations before/after the switch from "context" to "cancel":

  • Back when I used "context", it was hard to explain
    • The name was not intuitive to its task
    • This made explaining what it did very difficult
    • This also made reading code that used contexts difficult to read
  • Once switching to "cancel"
    • The name was intuitive, making it easy to explain
    • Even the "advanced" functionality (mainly functionality like parent Cancellers cancelling child Cancellers) was easy to explain because it had an intuitive name
    • People who were new to the language were able to use it easier
    • The trimmed functionality prevented misuse of the "data communication" aspect of "context"

Currently no public repositories use my "cancel" package (which makes sense, there's currently a stdlib package that does the exact same thing, and the stdlib package is most likely much more efficient).

Repetition

One of the design aspects in Go was to get rid of the whole Foo foo = new Foo() repetition, but with "context", we have func ExecContext(ctx context.Context). My package did not aim to fix this, as it has canc cancel.Canceller, but the repetition may be something to think about if we update "context" for Go2.

Other?

  • Of course we don't need to rename to "cancel", that's just the example I used.
  • Maybe the new package should have far fewer package-level functions?
    • ie canc, f = canc.Deadlined(deadline) instead of canc, f = cancel.WithDeadline(canc, deadline)
    • My API uses many package-level functions because I wanted it to be familiar to those who used "context" before.
  • Perhaps make "context" compatible with the new design? This is how I have designed my API, so any context.Context implements a cancel.Canceller.

Conclusion

The "context" package is hard to explain to new people because the name doesn't fit its purpose very well (at least for how it ended up being used). This proposal is to rename it to a more fitting name, and perhaps remove the "request-scoped values" aspect (as this part of the API is unintuitive, requires boilerplate for both setting and getting the value, and it is much more type-safe to just use function parameters anyway).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions