I know about the LSP, which requires that objects of a superclass shall be replaceable with objects of its subclasses without breaking the application.
However I've been building frontend applications for 5 years now and I have never come across a situation where I would replace an object of a superclass by an object of its subclass, perhaps because:
- Classes are not that used in JS: composition is favored over inheritance and composition is simpler when only using objects and functions
- When an object is used and it is needed to replace it with an object of its subtype, it often goes along with a change in feature, so a change in behavior is expected
- Complex objects are often framework-specific objects that rarely implement inheritance: think about Vue components, redux stores, or Angular services
The only places where I encountered the LSP was in clean code courses and job interviews, and 90% of these encounters only mentionnedmentioned the very abstract example where the Square
class shouldn't extend the Rectangle
class.
So, it surprises me that the LSP is often mentionnedmentioned as a cornerstone of clean code, yet I have never seen it in the wild. Perhaps I haven't seen it because the inheritance is not always explicit? Can I see a realistic bit of code where the LSP applies?