Don’t Worry About Anything, Just Call Us
Callbacks are great and always have been. Long ago, we didn’t know we were injecting control or inverting dependencies; we just used them. When closures appeared, I did not see a huge difference: a little state here, a little context there. I have found my Closures Killer App: remote cleanup
Here is an example in Go:
In the calling code (with dependencies on DataDog, Redis, etc.)
type cleanupSignature func(aName, aType string) func()
cleanupFactory := func(aName, aType string) func() {
if _, found := SpanFromContext(context); !found {
span, _ := datadog.SpanFromContext(context, aName, aType)
return func() {
span.Finish()
}
} else {
return func(){}
}
}
baz := foo(cleanupFactory, bar)
// ...
In the called code (with no dependencies on context, DataDog, etc.)
func foo(cleanupFactory cleanupSignature, zap int) string {
...