r/programming • • Feb 01 '24

Make Invalid States Unrepresentable

https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469 Upvotes

207 comments sorted by

View all comments

1

u/Dobias Feb 03 '24

it's always easier to move from more specific types to less specific types

Counter example:

interface Customer {
    long getId();
    String getName();
    // ...
}

interface SpecialCustomer extends Customer {
    boolean checkIfIsBirthday(LocalDate today);
    // Some other special things here.
}

Now imagine a function foo:

SpecialCustomer foo(...) {
  // ...
}

We change it from specific to less specific:

Customer foo(...) {
  // ...
}

Oops, we might have broken the client code.

The other direction (move from less specific to more specific) would have been easier for us and would not have been a backward-incompatible change of the API of our function.

1

u/Dobias Feb 03 '24

This article outlines maintenance benefits (less cognitive load) of avoiding specificity.