Skip to main content
10 events
when toggle format what by license comment
May 23, 2014 at 22:24 comment added user7043 @Prog Absolutely (if by "return all the strings" you mean in the same order). The client code was either too accepting (too broad, too liberal) by asking for a WordBank (perhaps there is some subtype which does guarantee original order), or it misread the contract. Either way it's a problem with the client code and need to be fixed there.
May 23, 2014 at 22:20 vote accept Aviv Cohn
May 23, 2014 at 22:20 comment added Aviv Cohn And also: if the 'contract' of getWords() in WordBank is "do some or no sorting of strings, and return the resulting list of strings", but the client code was using getWords() while assuming it meant "always return all the strings", the code will break when subsituting with a WordSorter - but that's the client code's fault, not WordSorter's fault. Right?
May 23, 2014 at 22:15 comment added user7043 @Prog Yes, that sounds good. I also added an example to the answer.
May 23, 2014 at 22:15 history edited user7043 CC BY-SA 3.0
added 204 characters in body
May 23, 2014 at 22:13 comment added Aviv Cohn I see. So it depends on what getWords() in WordBank was supposed to do in the first place. If it's meant to 'return all of the strings it contains', than there's an LSP violation. But if it's meant to 'return part of the strings it contains', then it's not an LSP violation since this contract is fulfilled both with WordBank and WordSorter. Is my understanding correct?
May 23, 2014 at 22:07 comment added user7043 @Prog Again, it completely depends on the contract of WordBank. The sole fact that it returns different data (assuming WordBank wasn't abstract) is not a problem. It would be quite silly to mandate that subtypes are identical. They only must be compatible and what's compatible depends entirely on what you're doing. It's up to the person defining WordBank. If they say a WordBank returns the same words in the same order, it's an LSP violation. If they say a WordBank returns a subset of the word in arbitrary order, then it's fine. It's a weaker contract but a valid and useful one.
May 23, 2014 at 21:57 comment added Aviv Cohn Thanks for answering. I think that what I'm asking is this: Say you have a WordBank instance and a WordSorter instance. They are both fed the same array of strings. In this case, these two lines of code: wordBank.getWords() and wordSorter.getWords() - return different data. I guess that my question is: a WordSorter will return different data than a WordBank. The data will be of the same type, and the method signatures are the same - so no compile error will occur by substituting a WordBank with a WordSorter. But the data is different. Is this a violation of LSP?
May 23, 2014 at 21:44 history edited user7043 CC BY-SA 3.0
added 324 characters in body
May 23, 2014 at 21:39 history answered user7043 CC BY-SA 3.0