Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • It sounds to me like you're confusing LSP with Principle of least surprise/astonishment, I believe that LSP has more clear-cut boundaries where this is violating them, though you are applying the more subjective POLA which is based on expectations which are subjective; that is each person expects something different than the next. LSP is based on contractual and well defined guarantees given by the provider, not subjectively assessed expectations guessed by the consumer. Commented Jan 8, 2013 at 20:05
  • @JimmyHoffa: You're right, and that's why I spelled out the exact meaning before I stated a simpler way of remembering LSP. However, if there is a question in my mind what "surprise" means then I'll go back and check the exact rules of LSP (can you really recall them at will?). Exceptions replacing functionality (or vice versa) is a difficult one because it doesn't breach any specific clause above, which is probably a clue that it should be handled an entirely different way. Commented Jan 8, 2013 at 20:26
  • 1
    An exception is an expected condition defined on the contract in my mind, think of a NetworkSocket, it may have an expected exception on send if it's not open, if your implementation doesn't throw that exception for a closed socket you're violating LSP, if the contract states the opposite and your subtype does throw the exception then you are violating LSP. That's more or less how I classify exceptions in LSP. As for remembering the rules; I could be wrong on this and feel free to tell me so, but my understanding of LSP vs POLA is defined in the last sentence of my comment above. Commented Jan 8, 2013 at 21:08
  • 1
    @JimmyHoffa: I had never considered POLA and LSP to be even remotely connected (I think of POLA in UI terms) but, now that you've pointed it out, they kind of are. I'm not sure they're in conflict though, or that one is more subjective than the other. Isn't LSP an early description of POLA in software engineering terms? In the same way as I get frustrated when Ctrl-C isn't Copy (POLA), I also get frustrated when an ImmutablePoint is mutable because it's actually a MutablePoint subclass (LSP). Commented Jan 8, 2013 at 21:30
  • 1
    I would consider a CircleWithFixedCenterButMutableRadius as a probable LSP violation if the consumer contract for ImmutablePoint specifies that if X.equals(Y), consumers may freely substitute X for Y, and vice versa, and must thus refrain from using the class in such a way that such substitution would cause trouble. The type might be able to legitimately define equals such that all instances would compare as distinct, but such behavior would likely limit its usefulness. Commented Mar 25, 2014 at 19:38