So I'm going to comment those out, and let's go down to this piece of code, and uncomment it.
那么我会把这些注释上,让我们看看这段代码,取消注释。
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?
请注意因为我在每个分支中只有一行代码,显然地我省略了什么?
I will be posting code that you can play with, and I suggest you go through exactly this kind of exercise.
我给你一些你用的上的代码,我建议你们,多进行一些这种实验。
I've written a little for loop, which is going to iterate over all of the elements in the list.
让我们先看看这儿的代码,我已经写了一个循环语句,用来迭代处理数组中所有的元素。
I'm going to start somewhere near the middle of the code. Again, a lot of times, people don't do that.
我会在代码中间的某个位置开始,再说一次,很多时候人们不会这么做。
What happens now after we've executed this third line of code, what happens next in the story?
我执行完第三行代码会发生什么呢,在这之后会发生什么呢?
All right, those pieces of code inherently involved something that does depend on the size of the input, or on the particular input.
好,这些代码,肯定会涉及到一些,与输入的大小相关的东西,或者说与特定输入相关。
And we will show you exactly how you can write programs that actually interact with the user in that way.
我们将给大家示范如何,书写代码以及跟用户交互。
The other thing you want to do is find the part of the program that is most likely at fault.
你们要做的另外一件事就是找出,最可能出错的那一部分代码。
What we will distribute for most lectures is a handout that's mostly code examples that we're going to do.
我们在大多课堂上会散发一些,印着我们要做的,代码范例的资料。
I was being a little lazy on Wednesday in the interest of simplifying the code as much as possible.
在周三我在那个网站上尽可能地,简化代码的时候,我有点偷懒。
All right, you can see that this little piece of code, it's got a loop in there, and what's it doing?
好,你们看到的这一小块代码,是做了一个循环吧?,他在做什么?
Right. If you look at the rest of that code, gee, it looks a lot like what I had elsewhere.
好了如果你看看剩下的代码,天呐,看上去就像我在别的地方写过的。
You will be able to infer from some of our code how in fact you can implement some more sophisticated programs.
你可以从一些代码中推断出,应该要怎样实现一些较复杂的程序。
And they're awfully simple because, all I can do now, is decide whether to execute some piece of code or another.
它们之所以太简单的原因是:,现在我能做的,就是,决定执行一块代码或者执行另外一块。
Having now used this pseudo code to line things up, I can start putting some additional structure on this.
用伪代码把这些事情串起来后,我可以开始进行,一些结构方面的优化了。
But there's a key line of code that I lost over a moment ago and that was this one here.
但是我刚才漏了一行关键的代码,就是这里的这一行。
What is that? It's the piece of code we wrote for computing square roots, square roots of actually perfect squares.
对吧?这是什么呢?,这是我们写的计算平方根的代码,计算完全全平方根的。
It could be, preconditions I want to have on input It could be, explanations of specific things you're doing.
也可以是我对输入的预期是什么,也可以是,对代码,正在进行的特定操作的解释。
All right. The fourth thing I'm going to do, is I'm going to then construct the block of code.
好,我要做的第四件事情就是,我会去写这块循环代码。
Because I might have written some code in which I'm expecting that integer particular variable to have an integer value.
因为我可能写了一些代码,来把一些,变量的类型,指定为。
But if I did have two lines of code or more, I would in fact need to put those back.
但是如果我的确有两行或更多的代码,我其实应该把花括号加上。
You've got to do it above this chunk of code, you've got to do it inside this chunk of code.
你不得不在一大段代码之上来实现它,你不得不在一大段代码之内来实现它。
And so at the end of the day, a hundred lines later, it should get to one hundred percent.
所以,最终,100行代码之后,它将100%完成了。
I've read through the same thing twenty times and I've missed something obvious. Someone who's never seen it before looks at and says, did you really mean to do this?
但是它们有一双旁观者之眼,我把同一段代码读上20遍,都会漏掉一些很明显的错误,从没看过这段代码的人会告诉我?
In general, good programming style says you put in comments that are going to be valuable in helping you as a reader understand what's going on inside of the code.
通常来说,优秀的编码风格意味着,你要放进去的注释,在帮助代码阅读者,理解代码内部,是怎么工作的。
But the things we were trying to stress were that it's an important design decision when you are coming up with a piece of code, as to what kind of efficiency your code has.
但是我们这里要强调的是,当你写代码的时候,决定你的代码的效率的很重要的事情,就是你的设计决定。
I'm gonna zoom in a little more and notice it's pretty much 1, 2, 3, 4, 5, 6, 7, 8, 9 lines of code and that's kind of rounding up because of the white space.
我现在放大一点,大家会看到,2,3,4,5,6,7,8,9行代码,多亏了空格,这些代码才能看起来如此美观。
So when you define a function, you have, as we've seen these curly braces, that define all ; of the code that's associated with that function; open curly brace code, close curly brace, that is the function.
当你定义一个函数,就像我们看到的这些花括号,定义了与,那个函数相关的所有代码;,左花括号,代码,右花括号,这就是一个函数。
应用推荐