What is the Liskov substitution principle? (Part 2)

_config.yml

Example of LSP violation

The ever cantankerous boss rages:

“Where is the the code you wrote yesterday? I want it now!!”

“Boss,” I meekly replied: “I don’t have yesterday’s code anymore, because I have updated it. You can have the updated code?”

“No gaddammit I want yesterday’s code!”

Why is it a LSP violation?

This is an example of a LSP violation because: the direct descendant of yesterday’s code – i.e. the updated code - is not substitutable for yesterday’s code: because the boss wants the old code and not the new. Nothing else will suffice.

Example of LSP compliance

The boss thinks about using the updated code, and responds begrudginly:

“Yeah ok I’ll have the updated code.”

“Yes sir, yes sir,” I meekly reply: “Three classes full sir”.

LSP compliance because you can substitute the new version of the code with what he expected (i.e. yesterday’s code) and everything is A-Okay!

And it’s as simple as that!

Written on January 5, 2017