I'm not going to do it for you, but what would I think about doing when I change the code?
要改变代码应该做什么呢?,我不会帮你们做这些,但是我在改编代码的时候会想什么呢?
And notice because I have just one line of code inside of each of these branches, what have I clearly omitted?
请注意因为我在每个分支中只有一行代码,显然地我省略了什么?
And in the process of tidying it up and making it prettier, I'll often by accident find the bug.
让我的程序更漂亮些,在这个清理美化代码的过程中,我常常无意中发现我的错误。
What happens now after we've executed this third line of code, what happens next in the story?
我执行完第三行代码会发生什么呢,在这之后会发生什么呢?
I was being a little lazy on Wednesday in the interest of simplifying the code as much as possible.
在周三我在那个网站上尽可能地,简化代码的时候,我有点偷懒。
Right. If you look at the rest of that code, gee, it looks a lot like what I had elsewhere.
好了如果你看看剩下的代码,天呐,看上去就像我在别的地方写过的。
I want the function, which is a little tool, a line of code I can include in my own programs and generally those tools live in sections two or three of the manual.
我想要一个函数,那是一个小工具,在我程序中可以包含的一行代码,通常这些工具在手册的第二节,或第三节。
Let's assume, for sake of argument, the thing I'm looking for is bigger than this. In that case, I'm going to throw away all of this, I'm going to hit that bottom line of that code.
让我们假定不是,假定要找的元素,比这个大,在这样的情况下,这些都擦掉,我要运行到代码的最后一行。
Code is right here. And all it's doing is when I'm getting into the different places I'm simply printing out information about where I am. What I want you to see palindrome is notice what happened here.
代码就在这儿,它的作用是当我运行到不同地方时,就输出我所在方位的信息,我想让你们看的就是注意这儿发生了什么,好了我调用了一个。
And to see that I've written another version of this, which I'm going to use here, where I'm going to give it a little indentation.
我已经写另一个版本的代码,我将把它用在这儿,我会在这儿加一点儿缩进。
But I got a couple of other of these strange looking things in there with underbars to them.
我可以在这片代码中看到这些,然后我需要一些可以给我,返回这些信息的东西,但是这里还有一些看起来。
What does that say? It says if in the code up here I get an exception of that sort, I'm going to go to this place to handle it.
这意味着如果在代码中,我得到一个这样的异常,我能到这里来处理它。
And henceforth I'm gonna start calling these tools, these monickers that I can use as building blocks in my own program, it's gonna be called a function as we'll see.
从此以后,我将在我的程序里把这些工具,称为我们可以用来写代码的组件,在我自己的程序中,我们将了解到它被叫做为一个函数。
Which is a way of reminding you that I need to think as I write the code about what are my expectations from the input, and how might I enforce them.
通过这个我在提醒大家,在写代码的时候,要搞清楚对输入的期望,并思考下如何执行这些期望。
And I'll sit there staring at the output, staring at the code, not realizing that that output was not produced by that code.
然后我就坐在那儿盯着输出的结果,看着代码,没有意识到输出的结果,并不是由我的代码产生。
Suppose I want to compute square roots a lot of places in a big chunk of code.
假设我想在一大段代码中,计算很多次平方根。
Because if I don't specify a name for this input, I have no way of actually referring to it in subsequent lines of code.
因为如果我不给这个输入定义一个名字,在随后的代码中,我就没有办法使用它。
Well there are a couple of interesting intermediate values here, right? There's tmp.
回去再看第一段代码,那么在这里我该关注什么呢?
This also, by the way, in some sense, gives me the beginnings of my comments for what the structure of my code is going to be.
顺便说一句,在某种意义上这也是我,对于我的代码结构的,讲解的开始。
I want to just use it, I shouldn't have to worry about what variables I use inside of it, I have shouldn't have to worry about where that is in the code, I should be able to just abstract it away. And that's what we want to add today, are those two things.
我只是想使用它,我不用考虑我在这段代码中,使用的是什么变量,我完全不用考虑代码的内容,只要抽象就好,我们今天想补充的内容就是这两点。
I didn't have anything in there that says, if you change one of these values, other values depend on it, and I want to make that change to it.
这样就对了,因为我在代码里面,没有任何的东西说,如果我改变了其中的一个值。
Inside the curly braces is this program's code, what am I doing?
在花括号里是这个程序的代码,我在做什么?
Another way of saying it is, anything that uses get float doesn't care what the details are inside or shouldn't, and if I change that definition, I don't have to change anything elsewhere in my code, whereas if I just have the raw code in there, I have to go off and do it.
换种说法就是,任何用到获取,输入这个功能的人不用担心具体的实现细节,如果我改变了这里的实现,我并不需要去改变我的代码,因为我的最底处的源码就在这里,我去改这里就可以了。
I stress it in particular because I know you're all going to get into this stage; you've got a problem set due in a couple of hours, you're still writing the code, you don't want to waste time, and I'm going to use quotes on "waste time", doing those extra things to do the defensive programming, you just want to get the darn thing done.
我特别强调了这一点,因为我知道你们都会慢慢成为程序员;,你在写代码过程中造成了一系列的错误,你还在写代码,你不想浪费时间,我想把“浪费时间“用引号圈起来,浪费时间来做因为防卫性程序设计,而带来的这些额外的事情,你只想赶紧把程序写完,这不是一个好习惯,因为当你做完回头。
And what line of code am I on now?
那我现在在伪代码的哪一行呢?
Remember last time, I said that there's different kinds of complexity in our code, and I suggested for simple branching programs, the amount of time it takes to run that program is, in essence, bounded by the number of instructions, because you only execute each instruction at most once.
但是这里有个很重要的点,记得上节课,我提过在我们的代码中,有不同种类的复杂度,而且我还说了对于简单的分支程序,运行这种程序需要的总体时间,大体上,是和指令的数目相关的,因为每个指令只会被执行最多一次。
And I suggest two things, right?
我在这儿的代码干了两件事情对不对?
I can't give it what it wants," know that when you're writing code and the problems that will remind of this, if you absolutely have to kill your program, you don't have to close the window, CTRL+C reboot crazy stuff like that, generally you can hit CTRL+C and it will just abort the program right where it is, in case you ever get trapped in some awkward situation.
我不能给它所要的“,当你在写代码时,程序将,提醒这个问题,如果你不得不,关闭这个程序,你不需要关闭这个窗口,重启像那样的疯狂举动,通常你可以敲,它将会在这个地方终止那个程序,假使你陷入了尴尬的处境。
And in fact, if we just go ahead and run this now, hopefully we get out-- oops, sorry, I'm going to have to change this quickly, I still have some things uncommented at the bottom, yeah, there they are, I don't think we need that yet, all right, we will comment those out.
如果我们现在去运行程序的话,希望我们会得到-噢,抱歉,我得去马上,改下代码,还有一些在底部的东西没被注释掉,对,就在这儿了,我不认为我们需要,这些东西了,好,我们把它们注释掉,好,那么我为什么要讲这些呢?
If I go back to that code from a moment ago, and I just rerun math3, notice how many places printed after the decimal point -- two?
如果刚才我回到那段代码中去,我们回到math3,请注意在小数点后面,会打印几位数呢--两位?
应用推荐