3
lorentz
1y

Turns out composition over inheritance won't save you from downcast hell, it just becomes `_ => abort()` hell.

Comments
  • 3
    Care to elaborate?
  • 2
    @Lensflare When the type hierarchy isn't very good in C++, C# and Java there's a tendency for downcasts to proliferate throughout the codebase. The mechanism by which this happens is usually as follows:

    - class with broad purpose has property of abstract class or interface type
    - specific code obtains instance of broad class which has specific meaning and therefore implies the subclass of the property
    - relying on this assumption, engineer adds downcast to specific code to quickly access subclass data without needing to establish proper pathways
    - since many specific fragments access the general class in specific ways, this repeats over and over. Whether the subclass assumptions are documented may change the severity of the problem but it's a problem nonetheless.

    1/2
  • 3
    In data driven programming, the problem changes. Mutually exclusive cases which were previously subclasses become sum types, and pattern matching allows to formulate complex conditions. But a bad data model is still a bad data model, only this time it manifests as a crashing catchall pattern at the end of an exhaustivity-tested list. Basically, an unknown or large number of representable values that are invalid in context.

    When the datastructure you're working with allows for a large number of variations that you can assert never to happen, the datastructure is probably wrong.

    2/2
  • 3
    No pattern will save a bad basic architecture.

    But some patterns offer more options to shot ones foot than others ;)
Add Comment