0:05
Let's summarize the methodology that we recommend you follow
when you wish to create a method.
The first aspect is to focus first
on "what" the method must do, that is, on "what"
and not on "how" the method will do what it has to do.
So think first of what the method must do
and especially, how to ensure that the method has no undesirable effects,
no "side effects",
as we discussed in the episode about method headers
with the example of the "sqrt", square-root, method.
The first step is thus to focus on "what"
rather than on "how".
Once this is clear, you can proceed to the second step which is:
what must the method receive from the rest of the program
to be able to work? Think about what it has to do.
For example, if the method is named "f" and if I call it here,
what should the rest of the program pass to this method
for it to work?
Third step:
you must determine the return type.
What will the method return to the rest of the program?
And for this, you ask yourselves:
does it make sense to write such a function call: "z = f(...)",
"f" being the name of the method,
does it make sense to write "z = f (something)"?
If it makes sense, then the return type will be the type of 'z'.
For example, writing "z = sqrt(2.0)" makes sense,
and so the return type of the "sqrt" method should be the same type as "z"
in this call; it would be a "double".
If it doesn't make any sense to write "z = function call",
then the method must return nothing,
and its return type must be "void".
2:16
Let's look at a practical example.
Suppose that we wish to create a method
which asks the user for a number,
where this number will be between two boundaries.
So we would ask the user to enter a number between 1 and 10.
Following the methodology,
we start by asking ourselves what it is we want to do exactly,
to design the method well.
And here, I suggest you start by writing comments
2:46
which clearly specify what you want to do.
So for example here, we should clearly specify
that it is a method which asks the user for a number
and that this number will be between 2 bounds that we will pass as parameters
3:10
This step, which consists in clearly specifying what the method must do,
where we are interested in the "what",
and not in the "how", is essentially conceptual,
but it is fundamental for a good design of the method.
You should not be careless about this step.
3:27
We can now go on to the second step which consists in asking ourselves
which parameters the method needs.
Here, we decided that it would receive two parameters:
one parameters specifying the lower bound,
which we can call, for example, "minimum";
and a parameter which specifies the upper bound,
which we can call "maximum".
3:53
In our example of asking the user for a number
between 1 and 10, we would then pass the value "1" as the first argument,
and the value "10" as second argument.
Once we are done with this second step,
which focused on parameters, we can pass to the third step,
which is to ask ourselves what the return type of the method will be.
What should the method return to the rest of the program?
To answer this, I suggest you write an assignment like:
"z = function call" so here, "z = demanderNombre(1, 10);".
4:28
with boundaries of, for example, 1 and 10.
And we should determine whether such a call makes sense.
Here, it clearly does
and the value to be stored in 'z' here
would be the one that the user gave us.
So a call like this makes sense
and so the return type of this method
will be the type of 'z' in such an expression.
So here, we expect to have a "double".
Meaning the return type is "double".
5:15
now and only now, we will attack the "how".
How the method must work.
How will it do what it has to do?
In other words, now we will start working on the method's body
and so we will add a block here, that is, two braces,
to write what the method must do here,
to ask the user for a number.