text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
>> And, indeed, any time you feel
yourself copying and pasting, | 6,912.57 | 2.5 |
and not even changing code,
odds are there's a better way. | 6,915.07 | 2.63 |
And, indeed, there is. | 6,917.7 | 1.77 |
Let me go ahead and do a for loop,
even though the syntax might not | 6,919.47 | 3.04 |
come naturally yet. | 6,922.51 | 2.06 |
>> Do this three times, simply
by doing the following-- | 6,924.57 | 4.924 |
and I happen to know this from practice. | 6,929.494 | 1.666 |
But we have a number of examples now. | 6,931.16 | 1.65 |
And you'll see online
more references still. | 6,932.81 | 2.14 |
>> This is the syntax on line 6, that
much like Scratch that repeats | 6,934.95 | 2.84 |
block, repeat the following three times. | 6,937.79 | 2.3 |
It's a little magical for now. | 6,940.09 | 1.25 |
But this will get more,
and more familiar. | 6,941.34 | 1.71 |
>> And it's going to repeat
line eight three times, | 6,943.05 | 2 |
so that if I re-compile make cough,
dot slash cough, cough, cough, cough. | 6,945.05 | 7.34 |
It still works the same way. | 6,952.39 | 1.64 |
So that's all fine and good. | 6,954.03 | 1.52 |
But that's not very abstracted. | 6,955.55 | 2.65 |
>> It's perfectly correct. | 6,958.2 | 1.171 |
But it feels like there
could be an opportunity, | 6,959.371 | 1.999 |
as in the world of
Scratch, to kind of start | 6,961.37 | 2.38 |
to add some semantics here so that
I don't just have some for loop, | 6,963.75 | 3.78 |
and a function that says
cough, or does cough. | 6,967.53 | 2.337 |
You know what? | 6,969.867 | 0.583 |
Let me try to be a
little cooler than that, | 6,970.45 | 2.17 |
and actually write a function that
has some side effects, call it cough. | 6,972.62 | 3.47 |
>> And it takes no input, and
returns no value as output. | 6,976.09 | 4.74 |
But you know what it does? | 6,980.83 | 1.85 |
It does this-- printf,
quote unquote, cough. | 6,982.68 | 6.69 |
>> And now up here, I'm going
to go ahead and for int, | 6,989.37 | 3.01 |
i gets zero, i less than 3, i plus plus. | 6,992.38 | 3.69 |
I'm going to not do printf, which is
arguably a low level implementation | 6,996.07 | 3.7 |
detail. | 6,999.77 | 0.5 |
I don't care how to cough. | 7,000.27 | 1.083 |
I just want to use the cough function. | 7,001.353 | 1.887 |
And I'm just going to call cough. | 7,003.24 | 1.6 |
>> Now, notice the dichotomy. | 7,004.84 | 1.364 |
When you call a function, if you don't
want to give it inputs, totally fine. | 7,006.204 | 3.166 |
Just do open paren, close
paren, and you're done. | 7,009.37 | 2.41 |
>> When you define a function, or
declare a function's prototype, | 7,011.78 | 4.491 |
if you know in advance it's not
going to take any arguments, | 7,016.271 | 2.499 |
say void in those parentheses there. | 7,018.77 | 2.4 |
And that makes certain that you
won't accidentally misuse it. | 7,021.17 | 4.49 |
Let me go ahead and make cough. | 7,025.66 | 1.36 |
And, of course, I've made a mistake. | 7,027.02 | 1.52 |
>> Dammit, there's that
implicit declaration. | 7,028.54 | 1.87 |
But that's fine. | 7,030.41 | 0.915 |
It's an easy fix. | 7,031.325 | 1.265 |
I just need the prototype higher up
in my file than I'm actually using it. | 7,032.59 | 5.65 |
>> So now let me make cough again, nice. | 7,038.24 | 1.83 |
Now, it works. | 7,040.07 | 0.72 |
Make cough, cough, cough, cough. | 7,040.79 | 2.14 |
So you might think that we're really
just over engineering this problem. | 7,042.93 | 3 |
And, indeed, we are. | 7,045.93 | 0.833 |
This is not a good
candidate of a program | 7,046.763 | 2.107 |
at the moment for
refactoring, and doing what's | 7,048.87 | 3.06 |
called hierarchical decomposition,
where you take some code, and then | 7,051.93 | 3.715 |
you kind of factor things out, so as
to ascribe more semantics to them, | 7,055.645 | 3.145 |
and reuse it ultimately longer term. | 7,058.79 | 2.14 |
But it's a building block toward
more sophisticated programs | 7,060.93 | 2.56 |
that we will start
writing before long that | 7,063.49 | 2.11 |
allows us to have the vocabulary
with which to write better code. | 7,065.6 | 4.49 |
And, indeed, let's see if we
can't generalize this further. | 7,070.09 | 2.83 |
>> It seems a little lame that I, main,
need to worry about this darn for loop, | 7,072.92 | 5.064 |
and calling cough again and again. | 7,077.984 | 1.416 |
Why can't I just tell cough,
please cough three times? | 7,079.4 | 3.65 |
In other words, why can't I just
give input to cough and do this? | 7,083.05 | 5.12 |
>> Why can't I just say, in
main cough three times. | 7,088.17 | 3.1 |
And now, this is kind of magical. | 7,091.27 | 1.88 |
It's very iterative here. | 7,093.15 | 1.39 |
And it's, indeed, a baby step. | 7,094.54 | 1.4 |
>> But just the ability to say on
line eight, cough three times, | 7,095.94 | 3.31 |
it's just so much more readable. | 7,099.25 | 1.48 |
And, plus, I don't have to know
or care how cough is implemented. | 7,100.73 | 3.48 |
And, indeed, later in the
term and for final projects, | 7,104.21 | 2.25 |
if you tackle a project with
a classmate or two classmates, | 7,106.46 | 2.69 |
you'll realize that you're going to
have to, or want to, divide the work. | 7,109.15 | 3.22 |
>> And you're going to want to decide
in advance, who's going to do what, | 7,112.37 | 2.28 |
and in which pieces? | 7,114.65 | 0.833 |
And wouldn't it be nice
if you, for instance, | 7,115.483 | 2.037 |
take charge of writing main, done. | 7,117.52 | 2.58 |
And your roommate, or your
partner more generally, | 7,120.1 | 3.37 |
takes care of implementing cough. | 7,123.47 | 1.76 |
>> And this division, these
walls of abstraction, | 7,125.23 | 4.31 |
or layers of abstraction if
you will, are super powerful, | 7,129.54 | 2.77 |
because especially for larger,
more complex programs and systems, | 7,132.31 | 3.17 |
it allows multiple people to build
things together, and ultimately | 7,135.48 | 4.59 |
stitch their work together in this way. | 7,140.07 | 2.61 |
But, of course, we
need to now fix cough. | 7,142.68 | 2.652 |
We need to tell cough
that, hey, you know what? | 7,145.332 | 1.958 |
You're going to need to take an
input-- so not void, but int and now. | 7,147.29 | 3.94 |
Let's go ahead and put into
cough the int. i gets zero. | 7,151.23 | 3.94 |
>> i is less than how many times. | 7,155.17 | 1.72 |
I said three before. | 7,156.89 | 1.66 |
But that's not what I want. | 7,158.55 | 1.87 |
I want cough to be generalized to
support any number of iterations. | 7,160.42 | 5.1 |
>> So, indeed, it's n that I want,
whatever the user tells me. | 7,165.52 | 3.28 |
Now, I can go ahead and say print cough. | 7,168.8 | 2.82 |
And no matter what number
the user passes in, | 7,171.62 | 3.13 |
I will iterate that many times. | 7,174.75 | 2.14 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.