Comment

Comment by salehenrahman

> I feel like the real Liskov Subsitution Principle should be "if you have a subtype P of type S, redesign your code so that there are no subtypes".

Inheritance (a construct to allow you to create class hierarchies) is not the same thing as subtyping.

For instance, an integer is a subtype of a real number (assuming both, in some hypothetical language, are distinct primitive types).

In fact, LSP will work perfectly fine even in languages that don't support inheritance.

LSP decries that that the behaviour of a particular operation matches the expressed intent of said operation.

Going back to our integer being a subtype of real number example, the "<" operator will work perfectly fine whether any of the operands are either an integer or a real number.

So, I definitely agree with the sentiment "class hierarchies are useless", but I can't say the same about LSP.

In fact, if someone is passing me an object, I expect it to behave the way it does, as is decried by LSP.

Also, you may argue that I am being pedantic, however I fear some people may take what you are implying a bit literally (e.g. never subtype anything), so I thought I'd clarify.

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton