Last time I showed you how to use the how to design data recipe to design a simple
data definition. In this video what I'm going to do is
show you how to use the how to design functions recipe to design a function
that consumes data defined by your own data definition.
So this'll be a function that consumes what's called non primitive data.
In this case it'll be a function that consumes the type city name.
In this problem we're asked to design a function based on having a data
definition. So the problem says, use the city name
data definition below to design a function that produces true if the given
city is the best in the world. And we're allowed to decide whatever best
city we want. So what's going on here is that we know
that city name itself, that's consumed by the function is going to be represented
as a city name. City name type here.
So that let's us get started right away on our signature.
This function's going to consume a city name.
And let's see, we're producing true. So it's a yes no question.
We're representing it with a Boolean. So, that's going to be produce a Boolean.
Now, what do we gotta do here? Well, we produce true if the given city
is the best in the world. You could start with this as a purpose.
Produce, I'll just copy it. Let's see, the next step of the recipe is
the stub. We'll call this function best question
mark. Remember there's a convention that
predicates, the name of predicates, functions that produce a boolean value
should end in a question mark. So we'll say best of some cn and for the
stub I'll say false. And I'll just label this as the stub.
Some and now we can get on to the check expects.
And when we design a function using a data definition, this is a good time to
go back to the how to design data definitions web page.
Remember I get to that web page by first going to the design recipes page and then
going to how to design data. And then this an atomic data definition,
a simple atomic data definition, so I'll go there.
And in this web page, each kind of data definition has a section called guidance
on examples and tests. And this gives us some hints about making
check expects for this particular kind of data definition.
So, whenever you design a function using a data definition, go to this part of how
to design data definition webpage and get some of this guidance.
For our purposes now I'll tell you what it says as we keep working.
Lisksys says, that one or two examples are enough.
And that also is true of a function that produces a Boolean value.
If you have a function that produces a Boolean value, you should always have at
least two examples, one that produces true and one that produces false.
So let's see, if I say check expect best? And now I need to give some city name,
like let's say Boston and is that the best city in the world?
Well, remember the problem description said we were free to choose any best city
we want. So I loved Boston when I lived there but
let me say that it's not the best city in the world.
Let's use a imaginary city for the best city in the world so no one can possibly
be upset. We'll use Hogsmeade as our best city in
the world. Now that is the best city in the world.
And probably this is a good time to make the purpose more specific.
So we'll say it produced true if the given city is Hogsmeade.
Okay, so now I've got two tests and the stub.
I should run and I get that one of the two tests fail.
But both tests actually manage to run. So that tells me that my tests are well
formed. Both tests ran.
So now we can keep going. Let me start by making the screen bigger
again. So, let's see.
Signature, purpose, stub, check-expects, now the template.
Well the way we get the template now is we're going to put a note here that says,
took template from city name. Because this function consumes a city
name, city name is a data definition that we designed.
So we're going to take the template right here.
And what I'm going to do is copy it. I'll have this note about where I got it.
The first thing I gotta do is remember to rename it.
I'll rename it to best. And now here is the template.
Now let's see what I do next, this is a function that has two cases.
A false case and a true case. So a good first thing to do when you have
a function that has two cases is to add an if to the template.
So what I'm going to do is I'm going to copy the existing template.
I'll put an if in here and for both the question and the true and false answer
I'll put the copy of the template. This is a way now of telling me, listen
there's an if and in order to fill out all these parts dot dot dot cn is what
you have to work with. Let's see cn is the city name.
So let's see in one case the city name is Hogsmeade.
Let's see. If the city name is Hogsmeade then this
example right here tells me I should produce true.
So that's true and if the city name is not Hogsmeade then this example on the
purpose tell me I should produce false. So that's going to be false.
Let me run it. Oh, I forgot when I was up here to
comment out this stub, there we go. Both tests pass.
Now, let me make couple points here. One is, did you notice that there was a
point in the template where this was dot dot dot cn and then I deleted that and
replaced it with true. This is an important thing about
templates. Templates tell you everything you have
available to work with. They don't tell you you have to use all
of it. So often times you'll be deleting things
out of templates. Another point to make about this function
is, let's think about this. This says if string equals c and
Hogsmeade is true. Produce true.
Otherwise if string equals C and the Hogsmeade is false, it produce false.
When you follow a very structured approach to designing code, sometimes you
produce code that's a little bit more cumbersome than it needs to be.
This function could instead just be this. Let me comment out this version of it and
run it again. And now that's working too.
But, you know, don't worry if you produce this version of the function.
This is clear and it's well tested. It's just if you ever find yourself
writing if xxx true, false. That's always the same as just xxx.