And in the process of tidying it up and making it prettier, I'll often by accident find the bug.
让我的程序更漂亮些,在这个清理美化代码的过程中,我常常无意中发现我的错误。
You should acquire overtime this instinctive aversion to just code or programs that just look ugly because things that look ugly generally are not easy to maintain.
对于代码和程序你会有一种本能的讨厌,这是因为这些代码和程序看起来很丑陋,并且不容易维护。
And the robot or the computer or what have you is just automatically, mechanically following the code commands of the program.
那些机器人,或叫做计算机等等,只是自动地,机械地执行程序的命令代码
And that does two things: bug one, it helps catch bugs in your code, and secondly it often converts it into a more efficient sequence of instructions before you actually go off and run it. All right?
这有两点好处:,第一是可以帮助你发现程序中的,第二是把你的代码,在运行之前,转换为更有效率的指令的集合,对不对?
s1 It's a little square, it's going to be called s1 and that's what exists as of line two of this program, GetSting but now I've called get string.
这是一个正方形,叫做1,这就是程序中第二行代码所表示的,现在我调用。
So when you're writing a program in this language and many others, if you wanna make a comment to yourself or for other people who are reading this with their TF or fellow colleagues, you use what are called comments.
当你用这样那样的语言写程序时,为了便于自己或他人,阅读我们写的代码,我们必须得做一些注解,这就是所谓的注释。
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.
从此以后,我将在我的程序里把这些工具,称为我们可以用来写代码的组件,在我自己的程序中,我们将了解到它被叫做为一个函数。
Now in fairness, that particular problem kind of happened because so many companies ended up running so much code for many more years than the programmers actually thought they would be running it, but the idea is still the same.
公平起见,那个问题有时会发生,因为实际上,很多企业在这么多年里运行了,比程序员多得多的代码,虽然他们将运行它,但那个构想还是一样的。
Having reached the end of the block, go back up and check the test.
执行到了这块代码的最后,程序会回去再做一次测试。
I'm going to use my own special program that will color code things for me in class, but the idea is the same; it's a text editor.
我将用我自己专门的程序,那个会分类为我标出代码的颜色,但是那个思想是一样的,这是一个文本编辑器。
So the interpreter is simply operating directly on your code at run time.
因此解释器在运行程序的时候,会直接的对你的代码进行操作。
Inside the curly braces is this program's code, what am I doing?
在花括号里是这个程序的代码,我在做什么?
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.
我特别强调了这一点,因为我知道你们都会慢慢成为程序员;,你在写代码过程中造成了一系列的错误,你还在写代码,你不想浪费时间,我想把“浪费时间“用引号圈起来,浪费时间来做因为防卫性程序设计,而带来的这些额外的事情,你只想赶紧把程序写完,这不是一个好习惯,因为当你做完回头。
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.
我想要一个函数,那是一个小工具,在我程序中可以包含的一行代码,通常这些工具在手册的第二节,或第三节。
> That lets the compiler know that you want to use some actual compiled code from that library.
>,那是告诉编译器我们想要在程序库中,使用一些当前的编译后的代码。
And because there's nothing else in the code at that point, the program should just quit naturally in a good way. Yeah?
因为在那个阶段的没有其它的代码了,这个程序将以一种比较好的方式自然地退出,请说?
But for the next week or two when you're writing programs, at least for the first time, generally if you're trying Scratch to debug them like you might have been trying to troubleshoot Scratch, you're probably going to reason - through it by looking through the your code -- -- top to bottom, maybe engage a staff member for help -- but your friend will also be printf.
但是,在接下来的一两周内,当你们写程序的时候,或至少在第一次写的时候,通常如果你在调试程序,就像你们可能已经试着调试过,你们或许是通过查看你们的代码,来推断你们的程序-,从头到尾,可能还需要别人的帮忙-,但是你的朋友也将要打印输出。
So I can now ignore that.
是不在程序的第一部分代码中的。
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.
我不能给它所要的“,当你在写代码时,程序将,提醒这个问题,如果你不得不,关闭这个程序,你不需要关闭这个窗口,重启像那样的疯狂举动,通常你可以敲,它将会在这个地方终止那个程序,假使你陷入了尴尬的处境。
Because as you'll soon find, when you're writing programs 0 that aren't just four lines long but are 40 or 400 which very quickly becomes easy, you'll find that even you the next morning have no idea what this code does for whatever reason, and just having some English comments is a huge boon to comprehension for yourself and others.
你们自己写程序时就会发现,用40行甚至400行代码来代替繁杂拥挤的4行代码0,将会让事情简化许多,你会发现,即使由于某些原因,你不知道如何写代码,先写些注释,于人于己,都是好事,易于理解嘛。
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.
我举手了吧?,我当然写过犯这种错误的程序了,我也尝试过应用含有这种错误的代码,不错,好的卫生,我还是要用这个词,对我们有帮助,养成写防卫性,代码的好习惯,是你应该做的,一系列事情之一,也是我们日后要做的主要的事情。
In the case of last year we did hand students some code with which they could write programs that talked to Yahoo Finance, finance.yahoo.com, free website.
在去年的情况下,我们给学生提供一些,能用来写程序跟雅虎财经的免费网站交互的代码,即finance。yahoo。com,一个免费的网站。
In Scratch you may have realized that, "I could implement my program this way with these puzzle pieces, but it kind of feels -- like I could use these puzzle pieces instead" -- that's absolutely the case in programming.
在Scratch中你可能已经意识到,“我可以用这一段代码来,执行我的程序,不过这有点感觉到,我可以用这些代码段来代替“,那绝对是编程中的一个情况。
I guess it's not macho enough, John, to just, you know, you know, go off and do things by hand, you ought to just run them, but it's a valuable tool to get into, so let me do that here.
我认为这不太男人了,John,你知道的,他们仅仅是认为比起用手做来,更应该直接去运行程序,但是这确实是调试代码的非常有价值的工具,让我在这里给大家演示下吧。
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被赋了正确的值。
So that is a flag that tells the compiler that I want to use or link into my own program code that someone else wrote that lives somewhere else on the system whose moniker is CS50.
所以那是一个标记,用来告诉编译器我想使用,或连接我自己的程序代码到一个叫做CS50的系统中的程序,可能其他人在那个系统写了这些程序。
Meaning, we're going to try to help you with how to develop good programming style, but you need to write in a way in which it is going to be easy for you to spot the places that cause those semantic bugs to occur.
这意味着我们会帮助你,形成良好的编程风格,但是你需要去以一种,容易发现,程序中,容易发生语义bug的地方的方式去写代码。
The curly braces are then the C's way of kind of making a puzzle piece that looks like this so you can put stuff inside.
花括号是C语言的一个方式,用来做一个像这样的程序块,所以你可以把代码写在里面。
Last week we did the socks thing and that was in pseudocode.
上周我们写了一个socks程序,但那只是伪代码。
Well it's a stupid looking program, but because I had more new line characters, and because I was using percent C, for individual characters, one per line, now I'm seeing dot slash ARG V2, well, now if I go ahead and rerun this with foo, I get this effect as well.
这是一个看起来很笨的程序,但是因为我有更多的新行代码,因为我使用的是%c,为单个字符使用的,每一行一个,现在我看到的是,/argv2,好的,现在如果我继续用foo返回它,我产生了这样的效果。
应用推荐