Skip to content

Khan/typed-context

Repository files navigation

This is the sample code for a post on the Khan Academy Engineering Blog describing how we use statically typed context.


Khan/typed-context is not accepting contributions. We’re releasing the code for others to refer to and learn from, but we are not open to pull requests or issues at this time.

Khan Academy is a non-profit organization with a mission to provide a free, world-class education to anyone, anywhere. You can help us in that mission by donating or looking at career opportunities.


There are 5 examples, each of which is described in the blog post. In each case the file thing.go contains a function DoTheThing which does the same things. They vary in how they access global & request specific elements.

There are 2 more examples added since the blog post. Some feedback on the original ideas was worth coding into an example. The Server Object is just a wrapper around a bunch of resources, which makes function signatures and call sites easy, but does very little to document dependencies. The Server Interface is essentially the Statically Typed Context, but without the context. Call sites end up with 2 parameters instead of 1, but some context purists might prefer it.

  1. Globals
  2. Parameters
  3. Context, with unsafe casting
  4. Context but safely
  5. Statically typed context
  6. Server object
  7. Server interface

The blog post explains the pros and cons, and explains why we use statically typed contexts at Khan Academy.

There's also a main that calls DoTheThing. Comparing the differences between the examples allows you to see how easy it is to write functions and call functions using the various techniques.

Each example also has a file mock.go which has stub implementations of the functions called in DoTheThing. The last example also has contexts.go which defines the interface types used in the example.

Finally there's a linter that can detect when a function declaration requires an interface type that is not used in the code. We use this linter to ensure every function declares the minimal interface possible, which ensures the interface list is an accurate list of dependencies. If you run it against example 5 it passes. In mocks.go there are lines like _ = ctx.Request() that exist to satisfy the linter. If those lines are removed the linter will fail.

We use statically typed contexts within Khan Academy. If you like the idea and are excited to use them at work, we're hiring.

About

Sample code for the Khan Academy blog about statically typed context

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages