We won't make you write dramas like Pirandello, but we're going to try make you at least write good code.
我们的函数中的东西,我们不会让你去写皮兰德娄的荒诞剧,但是我们会教你写漂亮的代码。
OK, so far though, I've just shown you what happens inside a peace a code. It raises an exception. It goes to that accept clause. We don't have to use it just inside of one place.
好了,我已经给你们看了,代码内部发生了什么,它报了一个异常,然后就到了处理的片段,我们不该就在一个地方用它。
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.
因为如果我不给这个输入定义一个名字,在随后的代码中,我就没有办法使用它。
So this means we can code for it in DNA, you don't have to worry how am I going to get into the cell.
这意味着我们可以在DNA中编写它,你不用担心它怎么进到细胞里去。
So you know from algebra I can't actually solve this. There may be multiple solutions to this. What would I have to do to change my code? And the answer is fortunately not a lot.
我解不出来方程,可能会有多组答案,我需要怎样修改我的代码呢?,很幸运的,答案是不用修改很多。
Imagine instead, you're writing code that's a hundred thousand lines, a million lines, of code. You don't want to write it in this form. All right, and the reason you don't want to do that is, well, several.
换一个角度来思考,你正在写的代码有几十万行,近百万行,你绝对不想写成这种格式,好吧,你不想这么做的原因有以下几个。
Well, you won't be surprised to hear it's the same solution. So let's look at that code.
好了,听到二者是一样你可别惊讶,让我们看看代码。
If I decide there's a better way to get input, and there is, then I can make it to change what I don't have to change the code that uses the input.
如果我觉得有更好的获取输入的方法,我可以直接改去用这种方式,而不用更改使用了,输入处的代码。
I don't happen to have one today because we're not going to do a lot of code.
今天我恰巧没带来,因为我们不会做很多代码。
When we start writing code, you're going to see that difference, but for now, don't worry about it, I just want to plant that idea.
当我们开始写代码的时候,你就会看到区别了,但是现在别担心,我只是想让你们初步有个印象。
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.
我只是想使用它,我不用考虑我在这段代码中,使用的是什么变量,我完全不用考虑代码的内容,只要抽象就好,我们今天想补充的内容就是这两点。
Today, I'm gonna start writing C code with you but I can't just write it and expect it to do anything.
现在,我和大家一起写C语言代码,但我不指望写出来后就能跑起来。
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.
我特别强调了这一点,因为我知道你们都会慢慢成为程序员;,你在写代码过程中造成了一系列的错误,你还在写代码,你不想浪费时间,我想把“浪费时间“用引号圈起来,浪费时间来做因为防卫性程序设计,而带来的这些额外的事情,你只想赶紧把程序写完,这不是一个好习惯,因为当你做完回头。
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.
伪代码,基于你们都是学语言的,伪代码意味着不正确,这听起来就像是没法运行的代码,这可不是我们学这个的目的,当我说到伪代码的时候,我的意思是要对步骤进行一个描述,而不是某种特定的编程语言。
There is this include which allows me to include code, source code so to speak that other people wrote so I don't have to figure out for myself how to print things to the screen.
这个“include“可以让我加进一些代码来,比方说其他人写的源码,这样我就不用自己解决,如何在屏幕上输出了,别人已经为我解决这个问题。
It becomes a 13 and that is consistent with the code and it also doesn't crash because now y has been assigned to the correct value.
它变成了一个13,这个和代码是一致的,程序也不会崩溃,因为现在y被赋了正确的值。
I can't rely on Professor Guttag if I give this- if this code to him, I can't rely on him to type in a float. Actually I can, because he's a smart guy, but in general, I can't rely on the user-- I wouldn't do it right to see if you did.
当Guttag使用这段代码的时候-,我不能相信他一定会输入一个浮点数,实际上我能因为他很聪明,但是总体上来讲,别相信用户一定会按照规则来-,我会故意做错来试试你的程序的。
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.
换种说法就是,任何用到获取,输入这个功能的人不用担心具体的实现细节,如果我改变了这里的实现,我并不需要去改变我的代码,因为我的最底处的源码就在这里,我去改这里就可以了。
So now, in this case, this code is going to, when we get here, check, and if you haven't seen that strange thing there, that exclamation point in bang computer-ese called a bang, it says x if ANS star ANS is not equal to x, all right?
那么现在,在这个例子中,当我们执行到这里,代码会去检查,如果你没有看到过这个奇怪的东西,这个感叹号在计算机学中被叫做,代码的意思是如果ANS的平方不等于?
So don't underestimate the value for the next week or two of just inserting printf statements into your code temporarily just to print out what the value of some variable is, or some expression is, so that you can just do a sanity check.
所以不要低估下一两周的重要性,虽然我们只将“printf“语句暂时地植入到,我们的代码中,只是用来打印一些,变量或表达式的值,所以你们提醒一下自己。
All right. If you flip to the second side of your handout, you'll see that I have a piece of code there, that does this-- let me, ah, I didn't want to do that, let me back up slightly- this is the algorithm called search.
你可以看到实现这个功能的代码,让我,额,我不想这么做,让我们回来一点,这就是命名为search的算法-,我们来看看这个算法吧。
And you can write your source code in it but to compile something you don't run a command like GCC and then the name of the file, you instead click a button.
你们可以在上面写源代码,如果要编译的话,不用像GCC这样要输入命令,文件名,只需点击按钮就可以了。
OK, so how can I code this up? Well, you might think, let's just jump in and write some code, I don't want to quite do that though, because I want to show you another tool that's valuable for thinking about how to structure the code, and that is a something called a flow chart. Now.
好,那么我怎么把这个以代码实现呢,你可能会想,让我们进去直接写代码把,但是我不想这么做,因为我想教给大家另外一项很有价值的,用来组织代码的工具,叫做流程图,现在,像Guttag教授,和我这么大岁数的人。
We don't expect everybody to be a course six major, contrary to popular opinion, so for those are you not in that category, the second thing we want to do is we want to help students who don't plan to major in course six to feel justifiably confident in their ability to write and read small pieces of code.
和大众意见正相反,我们不希望所有人都变成course,six的主修学生,所以对于那些不在第一梯队里的人来说,我们想做的第二件事情就是,希望帮助还不想,主修这门课的人,都能获得在自己能力范围内,读写小编码的自信。
应用推荐