Designing with dataclasses

Writing more structured code with dataclasses

Table of contents

  1. Introduction
  2. Rewriting imperative functions as dataclasses
  3. Inheritance and architectural implications
  4. A note on typing

Keep it @dataclass-y

While redesigning a set of stored values computed from some parameters, I realised that classes (specifically dataclasses) would be better than function calls 'frozen' with partial.

Here I discuss some insights about the sort of design this leads to, and demonstrate my preference with minimal code examples.

As usual, the goal is to lighten the cognitive load: - when reasoning about it during writing (trying to achieve a certain behaviour, or test that behaviour), - re-reading your own code after the context has faded from memory, - and when sharing it with others who wish to reason about it.

Technically dataclasses date back to 3.7, but many features are new in 3.10: an important one I discuss here is kw_only.

Thanks for reading! 🚀