Software Design/Explicitly pass integration objects into functions

From Wikiversity
Jump to navigation Jump to search

Checklist questions:

  • Are integration objects (database connection pools, HTTP clients, API clients) passed into a function which needs them explicitly as parameters rather than read from constants or from the fields of the function's enclosing object?

Why[edit | edit source]

Integration object parameters to a function is a form of documentation: they make the semantics of the function more apparent to the caller and to developers studying the code of the function.

If an integration object requires some initialization (which is against the Don't expose init() function practice), reading the object from a field or a constant creates a potential for initialization race bugs, i. e. reduces code robustness.

Why not[edit | edit source]

Verbose function calls with extra arguments make client code bigger and less clear.

When an integration object is used in nearly every function in a class or a package, passing it to all of them doesn't convey additional information. In particular, although logger objects or time source objects could be seen as integration objects, if they were passed to a significant fraction of all functions in the system, it would stop being very useful from the documentation point of view.

Sources[edit | edit source]

  • Adam Warski (January 6, 2020). "ZIO environment meets constructor-based dependency injection".