First of all, shout out to Kevin for reading my blog:
He wanted to make sure it was clear that you need to look at the specification rather than the implementation of a method in order to verify that a subclass method does not violate the LSP.
Kevin gave three examples of the same method with different specification. In the first two examples, #get_number has been stubbed out (given a return value that fits its expected behavior without actually containing any functionality). We need to look at the specification for the method, not the implementation, for clues on obeying LSP.
Implementation 1:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementation 1 | |
class Base | |
## returns a positive integer from 1..100, inclusive | |
def get_number | |
5 | |
end | |
end |
Implementation 2:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementation 2 | |
class Base | |
## gets the number of files in the current directory, otherwise raises DiskError | |
def get_number | |
5 | |
end | |
end |
Implementation 3:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementation 3 | |
class Base | |
def get_number | |
raise NotImplementedError | |
end | |
end |
Also, for the record, I didn't come up with all this on my own, I pretty much just paraphrased Kevin's email. I think I definitely understand the concept better now.
He also put this in the email, and I thought it was worth sharing:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_random_number | |
8 ## <-- number was chosen at random | |
end |
Today I also helped Aki and Dylan with some Bottle stuff. They're trying to push HTML form data into an sql database using Bottle, which is something I did in math-drill last year. I showed them my code as an example, but unfortunately it's a bit more complex than what they need. My code actually generates the form using python, which isn't something they need to worry about and just makes it harder for them to understand. If they need my help, hopefully they'll ask...
OK, Alex, the Cobert video was a great touch, and yes, a double shout out to Kevin for reading your blog! Finding mentors like Kevin is key to having a top rate educational experience like the one you have been privileged to experience thanks to him. I had never heard of the Liskov substitution principal until you started discussing it with Kevin.
ReplyDeleteThanks for the link to math-drill code. You're right, it is too much to be used as a first example for what they are trying to do. I referred them to the bottle zoo, which has examples which increase in complexity at a very modest pace.