It could be, preconditions I want to have on input It could be, explanations of specific things you're doing.
也可以是我对输入的预期是什么,也可以是,对代码,正在进行的特定操作的解释。
And the problem is, that we want to build this abstract data type, but we'd like to basically know what kind of object is it, and what functions actually belong to it, how do we use them?
然后我对它们运行了这个方法,就是这一小段代码这个方法,这有意义么?,当然没有了,对不对?,因为你知道,当你把两个极坐标,表示的点进行相加运算的时候?
You begin to realize that all of these big seemingly complicated pieces of software in websites that you use, if you just hone in on little pieces of them at a time and you realize oh I can do that and oh I can do that.
你开始意识到,你所登陆的网站上这些大而复杂的软件,如果你只对其中的一小块代码进行研究,你就会发现,哦,我也可以来做这个。
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.
通过这个我在提醒大家,在写代码的时候,要搞清楚对输入的期望,并思考下如何执行这些期望。
Right. So similarly, if you've got one line of code with a semicolon at the end saying, this is the end of the statement, it doesn't really make sense to continue that conversation to try to continue conceptually that puzzle piece with curly braces.
对的,类似的,如果你得到了一行代码,后面有一个分号,标志语句的结尾,这就没有意义来继续,那个会话来尝试对那段代码,使用花括号。
It's words to you, or to the reader of the code, that are telling you what's going on inside of this code. OK?
注释是一些对你,或者对代码阅读者说的一些话,告诉你这段代码,是干什么的,对不对?
And it becomes a powerful way of reasoning about the code, a powerful way of using the code, so those notions of specs are really important.
以保证你写对代码,这将成为分析代码,使用代码的重要手段,所以规范说明的概念。
So the interpreter is simply operating directly on your code at run time.
因此解释器在运行程序的时候,会直接的对你的代码进行操作。
So in your mind, if you are now the computer program and you are executing this thing from top to bottom, what just has happened verbally is we are stepping into the line of code that says sort left half of elements.
在你们看来,如果你现在是计算机程序,正在从上到下执行一系列指令,刚才所发生的就是我们现在已执行到了这行代码,它告诉你要对左半部分的元素排序。
And when I do this test, what I want to do, is say I'm going to pick the middle spot, and depending on the test, if I know it's in the upper half, I'm going to set my start at the mid point and the end stays the same, if it's in the front half I'm going to keep the front the same and I'm going to change the endpoint.
如果我知道目标数可能,再比中值点大的区间里,我可能就会把开始点设为中值点,而尾点不变,如果在小的那个区间里,就保持开始点不变而把尾点设为中值点,你们可以看到这儿的代码,就是这么做的,对不对?它是怎么做的?
And I suggest two things, right?
我在这儿的代码干了两件事情对不对?
Spelling. These are just dumb little things to look for. Did you spell all the identifiers the way you think you did. The problem is, when you read code you see what you expect to see.
拼写错误,你可能得找找这些粗心的小错误,你是否拼对了标识符,问题是当你阅读代码的时候,你看到的都是你以为的。
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.
如果我们现在去运行程序的话,希望我们会得到-噢,抱歉,我得去马上,改下代码,还有一些在底部的东西没被注释掉,对,就在这儿了,我不认为我们需要,这些东西了,好,我们把它们注释掉,好,那么我为什么要讲这些呢?
Sorry? OK. You're doing the whole thing for me What's the complexity just of this inner loop m How many times do I go through that loop?
就这一块代码的,我执行了多少次这个循环?,次对不对?,here?,Just,this,piece。,我一会儿就会去得到你的答案的?
Meaning, why in the world do I have to tell the reader that I'm binding x to the value three? All right?
意思就是,我为什么要,告诉代码阅读者,要把x绑定到3这个值上呢?,对不对?
We began here so at this point in the story the next thing that's going to happen is this line called Print F that says swap exclamation point, right?
我们从这里开始,在这里,下一步将要发生了的事情是,这行叫做printf的代码,打印:“交换完成!“对不?
Good programming style. All right? Things that we ought to do, as you put these pieces together in order to give you really good code.
优秀的编程风格,对不对?,为了得到真正优秀的代码,我们在把代码放到一起的时候。
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.
这样就对了,因为我在代码里面,没有任何的东西说,如果我改变了其中的一个值。
It says the element's not there. I can just stop.
好,这段代码就是这么做的对不对?
Pseudo code. Now, you're all linguistic majors, pseudo means false, this sounds like code that ain't going to run, and that's not the intent of the term. When I say pseudo code, what I mean is, I'm going to write a description of the steps, but not in a particular programming language.
伪代码,基于你们都是学语言的,伪代码意味着不正确,这听起来就像是没法运行的代码,这可不是我们学这个的目的,当我说到伪代码的时候,我的意思是要对步骤进行一个描述,而不是某种特定的编程语言。
I've certainly written code that has this problem, I've tried to use my own code that has this problem, and good to us, right, good hygiene, I'm going to use that word again here, of getting into the habit of writing defensive code up front, it's part of that collection of things that you ought to do is a great thing to do.
我举手了吧?,我当然写过犯这种错误的程序了,我也尝试过应用含有这种错误的代码,不错,好的卫生,我还是要用这个词,对我们有帮助,养成写防卫性,代码的好习惯,是你应该做的,一系列事情之一,也是我们日后要做的主要的事情。
I spotted an error here, I could have spotted it by running it on different test sets, and using prints things, another way of doing it, but this idea of at least simulating it on simple examples lets you check a couple of important questions.
操作么?我在这里发现了一个错误,我也可以通过运行不同的测试集,或者运用print声明来发现这个错误,但是至少应该用简单的例子来手工模拟下,代码的思想可以让你,对一些很重要的问题进行检验。
- So now if Christiana doesn't actually -- if either programmer or who's programming these little puzzle pieces, don't actually do anything with this return value, and I call, get int, she wants to hand this piece of paper -- to me, but if I don't actually assign -- no, no, I just messed up the demo.
现在如果Christiana没有-,如果程序员或谁在编写这段代码,不要对这个返回值做任何事情,我调用GetInt,她把这张纸给我,但是如果我没有赋值-,不,不,我刚才把这个演示搞乱了。
应用推荐