-2

I read in Agile Principles Patterns and Practices in C# (Uncle Bob) book that, the presence of degenerate functions in derivatives is not always indicative of an LSP violation, but it's worth looking at them when they occur.

My question is, can someone give me an example when they don't violate LSP.

  • 1
    My understanding is that this type of question is better suited for Programmers SE: http://meta.stackexchange.com/questions/68384/whats-the-difference-between-stack-overflow-and-programmers-se – Volkan Paksoy Sep 21 '15 at 22:47
  • I don't understand why Programmers isn't on the list of sites to migrate to when you vote to close. – Chris Mantle Sep 21 '15 at 23:08
  • 2
    @ChrisMantle it used to be, but then people tended to use it as a toilet bowl for bad questions. – ratchet freak Sep 21 '15 at 23:09
  • 2
    @ChrisMantle Stack Overflow is a lot bigger than Programmers so giving non-moderators the ability to migrate questions there directly led to an avalanche of poorly scoped content, particularly as Programmers scope refined itself. –  Sep 21 '15 at 23:11
  • 2
    @ChrisMantle And we *still* get so many poor cross-posts from SO that we have a chatbot that tells us whenever an SO comment mentions Programmers. Which is why we're all counter-commenting you at the same time =) – Ixrec Sep 21 '15 at 23:13
  • It all makes sense - I'll remember the chatbot next time I think about pinging something over to Programmers :) – Chris Mantle Sep 21 '15 at 23:14
  • @ChrisMantle try this :http://www.mitsuku.com/ – user5360927 Sep 21 '15 at 23:45

1 Answers1

1

A good C# example, I think, is the various stream classes. The abstract Stream class defines a number of methods that may not apply to derived classes. I'm thinking specifically of the Can[...] methods, CanRead, CanSeek, CanTimeout, etc. On the face of it, they're degenerate methods in a derived class they don't apply to, but they are aspects of a stream that don't necessarily need to be implemented. A method that refers to the Stream abstract class can query CanRead, for example, and react accordingly based on the result, even if it's a degenerate implementation, as long as the degenerate implementation returns the right answer. In this way, a Stream implementation might have degenerate methods, but not violate LSP.

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
  • The abstract Stream class defines a number of methods that may not apply to derived classes.give me an example.(MemoryStream implements CanRead, CanSeek, CanTimeout) – user5360927 Sep 21 '15 at 21:56
  • `MemoryStream` implements them, yes, but can a memory stream time-out? It wouldn't have to implement any logic, just return `false`. Perhaps a better candidate for a true degenerate method on `Stream` would be `WriteByte`. A stream that's only ever read-only would not implement it. – Chris Mantle Sep 21 '15 at 22:24
  • Okay I understand you but what about this: "The f function in Base is implemented but in Derived is degenerate. Presumably, the author of Derived found that function f had no useful purpose in a Derived. Unfortunately, the users of Base don't know that they shouldn't call f, and so there is a substitution violation." page 198 – user5360927 Sep 21 '15 at 22:51
  • 1
    In the case of `Stream`, the user of a derived class knows (or should know) to query `CanWrite` before calling `WriteByte`. `WriteByte` is degenerate, but the user _knows_ they shouldn't call it if `CanWrite` is false, so it doesn't violate LSP. – Chris Mantle Sep 21 '15 at 22:59