Specialization is for insects. 专业化是为昆虫准备的。(而人应该什么都会做)
——R. A. Heinlein 罗伯特 · 海因莱因 1
In this chapter, we present some of the things that we think make programming important, interesting, and fun.
这一章,我们要讲一些对于编程很重要,有趣的东西。
We also present a few fundamental ideas and ideals. We hope to debunk a couple of popular myths about programming and programmers.
我们还会将几种基础的观点和想法。我们希望破除一些比较流行的关于程序和程序员的误解。
This is a chapter to skim for now and to return to later when you are struggling with some programming problem and wondering if it’s all worth it.
这章适合你在后面的编程学习卡住时回看,有助于你思考学习编程是否值得。
Like most learning, learning how to program is a chicken and egg problem: We want to get started, but we also want to know why what we are about to learn matters.
就像学习其他学科一样,学习如何编程也是一个鸡生蛋蛋生鸡的问题:我们想要马上动手,而我们又想知道学编程是为了什么?
We want to learn a practical skill, but also make sure it is not just a passing fad. We want to know that we are not going to waste our time, but don’t want to be bored by still more hype and moralizing.
我们想要学习这门实践技能,但有不确定它是否只是昙花一现。我们感觉学这个不会浪费时间,但又不想陷入盲目的狂热和说教之中。
For now, just read as much of this chapter as seems interesting and come back later when you feel the need to refresh your memory of why the technical details matter outside the classroom.
至于现在,我们不想这些有的没的,一章章地认真看书吧。
This chapter is a personal statement of what we find interesting and important about programming. It explains what motivates us to keep going in this field after decades.
这一章的内容主要是我自己挑选出一些编程中有趣且重要的事物。这些事物解释了是什么驱动着人们在编程这个领域耕耘了几十年。
This is a chapter to read to get an idea of possible ultimate goals and an idea of what kind of person a programmer might be.
这章还讨论了一个人成为程序员的终极目标和他应具有的素质。
A beginner’s technical book inevitably contains much pretty basic stuff. In this chapter, we lift our eyes from the technical details and consider the big picture: Why is programming a worthwhile activity? What is the role of programming in our civilization? Where can a programmer make contributions to be proud of? Where does programming fit into the greater world of software development, deployment, and maintenance? When people talk about “computer science,” “software engineering,” “information technology,” etc., where does programming fit into the picture? What does a programmer do? What skills does a good programmer have?
一本技术入门书必然会包含许多基础知识。在这一章,我们不关注技术细节,而是着眼于专业全景:为什么编程是件有价值的事情?在人类文明中编程具有什么地位?程序员从何处获取到奉献的自豪感?为何编程正好顺应了软件开发,部署和维护(的潮流)?当人们谈论「计算机科学」、「软件工程」、「IT」时,编程在这些领域中又扮演着什么角色?程序员到底做什么工作?一个好的程序员应该具备什么样的素质?
To a student, the most urgent reason for understanding an idea, a technique, or a chapter may be to pass a test with a good grade — but there has to be more to learning than that!
对于学生来说,学习编程的首要理由是要以一个好的成绩通过考试——但真正的目标远不止于此。
To someone working in the software industry, the most urgent reason for understanding an idea, a technique, or a chapter may be to find something that can help with the current project and that will not annoy the boss who controls the next paycheck, promotions, and firings — but there has to be more to learning than that!
而对于那些已经在软件开发企业工作的人来说,当务之急是马上弄明白一个idea,技术或者从这章找出对正在着手的项目有用的东西,避免把老板整生气,从而影响下个月的薪水,升职,甚至被炒鱿鱼。——但还是那句话:真正的目标远不止于此!
We work best when we feel that our work in some small way makes the world a better place for people to live in. For tasks that we perform over a period of years (the “things” that professions and careers are made of ), ideals and more abstract ideas are crucial.
我们努力工作,因为我们知道自己正在为世界和人类做贡献,哪怕微不足道。而对于动辄需要数年,十数年,甚至数十年的事业,理想和更抽象的观念就变得十分重要。
Our civilization runs on software. Improving software and finding new uses for software are two of the ways an individual can help improve the lives of many. Programming plays an essential role in that.
我们的文明运行在软件之上。改进软件性能,寻找新的应用领域,这两种利用软件的方式可以改善许多人的生活。而编程之于软件,就是重中之重。
Good software is invisible. You can’t see it, feel it, weigh it, or knock on it. Software is a collection of programs running on some computer.
好的软件是隐形的。你看不见,感觉不到,碰不到它。软件是运行在某台计算机上的程序集合。
Sometimes, we can see the computer. Often, we can see only something that contains the computer, such as a telephone, a camera, a bread maker, a car, or a wind turbine.
有时,我们可以看见计算机。但更多时候,我们看见的是装着计算机的东西——比如电话,照相机,烤面包机,汽车或者风力发电机。
We can see what that software does. We can be annoyed or hurt if it doesn’t do what it is supposed to
do. We can be annoyed or hurt if what it is supposed to do doesn’t suit our needs.
我们已经看到软件都能做什么。当软件无法正确运行,或者不能给出我们想要的结果的时候,我们会很生气。
How many computers are there in the world? We don’t know; billions at least. There may be more computers in the world than people. We need to count servers, desktop computers, laptops, tablets, smartphones, and computers embedded in “gadgets.”
世界上一共有多少台计算机?没人知道准确的数字;少说有十亿台。这世界上计算机的数量或许比人还要多。我们要把服务器,桌面电脑,笔记本,平板电脑,智能手机和嵌入在「小设备」中的计算机都算进去。
How many computers do you (more or less directly) use every day? There are more than 30 computers in my car, two in my cell phone, one in my MP3 player, and one in my camera.
每天你又会使用多少个计算机?以我的情况来看,我的汽车里差不多有30个,手机里有2个,MP3里有1个,照相机里有1个。
Then there is my laptop (on which the page you are reading is being written) and my desktop machine. The air-conditioning controller that keeps the summer heat and humidity at bay is a simple computer.
然后还有我的笔记本电脑(也就是我正用来写这本书的)和我的台式机。还有空调里的控制器,也算一个简单的计算机。
There is one controlling the computer science department’s elevator. If you use a modern television, there will be at least one computer in there somewhere
我所在的计算机学院的电梯里面也有一个控制电梯的计算机。如果你家里的电视是智能电视,那么它的里面也至少有一个计算机。
A bit of web surfing gets you into direct contact with dozens — possibly hundreds — of servers through a telecommunications system consisting of many thousands of computers — telephone switches, routers, and so on.
而你简简单单,网上冲会儿浪就可能与几十台——甚至上百台服务器通讯交互,而这些服务器是处在由成千上万台计算机——交换机,路由器以及其他设备——组成的电信系统之中。
No, I do not drive around with 30 laptops on the backseat of my car! The point is that most computers do not look like the popular image of a computer (with a screen, a keyboard, a mouse, etc.); they are small “parts” embedded in the equipment we use.
声明一下,我并不是说我的车的后座上有30台笔记本电脑!我的意思是大部分电脑并不是以我们熟悉的形式(一个屏幕,一个键盘,一个鼠标),而是非常小,嵌入到我们使用的设备中。
So, that car has nothing that looks like a computer, not even a screen to display maps and driving directions (though such gadgets are popular in other cars).
所以,车里面没有长得像电脑的东西,甚至是显示地图和方位的显示屏。
However, its engine contains quite a few computers, doing things like fuel injection control and temperature monitoring.
然而,发动机中有很多电脑,他们控制着燃油喷嘴,监视着运行温度。
The power-assisted steering involves at least one computer, the radio and the security system contain some, and we suspect that even the open/close controls of the windows are computer controlled. Newer models even have computers that continuously monitor tire pressure
助力转向系统以至少有一个计算机,广播和安全系统也包括几台,开关窗也是由计算机控制的。新的车型还会有专门的计算机监控胎压。
How many computers do you depend on for what you do during a day? You eat; if you live in a modern city, getting the food to you is a major effort requiring minor miracles of planning, transport, and storage.
一天之中计算机又帮助你做了多少决策呢?以「吃」为例;如果你住在城市里,得到事物这个需要付出巨大努力的事情的背后是有计划、运输、仓储等一系列小「奇迹」作为依靠的。
The management of the distribution networks is of course computerized, as are the communication
systems that stitch them all together. Modern farming is highly computerized; next to the cow barn you find computers used to monitor the herd (ages, health, milk production, etc.), farm equipment is increasingly computerized, and the number of forms required by the various branches of government can make any honest farmer cry.
物流网络的管理很显然是需要计算机做计算支持的,网络又把不同的物流网络连在一起。现代农业高度依赖计算机;牛棚的旁边你可以找到显示屏,上面有牛的年龄,健康状况和产奶量等指标的数据,农场其他设备也需要计算机,政府各个部门要求填的表格让农民苦不堪言。
If something goes wrong, you can read all about it in your newspaper; of course, the articles in that paper were written on computers, set on the page by computers, and (if you still read the “dead tree edition”) printed by computerized equipment — often after having been electronically transmitted
to the printing plant. Books are produced in the same way.
你可以从报纸上知道发生了什么事情;当然,报纸上的新闻是用计算机上的软件编辑、分页、排版、打印的。书籍和报纸一样。
If you have to commute, the traffic flows are monitored by computers in a (usually vain) attempt to avoid traffic jams. You prefer to take the train? That train will also be computerized; some even operate without a driver, and the train’s subsystems, such as announcements, braking, and ticketing, involve lots of computers.
通勤途中,交通状况的数据也会显示在交通指挥中心的大屏幕上来帮助交警进行调度,防止交通堵塞(通常没有吊用)。你喜欢坐火车么?火车也受计算机控制;一些火车甚至没有司机,火车上的各种系统,比如广播,刹车,检票系统,都依靠计算机运行着。
Today’s entertainment industry (music, movies, television, stage shows) is among the largest users of computers. Even non-cartoon movies use (computer) animation heavily; music and photography are also digital (i.e., using computers) for both recording and delivery.
今天的娱乐产业(音乐,电影,电视,舞台秀)都离不开计算机。即便不是动画的真人电影,也会使用计算机动画技术;音乐和摄影都需要使用计算机进行录制和分发。
Should you become ill, the tests your doctor orders will involve computers, the medical records are often computerized, and most of the medical equipment you’ll encounter if you are sent to a hospital to be cured contains computers
人一定会生病,医生用计算机开处方,你的用药记录、就诊记录也存在计算机里。
Unless you happen to be staying in a cottage in the woods without access to any electrically powered gadgets (including light bulbs), you use energy.
即使你碰巧住在没有电力供应的丛林小木屋里,你也逃不过计算机,因为你需要使用能量。
Oil is found, extracted, processed, and distributed through a system using computers every step along the way, from the drill bit deep in the ground to your local gas (petrol) pump.
而能量的来源——石油的勘探,加工,运输都基于一个有计算机的系统里,从石油从钻井里被打出来,到石油液化气加到你所在城市的加油站的泵里,这中间的每一步,都需要计算机。
If you pay for that gas with a credit card, you again exercise a whole host of computers. It is the same story for coal, gas, solar, and wind power
你用信用卡支付气的费用时,你就又和众多计算机交互了。对于煤,天然气,太阳能,风能,都是如此。
The examples so far are all “operational”; they are directly involved in what you are doing.
这些例子都是生活中「能做到」的,和你的日常生活里直接相关。
Once removed from that is the important and interesting area of design. The clothes you wear, the telephone you talk into, and the coffee machine that dispenses your favorite brew were designed and manufactured using computers.
还有设计领域。你穿的衣服,你用的电话,你使的咖啡机,从它们的设计到生产都用到了计算机,他们本身也都包含计算机。
The superior quality of modern photographic lenses and the exquisite shapes in the design of modern everyday gadgets and utensils owe almost everything to computer-based design and production methods.
如今高质量的摄影镜头和其他形状奇特,具有现代感的事物,器具,无不是借助计算机设计和生产出来的。
The craftsmen/designers/artists/engineers who design our environment have been freed from many physical constraints previously considered fundamental. If you get ill, the medicines given to cure you will have been designed using computers.
工程师/设计师/艺术家/建筑师借助计算机的力量,得以突破物理世界的限制,尽情发挥他们的才能。如果你病了,你用的药物的合成结构甚至都是计算机设计出来的。
Finally, research — science itself — relies heavily on computers. The telescopes that probe the secrets of distant stars could not be designed, built, or operated without computers, and the masses of data they produce couldn’t be analyzed and understood without computers
最后,还有科研,也是极度依赖计算机的领域。没有计算机,天文望远镜没法设计制造出来,就更别谈使用和处理望远镜获取的海量数据。
An individual biology field researcher may not be heavily computerized (unless, of course, a camera, a digital tape recorder, a telephone, etc. are used), but back in the lab, the data has to be stored, analyzed, checked against computer models, and communicated to fellow scientists.
生物学家也离不开计算机,实验室数据需要存储,分析,模型需要在计算机上检验正确与否,科学家也需要通过计算机交流科研成果。
Modern chemistry and biology — including medical research — use computers to an extent undreamed of a few years ago and still unimagined by most people. The human genome was sequenced by computers. Or — let’s be precise — the human genome was sequenced by humans using computers.
现代化学家生物学家,包括药学家,使用计算机能实现以前想都不敢想的事情。人类基因组计划就是靠计算机得出基因序列的。或者我们准确地说,人类基因是靠着人使用计算机测序的。
In all of these examples, we see computers as something that enables us to do something we would have had a harder time doing without computers.
这所有的例子,无不在证明我们已经离不开计算机,很多事情没了计算机很难做成。
Every one of those computers runs software. Without software, they would just be expensive lumps of silicon, metal, and plastic: doorstops, boat anchors, and space heaters.
每台计算机上都运行着软件。没有软件的计算机,就是一坨昂贵的,由硅片,金属和塑料组成的,浪费巨大空间的「门挡」,沉的要死的「船锚」,或者能给整个房间取暖的「小太阳」,毫无用处。
Every line of that software was written by some individual. Every one of those lines that was actually executed was minimally reasonable, if not correct. It’s amazing that it all works!
软件的没一行代码都是由某人编写的。每一行,哪怕有错误,只要能执行,就有它的道理。
We are talking about billions of lines of code (program text) in hundreds of programming languages. Getting all that to work took a staggering amount of effort and involved an unimaginable number of
skills.
我们所讨论的是由上百种语言编写的数十亿行代码。可以想象,产出这么多代码需要付出多少人的心血,多长时间的努力。
We want further improvements to essentially every service and gadget we depend on. Just think of any one service and gadget you rely on; what would you like to see improved?
我们总是希望日常的事物不断地改进,想想你生活中接触的事物,你希望什么东西可以改进一下呢?
If nothing else, we want our services and gadgets smaller (or bigger), faster, more reliable, with more features, easier to use, with higher capacity, better looking, and cheaper. The likelihood is that the improvement you thought of requires some programming.
如果没有,那我们的目标就转为让这些事物能变得更小(或更大),更快,更可靠耐用,拥有更多功能,更易用,有更大的容量,更好看的外观,更便宜。
而若你想做到这些,你最好是学会编程!!!
Computers are built by people for the use of people. A computer is a very generic tool; it can be used for an unimaginable range of tasks. It takes a program to make it useful to someone.
计算机是人发明出来供人使用的。它是一种能完成各种你意想不到的任务的通用工具。它接受程序作为输入来为人工作。
In other words, a computer is just a piece of hardware until someone — some programmer — writes code for it to do something useful. We often forget about the software. Even more often, we forget about the programmer.
换句话说,一台计算机在运行程序员编写的代码完成特定任务之前,它只是一坨硬件。我们经常忘记软件,或者说,经常忘了程序员这个角色(的重要性)。
Hollywood and similar “popular culture” sources of disinformation have assigned largely negative images to programmers. For example, we have all seen the solitary, fat, ugly nerd with no social skills who is obsessed with video games and breaking into other people’s computers.
好莱坞或者与之类似的「流行文化」塑造了不少负面的程序员形象。比如,我们经常看到一个又胖又丑的孤僻宅男,毫无社交能力,一边挂着游戏,一边黑进别人电脑这种场景。
He (almost always a male) is as likely to want to destroy the world as he is to want to save it. Obviously, milder versions of such caricatures exist in real life, but in our experience they are no more frequent among software developers than they are among lawyers, police officers, car sales-
men, journalists, artists, or politicians.
一般这种角色都是男性,要么他要毁灭世界,要么拯救世界。当然,现实中确实也存在这种漫画情节,但是凭我们的经验来说,软件开发者其实和律师、警察、4S店销售,记者,画家,政客一样,十分常见。
Think about the applications of computers you know from your own life. Were they done by a loner in a dark room? Of course not; the creation of a successful piece of software, computerized gadget, or system involves dozens, hundreds, or thousands of people performing a bewildering set of roles: for example, programmers, (program) designers, testers, animators, focus group 2 managers, experimental psychologists, user interface designers, analysts, system administrators, customer relations people, sound engineers, project managers, quality engineers, statisticians, hardware interface engineers, requirements engineers, safety officers, mathematicians, sales support personnel, troubleshooters, network designers, methodologists, software tools managers, software librarians, etc.
想想你生活中需要用到计算机的场景。真的是一个人在一个昏暗的房间里这种画面么?肯定不是吧?
成千上万种软件,及其每个部分,每个计算元件都需要不同角色,不同职责的人来开发,比如:程序员,(程序)设计人员,程序测试人员,动画制作人员,焦点小组成员,实验心理学家,用户界面设计师,分析师,系统管理员,客户信息维护人员,项目经理,质量工程师,统计学家,硬件接口工程师,需求工程师,音频工程师,安全负责人,数学家,销售支持,故障维修人员,网络工程师,方法论者,软件工具管理者,电子图书管理员。
The range of roles is huge and made even more bewildering by the titles varying from organization to organization: one organization’s “engineer” may be another organization’s “programmer” and yet another organization’s “developer,” “member of technical staff,” or “architect. There are even organizations that let their employees pick their own titles. Not all of these roles directly involve
programming.
角色的复杂让程序员在各行各业的称呼都多种多样:有时叫「工程师」,有时叫「程序员」,有时又叫「开发者」,有时又叫「技术人员」,有时又叫「架构师」。甚至还有公司或组织让程序员挑选自己的职位称呼。
Not all of these roles directly involve programming. However, we have personally seen examples of people performing each of the roles mentioned while reading or writing code as an essential part of their job.
并不是所有这些职位都直接参与编程。但有些岗位,读写代码却是本职工作。
Additionally, a programmer (performing any of these roles, and more) may over a short period of time interact with a wide range of people from application areas, such as biologists, engine designers, lawyers, car salesmen, medical researchers, historians, geologists, astronauts, airplane engineers, lumberyard managers, rocket scientists, bowling alley builders, journalists, and animators (yes, this is a list drawn from personal experience).
除此之外,更多的程序员是和其他应用领域人员,比如生物学家,工程师,律师,汽车销售人员,药物研究人员,历史学家,地理学家,宇航员,飞机设计师,伐木厂管理者,火箭科学家,保龄球馆建造者,记者和动画师进行短期合作。
Someone may also be a programmer at times and fill non-programming roles at other stages of a professional career
一些人也会在他们非编程的职业中的某个阶段用编程解决问题。
The myth of a programmer being isolated is just that: a myth. People who like to work on their own choose areas of work where that is most feasible and usually complain bitterly about the number of “interruptions” and meetings.
造成程序员被孤立的形象的迷惑本身就很令人迷惑。这种形象往往是这样的:喜欢自己一人安静工作,讨厌「被人打断」或者永无休止,大大小小的会议。
People who prefer to interact with other people have an easier time because modern software development is a team activity. The implication is that social and communication skills are essential and valued far more than the stereotypes indicate.
然而喜欢和人打交道的人才适用于今天讲究团队协作的软件开发。这就意味着社交和交谈技能远比刻板印象里展现的程序员特质更重要。
On a short list of highly desirable skills for a programmer (however you realistically define programmer), you find the ability to communicate well — with people from a wide variety of backgrounds — informally, in meetings, in writing, and in formal presentations.
在如今程序员的必备技能中,你会发现交流技能——即和不同领域的人,在会议,书面和其他正式场合中的交谈能力变得十分重要。
We are convinced that until you have completed a team project or two, you have no idea of what programming is and whether you really like it. Among the things we like about programming are all the nice and interesting people we meet and the variety of places we get to visit as part of our professional lives.
如果你没有参与过团队项目的开发,你是不会体会到编程的真谛,也无法确认是否真的喜欢编程。编程的一个开心之处就在于能和很多优秀的人一起共事,又因为业务需求可以去很多地方,见识到很多事情。
One implication of all this is that people with a wide variety of skills, interests, and work habits are essential for producing good software. Our quality of life depends on those people — sometimes even our life itself. No one person could fill all the roles we mention here; no sensible person would want every role
这就意味着来自各行各业,身怀不同技艺,各自利益和工作习惯的人都加入到生产软件的过程中来。
我们生活的质量掌握在这些人手中。没有人能够具备所有的技能,但凡有理智的人也不会逼自己成为这样的人。
The point is that you have a wider choice than you could possibly imagine; not that you have to make any particular choice. As an individual you will “drift” toward areas of work that match your skills, talents, and interests.
这也意味着你有很多选择;不用刻意去挑。你可以在任意领域施展你的技能,天赋和兴趣。
We talk about “programmers” and “programming,” but obviously programming is only part of the overall picture. The people who design a ship or a cell phone don’t think of themselves as programmers. Programming is an important part of software development, but not all there is to software development.
我们谈论「程序员」和「编程」,但很显然,编程只是整个职业全景的一部分。设计手机和船舶的人不会把自己当作程序员。编程确实是软件工程中的一部分,但它不是软件工程的全部。
Similarly, for most products, software development is an important part of product development, but not all there is to product development.
同样,对于大多数产业,软件开发是产品生产的一个重要环节,但它也不是全部。
We do not assume that you — our reader — want to become a professional programmer and spend the rest of your working life writing code. Even the best programmers — especially the best programmers — spend most of their time not writing code.
我们不假设你——也就是我们的读者——想要成为专业程序员,这辈子都写代码。甚至最优秀的程序员——或者说尤其是最优秀的程序员——大部分时间都没在写代码。
Understanding problems takes serious time and often requires significant intellectual effort. That intellectual challenge is what many programmers refer to when they say that programming is interesting.
理解问题需要大量的时间和努力。而这个钻研奋斗的过程正是程序员们口中说的「有趣」的部分。
Many of the best programmers also have degrees in subjects not usually considered part of computer science. For example, if you work on software for genomic research, you will be much more effective if you understand some molecular biology
许多优秀的程序员并非科班出身。比如,如果你是搞基因研究的,你懂分子生物学的话就对编程很有帮助。
If you work on programs for analyzing medieval literature, you could be much better off reading a bit of that literature and maybe even knowing one or more of the relevant languages.
如果你想写一个分析中世纪文学的程序,你最好平时就有阅读文学的习惯,并且掌握几种语言。
In particular, a person with an “all I care about is computers and programming” attitude will be incapable of interacting with his or her non-programmer colleagues. Such a person will not only miss out on the best parts of human interactions (i.e., life) but also be a bad software developer.
那些口口声声说「我只关注计算机和编程本身」的人是没法和他那些没有编程背景的同事合作的。这种人在岗位上(甚至人生里)也没法与人打好交道,只能当一个一般的程序员。
So, what do we assume? Programming is an intellectually challenging set of skills that are part of many important and interesting technical disciplines. In addition, programming is an essential part of our world, so not knowing the basics of programming is like not knowing the basics of physics, history, biology, or literature.
那么我们的读者受众到底是谁呢?编程是一项需要多种技能的智力活动,它是很多学科的组成部分。夸张点儿说,编程是我们世界的本质,所以不懂编程,就相当于不懂物理,历史,文学和生物。
Someone totally ignorant of programming is reduced to believing in magic and is dangerous in many technical roles. If you read Dilbert, think of the pointy-haired boss as the kind of manager you don’t want to meet or (far worse) become. In addition, programming can be fun.
而那些身在技术岗位却认为编程是一种魔法,对其一无所知的人更加危险。如果你读过Dilbert的《钉子头老板》漫画,你就知道有一个对技术一无所知的上司会多头疼。再次强调,编程很有趣。
But what do we assume you might use programming for? Maybe you will use programming as a key tool in your further studies and work without becoming a professional programmer
那学习编程到底是为了什么呢?你可能只是在未来的科研中会用到,而不是真要把程序员作为职业。
Maybe you will interact with other people professionally and personally in ways where a basic knowledge of programming will be an advantage, maybe as a designer, writer, manager, or scientist
又可能你想和那些运用编程就如虎添翼的领域中的人士合作,比如设计师,作家,经理和科学家。
Maybe you will do programming at a professional level as part of your studies or work. Even if you do become a professional programmer it is unlikely that you will do nothing but programming.
又或者你是以一个专业的姿态在你的科研中运用编程。即使你真的成了一名程序员,也不可能一直编程。
You might become an engineer focusing on computers or a computer scientist, but even then you will not “program all the time.” Programming is a way of presenting ideas in code — a way of aiding problem solving. It is nothing — absolutely a waste of time — unless you have ideas that are worth presenting and problems worth solving.
你可能会成为专注于计算机工程师或者计算机科学家,但无论怎样你都不可能一直编程。编程是用代码表达想法——一种解决问题的方式——这并不是什么难事儿——只是有点儿费时间而已——你的重点是找到表示和解决问题的方法。
This is a book about programming and we have promised to help you learn how to program, so why do we emphasize non-programming subjects and the limited role of programming? A good programmer understands the role of code and programming technique in a project.
这是一本教你编程的书并且我们保证一定教会你编程,那我们为什么还要强调非编程因素和编程的有限性呢?一个好的程序员应该知晓代码的角色和项目中编程的地位。
A good programmer is (at most times) a good team player and tries hard to understand how the code and its production best support the overall project. For example, imagine that I worked on a new MP3
player (maybe to be part of a smartphone or a tablet) and all that I cared about was the beauty of my code and the number of neat features I could provide.
一个优秀的程序员(在大部分时候)是一个团队领导,应该用心理解代码对于整个项目产品的重要性。
比如说,假设我正在开发一款MP3播放器(或者这个功能是智能手机或者平板的一部分),你应该考虑的只有代码的美观性和我能给用户提供多少功能。
I would stay in my lab, rather than go out to meet potential users, who undoubtedly would have
bad tastes in music anyway and would not appreciate the latest advances in GUI (graphical user interface) programming.
但比起到外面见潜在用户,我更愿意待在实验室里琢磨,我觉得用户音乐品味很差,并且他们也不会懂我在GUI界面上的小改动。
The likely result would be disaster for the project. A bigger computer would mean a costlier MP3 player and most likely a shorter battery life. Encoding is an essential part of handling music digitally, so failing to pay attention to advances in encoding techniques could lead to increased memory requirements for each song (encodings differ by as much as 100% for the same-quality output).
这样做的可能结果就是毁了整个项目。MP3里装了一个巨大的计算机意味着成本陡增,电池寿命剧减。MP3开发的核心在于音频的数字编码技术,如果你把GUI当作重点,就会使内存使用增加。(不同的编码会使输出相差100%)。
A disregard for users’ preferences — however odd and archaic they may seem to you — typically leads to the users choosing some other product. An essential part of writing a good program is to understand the needs of the users and the constraints that those needs place on the implementation (i.e., the code).
对用户喜好的无视——哪怕用户的需求有多么古怪或者过时——都会导致用户流失。好的编程的核心思想就是理解用户的需求和实现这些需求的限制条件。
To complete this caricature of a bad programmer, we just have to add a tendency to deliver late because of an obsession with details and an excessive confidence in the correctness of lightly tested code. We encourage you to become a good programmer, with a broad view of what it takes to produce good software. That’s where both the value to society and the keys to personal satisfaction lie.
完善坏程序员的画像,我们在之后的正文里还会讲述他们身上的其他缺点,比如过分注重细节,对粗略测试的代码具有盲目的自信。(这一句翻得应该有问题,先这样)我们希望你成为一个好的程序员,具有广阔的视角,编写优质的软件。对社会有价值的同时也成就自己的人生。
Nobody knows everything there is to know about computers or software. This section just gives you a few examples. Maybe you’ll see something you like. At least you might be convinced that the scope of computer use — and through that, programming — is far larger than any individual can fully grasp.
没有人知道关于计算机和软件的一切知识。这一章只是给你列举出一些例子。你可以挑你喜欢的看。通过本章学习,你会了解到计算机的应用领域——编程的应用领域——远比你想象得要广泛得多。
Most people think of a computer as a small gray box attached to a screen and a keyboard. Such computers tend to be good at games, messaging and email, and playing music. Other computers, called laptops, are used on planes by bored businessmen to look at spreadsheets, play games, and watch videos.
大多数人觉得电脑就是带个屏幕和键盘的灰盒子。可以用来打游戏,发邮件, 听音乐。还有叫笔记本的 另一种电脑,通常是社畜用来编辑无聊的电子表格,也可以用来打游戏,看视频。
This caricature is just the tip of the iceberg. Most computers work out of our sight and are part of
the systems that keep our civilization going. Some fill rooms; others are smaller than a small coin. Many of the most interesting computers don’t directly interact with a human through a keyboard, mouse, or similar gadget.
这种印象只是冰山的一角。大部分计算机并不在我们的视线范围内并维持着我们人类社会的运行。有些电脑有房间那么大;有些比硬币还小。许多计算机并不通过键盘,鼠标与人直接交互。
The idea of a computer as a fairly large rectangular box with a screen and a keyboard is common and often hard to shake off. However, consider these two computers:
很难改变人们对计算机就是个大盒子的印象,我们不妨来考虑下面这两种计算机:
Both of these “gadgets” (which happen to be watches) are primarily computers. In fact, we conjecture that they are essentially the same model computer with different I/O (input/output) systems
这两种手表都算计算机。我们把它们视作同种计算机模型,但具有不同输入/输出系统。
The left one drives a small screen (similar to the screens on conventional computers, but smaller) and the second drives little electric motors controlling traditional clock hands and a disk of numbers for day-of-month readout.
左边的手表有一个类似计算机的小屏幕,右边的则是有几个小电动马达驱动着传统样式的表盘来显示年月日。
Their input systems are the four buttons (more easily seen on the right-hand watch) and a radio receiver, used for synchronization with very high-precision “atomic” clocks. Most of the programs controlling these two computers are shared between them.
他们的是靠四个按钮输入的,还有一个无线电接收器,用来和更加精确的原子钟进行同步。大部分程序可以在这两种计算机上运行。
These two photos show a large marine diesel engine and the kind of huge ship that it may power:
下面是大型船舶及其引擎照片:
Consider where computers and software play key roles here:
以此做例子来考虑计算机和软件的关系:
Consider the implication of a failure of one of the hundreds of computers explicitly mentioned or implied in this brief description. Chapter 25 (“Embedded Systems Programming”) examines this in slightly more detail. Writing code for a modern ship is a skilled and interesting activity.
假设这些控制船只的计算机中有一台出现了状况。我们会在第25章来详细讲这个示例,为现代船只编写代码是一件有意义又有趣的事情。
It is also useful. The cost of sea transport is really amazingly low. You appreciate that when you buy something that wasn’t manufactured locally. Sea transport has always been cheaper than land transport; these days one of the reasons is serious use of computers and information.
也很有用。远洋运输的成本非常低。这也归功于计算机和信息技术的普及。
These two photos show a telephone switch and a telephone (that also happens to be a camera, an MP3 player, an FM radio, a web browser, and much more):
下面两张图片展示了电话交换机和电话:
艹他妈,csdn这破比编辑器,不晓得这自动保存和同步是怎么做的?!我明明保存了,结果莫名其妙没有了,这一整节都没了,艹!这节和下一节的前面几段不再重新翻了,直接跳过!破jb玩意儿。
A wide variety of imaging techniques are used to let the surgeon see the inside of the patient, to see the point of surgery with significant enlargement or in better light than would otherwise be possible.
先进的图像技术可以让主刀医生看见病人身体内部,让医生可以更清楚地观察到病人的情况。
With the aid of a computer a surgeon can use tools that are too fine for a human hand to hold or in a place where a human hand could not reach without unnecessary cutting.
在计算机的帮助下,一些追求高精度操作,人难以触及的手术也无需让人亲手来做。
The use of minimally invasive surgery (laparoscopic surgery) is a simple example of this that
has minimized the pain and recovery time for millions of people. The computer can also help steady the surgeon’s “hand” to allow for more delicate work than would otherwise be possible.
计算机也让微创手术成为可能,可以很大程度减少对病人的创伤,减轻病人痛苦。对于复杂的手术,计算机可以充当医生的第三只手。
Finally, a “robotic” system can be operated remotely, thus making it possible for a doctor to help someone remotely (over the internet). The computers and programming involved are mind-boggling, complex, and interesting. The user interface, equipment control, and imaging challenges alone will
keep thousands of researchers, engineers, and programmers busy for decades
医用机器人还可以远程操控,使得医生可以通过远程协助另一名医生进行手术。
We heard of a discussion among a large group of medical doctors about which new tool had provided the most help to them in their work: The CAT scanner? The MRI scanner? The automated blood analysis machines? The high-resolution ultrasound machines? PDAs? After some discussion, a surprising “winner” of this “competition” emerged: instant access to patient records. Knowing the medical history of a patient (earlier illnesses, medicines tried earlier, allergies, hereditary problems, general health, current medication, etc.) simplifies the problem of diagnosis and minimizes the chance of mistakes.
我们听过很多医生讨论哪种新工具给他们的帮助最大:CAT扫描?MRI扫描?自动血分析仪?高分辨率超声波?PDA?结果发现都不是,而是「马上能从电脑里获取到病人信息」。了解病人的过去信息(既往病史,曾用药,过敏史,遗传病史,健康状况,目前用什么药)会使诊疗过程变得简单且不易出错。
These two photos show an ordinary PC (well, two) and part of a server farm:
下图是两种计算机,左图是PC,右图是服务器:
We have focused on “gadgets” for the usual reason: you cannot see, feel, or hear software. We cannot present you with a photograph of a neat program, so we show you a “gadget” that runs one. However, much software deals directly with “information.” So let’s consider “ordinary uses” of “ordinary computers” running “ordinary software.”
我们之前的例子都有一个共性:你使用某个设备,但你看不到,听不到,感受不到软件在其中运行。我们没法用图片向你展示一个程序,所以我们只能直接给你展示运行程序的机器。软件直接处理「信息」。这节我们就来说说「最常见的计算机」运行「最常见的软件」这种「最常见的用法」。
A “server farm” is a collection of computers providing web services. Organizations running state-of-the-art server farms (such as Google, Amazon, and Microsoft) are somewhat close-mouthed about the details of their servers, and the specifications of server farms change constantly (so most of the information you find on the web is outdated).
「服务器集群」是一堆提供网络服务的计算机组成的。那些拥有大型服务器集群的公司(谷歌,微软,亚马逊)是对他们服务器的细节闭口不谈的,服务器集群的规范也频繁更新(所以你能看到的规范其实早已过时)
However, the specifications are amazing and should convince anyone that there is more to programming than simply computing a few numbers on a laptop:
但这些规范依然提醒人们服务集群级别的编程并非简单地像用几台笔记本做计算那样:
We may be underestimating the amounts, and by the time you read this, we almost certainly are. In particular, efforts to minimize energy usage seem to be driving machine architectures toward more processors per server and more cores per processor.
这些数字应该很好理解。我们可以推断出现在通过增加处理器数或单位处理器核心数来节省电能。
A GB is a gigabyte, that is, about 1 0 9 10^{9} 109 characters. A TB, a terabyte, is about 1000GB, that is, about 1 0 12 10^{12} 1012 characters. A PB, a petabyte (that is, 1015 bytes), is becoming a more common measure. This is a pretty extreme example, but every major company runs programs on the web to interact with its users/customers.
1GB叫做吉比特,大概能存 1 0 9 10^{9} 109 个字符。TB,terabyte,1000GB, 1 0 12 10^{12} 1012 个字符。PB, 1 0 15 10^{15} 1015 个字符。每个大公司都在web上运行着与用户交互的程序。
Examples are Amazon (book and other sales), Amadeus (airline ticketing and automobile rental), and eBay (online auctions). Millions of companies, organizations, and individuals also have a presence on the web. Most don’t run their own software, but many do and much of that is not trivial.
比如亚马逊,Amadeus,易趣。大部分组织,公司或者个人在web上都有出现。大部分都不运行他们自己的软件,但形式也不简单。
The other, and more traditional, massive computing effort involves accounting, order processing, payroll, record keeping, billing, inventory management, personnel records, student records, patient records, etc.
还有一些传统行业,会计,订单处理,支付,各种记录,账单,投资管理,个人记录,学生记录,病人记录。
— the records that essentially every organization (commercial and noncommercial, governmental
and private) keeps. These records are the backbone of their respective organizations. As a computing effort, processing such records seems simple: mostly some information (records) is just stored and retrieved and very little is done to it.
这些记录都由各大组织(商业的,非营利的,政府,私人)保存。这些记录是这些组织的业务核心。
对于强大的算力,处理这些记录绰绰有余:大部分信息只需要存或者取,很简单。
Examples include
The sheer scale of the databases involved makes these systems highly complex. To that add the need to respond quickly (often in less than two seconds for individual queries) and to be correct (at least most of the time).
数据库的存在又让这些系统变得更加复杂。还要在短时间内正确地响应用户请求。
These days, it is not uncommon for people to talk about terabytes of data (a byte is the amount of memory needed to hold an ordinary character). That’s traditional “data processing” and it is merging with “the web” because most access to the databases is now through web interfaces.
现在没多少人谈论TB级别的数据,「数据处理」已经和「web」融为一体,因为对数据库的操作都通过web接口来进行。
This kind of computer use is often referred to as information processing. It focuses on data — often lots of data. This leads to challenges in the organization and transmission of data and lots of interesting work on how to present vast amounts
of data in a comprehensible form: “user interface” is a very important aspect of handling data.
这种电脑专注于信息处理。它专门处理数据——海量数据。这对一些机构组织是一种挑战,他们需要把大量数据处理成用户易懂的形式,还要做好数据传输,「用户界面」是处理数据的一个非常重要的方面。
For example, think of analyzing a work of older literature (say, Chaucer 3 ’s Canterbury Tales 4 or Cervante’s 5 Don Quixote 6 ) to figure out what the author actually wrote by comparing dozens of versions. We need to search through the texts with a variety of criteria supplied by the person doing the analysis and to display the results in a way that aids the discovery of salient points. Thinking of text analysis, publishing comes to mind: today, just about every article, book, brochure, newspaper, etc. is produced on a computer. Designing software to support that well is for most people still a problem that lacks a really good solution.
举个例子,假设我们要分析古典文学(比如乔叟的《坎特伯雷故事集》或者塞万提斯的《堂吉诃德》),从众多版本中分辨哪个才是真迹。我们需要遍历全文提取出一些表现出作者独有的特征。
今天文本分析无处不在,文章,书籍,小册子,报纸的文本都从计算机编辑而来。如何设计一个适用于大部分人的好的软件仍是一个需要进一步解决的问题。
It is sometimes claimed that a paleontologist can reconstruct a complete dinosaur and describe its lifestyle and natural environment from studying a single small bone. That may be an exaggeration, but there is something to the idea of looking at a simple artifact and thinking about what it implies.
有时我们会听到这样一种言论:古生物学家会通过研究一块很小的恐龙化石来复原一整条恐龙,并且描述出它的生活习性和它所处的自然环境。这可能有夸大的嫌疑,但有时我们确实可以做到见微知著。
Consider this photo showing the landscape of Mars taken by a camera on one of NASA’s Mars Rovers:
来看下面这张图片,这张照片是由NASA的一台火星车在火星表面拍摄的:
If you want to do “rocket science,” becoming a good programmer is one way. The various space programs employ lots of software designers, especially ones who can also understand some of the physics, math, electrical engineering, mechanical engineering, medical engineering, etc.
如果你想从事「火箭科学」行业,以程序员的身份踏入这个行业是一条门路。许多太空中用到的程序需要大量的程序开发人员来编写,尤其需要那些对熟数学,物理,电子工程,机械工程,医学工程领域知识熟悉的人。
that underlie the manned and unmanned space programs. Getting those two Rovers to drive around on Mars for years is one of the greatest technological triumphs of our civilization.
这些大程序都基于一些有人或无人条件下的太空程序。将两辆火星车送上火星并且令其运行数年也是人类文明最杰出的科技成果之一。
One (Spirit) sent data back for six years and the other (Opportunity) is still working at the time of writing and will have its tenth anniversary on Mars in January 2014. Their estimated
design life was three months
勇气号在六年之后仍能发回信息,机遇号在本书撰写之时仍在工作,2014年1月就是它登陆火星10周年。它原本的设计寿命只有3个月。
The photo was transmitted to earth through a communication channel with a 25-minute transmission delay each way; there is a lot of clever programming and advanced math to make sure that the picture is transmitted using the minimal number of bits without losing any of them. On earth, the photo is then rendered using algorithms to restore color and minimize distortion due to the optics and
electronic sensors.
图片通过一个延时足有25分钟的信道传输;图片传输背后有很多先进的算法和数学知识做支撑,从而使用最少的比特位数无损传回。在地球这一端,传回的图片数据要经过一系列算法的处理来恢复色彩,并纠正光学仪器和传感器的误差。
The control programs for the Mars Rovers are of course programs — the Rovers drive autonomously for 24 hours at a time and follow instructions sent from earth the day before. The transmission is managed by programs
火星车的控制系统很显然也是一种程序——火星车根据前一日传回的地球指令在一天的24小时里自动移动。指令的传输也靠程序。
The operating systems used for the various computers involved in the Rovers, the transmission, and the photo reconstruction are programs, as are the applications used to write this chapter.
火星车上不同的计算机还使用不同的电脑,信息传输,照片构建,都是操作系统上的应用。
The computers on which these programs run are designed and produced using CAD/CAM (computer-aided design and computer-aided manufacture) programs. The chips that go into those computers are produced on computerized assembly lines constructed using precision tools,
and those tools also use computers (and software) in their design and manufacture.
这些计算机都是依靠CAD(计算机辅助设计)和CAM(计算机辅助制造)程序来制作的。这些计算机运行汇编指令,相应的预测工具也需要计算机来设计和制造。
The quality control for those long construction processes involves serious computation. All that code was written by humans in a high-level programming language and translated into machine code by a compiler, which is itself such a program. Many of these programs interact with users using GUIs and exchange data using input/output streams.
针对软件构建过程的质量控制也需要严格的计算。这些代码都是由人编写并由编译器编译成机器代码的,编译器本身也是程序。其中大部分程序都需要GUI和输入输出流。
Finally, a lot of programming goes into image processing (including the processing of the photos from the Mars Rovers), animation, and photo editing (there are versions of the Rover photos floating around on the web featuring “Martians”).
还有很多程序负责图像处理,动画和图片编辑的程序。
What do all these “fancy and complicated” applications and software systems have to do with learning programming and using C++? The connection is simply that many programmers do get to work on projects like these. These are the kinds of things that good programming can help achieve.
我们说了这么多,各式各样的应用,这些都要用到C++么?实际上是因为程序员都要接触这样的项目。而通过C++学习良好的编程习惯和技术能帮助你更好地去参与这样的项目。
Also, every example used in this chapter involved C++ and at least some of the techniques we describe in this book. Yes, there are C++ programs in MP3 players, in ships, in wind turbines, on
Mars, and in the human genome project. For more applications using C++, see www.stroustrup.com/applications.html.
本书中的一些例子也用到了刚才提到的一些技术。比如MP3播放器,船舶,风机,火星车,甚至人类基因计划中的一些非常小的基本程序。要想了解C++的其他应用,可以看本书的配套网站。
What do we want from our programs? What do we want in general, as opposed to a particular feature of a particular program? We want correctness and as part of that, reliability If the program doesn’t do what it is supposed to do, and do so in a way so that we can rely on it, it is at best a serious nuisance, at worst a danger.
我们想要从我们的程序中得到什么?我们是要追求与特定程序的功能相反的普遍规律么? 我们想要的是正确性和可靠性。如果一个程序没法按照预期运行,我们没法正常使用这个程序的话,那么这个程序给我们带来的后果最好情况是一个小缺陷,最坏情况会造成危险。
We want it to be well designed so that it addresses a real need well; it doesn’t really matter that a program is correct if what it does is irrelevant to us or if it correctly does something in a way that annoys us. We also want it to be affordable; I might prefer a Rolls-Royce or an executive jet to my usual forms of transport, but unless I’m a zillionaire, cost will enter into my choices
我们应该好好设计程序好让它符合要求;而对我们没有用处的程序,它正确与否和我们无关,或者正确的程序却做了让我们闹心的事情也会很烦。我们还希望能支付得起程序的开发成本;是人都喜欢劳斯莱斯和喷气式客机,但除非我是个亿万富翁,否则我必须要考虑成本。
These are aspects of software (gadgets, systems) that can be appreciated from the outside, by non-programmers. They must be ideals for programmers and we must keep them in mind at all times, especially in the early phases of development, if we want to produce successful software.
这些都是作为用户,希望程序能够做到的点。对于想要编写成功运行的程序的程序员更应该重视这些,时刻牢记,尤其在程序开发的初级阶段。
In addition, we must concern ourselves with ideals related to the code itself: our code must be maintainable; that is, its structure must be such that someone who didn’t write it can understand it and make changes. A successful program “lives” for a long time (often for decades) and will
be changed again and again
而我们的理想是建立在代码之上的:我们的代码必须具有可维持的特点;换句话就是你写的代码,别人也应该毫不费力的看懂并可以进行修改。一个成功的程序应该能存活十数年,并且经得起多次修改。
For example, it will be moved to new hardware, it will have new features added, it will be modified to use new I/O facilities (screens, video, sound), to interact using new natural languages, etc. Only a failed program will never be modified.
比如,程序要移植到新的硬件平台,或者添加新的功能,或者与新的输入输出对接,又或者给其他国家使用不同语言的人用。只有一个失败的程序才不需要修改。
To be maintainable, a program must be simple relative to its requirements, and the code must directly represent the ideas expressed. Complexity — the enemy of simplicity and maintainability — can be intrinsic to a problem (in that case we just have to deal with it), but it can also arise from poor expression of ideas in code. We must try to avoid that through good coding style — style matters!
为了达到可维持性,程序首先要符合需求,代码要百分百还原最初的想法。复杂性——简约和可维持的敌人——可能潜藏在问题内部(我们还必须要解决它),也可能源于代码的不足之处。我们需要通过遵守良好的编程风格来避免复杂度暴涨。
This doesn’t sound too difficult, but it is. Why? Programming is fundamentally simple: just tell the machine what it is supposed to do. So why can programming be most challenging? Computers are fundamentally simple; they can just do a few operations, such as adding two numbers and choosing the next instruction to execute based on a comparison of two numbers.
这听起来不太难,但做起来很难。为什么?编程听起来也很简单:只需要告诉机器如何做就好。那为什么编程又充满挑战呢?因为计算机本身的结构也很简单,只能做非常基本的几个操作,比如把两个数相加,根据比较两数的结果来选择下一条指令。
The problem is that we don’t want computers to do simple things. We want “the machine” to do things that are difficult enough for us to want help with them, but computers are nitpicking, unforgiving, dumb beasts. Furthermore, the world is more complex than we’d like to believe, so we don’t really know the implications of what we request. We just want a program to “do something like this” and don’t want to be bothered with technical details.
但我们不满足于用计算机做简单的事。我们希望「机器」做点儿难事儿,我们还可以帮帮它,但是计算机有点儿个愣子,还死犟眼子。世界也在日益变得复杂,我们不知道客户会冒出什么样的需求。我们只想让程序干好它「应该干的事儿」,不想在意过多技术细节。
We also tend to assume “common sense.” Unfortunately, common sense isn’t all that common among humans and is totally absent in computers (though some really well-designed programs can imitate it in specific, well-understood cases).
我们也倾向于假设“常识”不幸的是,常识在人类中并不那么常见,在计算机中也完全不存在(尽管一些设计精良的程序可以在特定的、容易理解的情况下模仿它)。
This line of thinking leads to the idea that “programming is understanding”: when you can program a task, you understand it. Conversely, when you understand a task thoroughly, you can write a program to do it. In other words, we can see programming as part of an effort to thoroughly understand a topic. A program is a precise representation of our understanding of a topic.
这就引出了一个思想:「编程就是理解」:当你为了一个任务编程,你要理解他。反之亦然,你理解了一个任务,你自然可以编出代码。我们可以把编程看作是透彻理解一个主题的过程。而程序是这个理解过程的结果产物。
When you program, you spend significant time trying to understand the task you are trying to automate.
当你编程时,你肯定是在尝试理解一个任务并使其自动化。
We can describe the process of developing a program as having four stages:
我们可以把程序开发分成四阶段:
Programming plus testing is often called implementation. Obviously, this simple split of software development into four parts is a simplification. Thick books have been written on each of these four topics and more books still about how they relate to each other.
编程+测试=实现。很显然,这四步是简化过的软件开发步骤。其中的每一步都能写一本大厚书出来,然后还会有别的书来讲这四步如何联系起来。
One important thing to note is that these stages of development are not independent and do not occur strictly in sequence. We typically start with analysis, but feedback from testing can help improve the programming; problems with getting the program working may indicate a problem with the design; and working with the design may suggest aspects of the problem that hitherto had been overlooked in the analysis. Actually using the system typically exposes weaknesses of the analysis.
还有一个重要的事,这四步并不是各自独立的,并且有时也不会按照以上步骤。一般来说,分析起手,但测试获得反馈有助于编程环节;而编程中出现的问题也会引出设计阶段的缺陷;设计阶段也会引出分析阶段的问题。使用系统的过程也会显现出分析的问题。
The crucial concept here is feedback. We learn from experience and modify our behavior based on what we learn. That’s essential for effective software development. For any large project, we don’t know everything there is to know about the problem and its solution before we start.
这里重要的概念是反馈。我们从经验中学习,并且依据这些经验修正我们之前学到的方法。这是高效开发软件的重点。对于任何一个大型项目,我们都不可能事先考虑到所有问题。
We can try out ideas and get feedback by programming, but in the earlier stages of development it is easier (and faster) to get feedback by writing down design ideas, trying out those design ideas, and using scenarios on friends. The best design tool we know of is a blackboard (use a whiteboard instead if you prefer chemical smells over chalk dust).
我们可以穷尽我们的智力,并从编程中获得经验,在开发的初级阶段可以快速简要地列出设计想法,尝试这些想法,并使用朋友作为用户场景。最好的设计工具就是黑板或白板。
Never design alone if you can avoid it! Don’t start coding before you have tried out your ideas by explaining them to someone. Discuss designs and programming techniques with friends, colleagues, potential users, and so on before you head for the keyboard. It is amazing how much you can learn from simply trying to articulate an idea. After all, a program is nothing more than an expression (in code) of some ideas
千万不要一个人独自设计!也不要在没想好,又不能给别人讲明白的情况下开始编程。请和朋友,同事,潜在用户讨论你的设计和编程方法。从各方吸取idea将会给你编程带来惊人的改变。毕竟,编程只是表达想法的一种方式而已。(重要的是idea,和人交流能获得各种各样的idea,而编程是次要的。)
Similarly, when you get stuck implementing a program, look up from the keyboard. Think about the problem itself, rather than your incomplete solution. Talk with someone: explain what you want to do and why it doesn’t work. It’s amazing how often you find the solution just by carefully explaining the problem to someone. Don’t debug (find program errors) alone if you don’t have to!
如果你卡住了,那就离开键盘。专注问题,和别人交谈,告诉他你想做什么,哪里出了问题。当你向别人解释问题的过程中,也有可能灵感迸发。不要自己一个人debug或者找错误。
The focus of this book is implementation, and especially programming. We do not teach “problem solving” beyond giving you plenty of examples of problems and their solutions. Much of problem solving is recognizing a known problem and applying a known solution technique. Only when most subproblems are handled this way will you find the time to indulge in exciting and creative “out-of-the-box thinking.” So, we focus on showing how to express ideas clearly
in code.
这本书的重点在于实现(也就是之前说的编程+测试),但尤其注重编程。我们不教「求解问题」只给你提供大量问题和解法的例子。所以大部分问题都是众所周知的,它们的解法也是大家熟知的,你只要按部就班实现就好了。只有当大多数子问题都以这种方式处理时,你才会有时间沉浸在令人兴奋和富有创造性的“开箱即用的思维”中因此,我们着重于展示如何清晰地用代码表达想法。
Direct expression of ideas in code is a fundamental ideal of programming. That’s really pretty obvious, but so far we are a bit short of good examples. We’ll come back to this, repeatedly. When we want an integer in our code, we store it in an int
, which provides the basic integer operations. When we want a string of characters, we store it in a string
, which provides the most basic text manipulation
operations.
直接表达idea是编程的基本目标。但我们现在举不出太多好例子。比如我们需要一个整数,我们就把这个数存在int
型变量中,这个类型给我们提供基本的整数操作。当我们需要一串字符,我们就把它存为string
变量,这个类型提供基本的处理文本的操作。
At the most fundamental level, the ideal is that when we have an idea, a concept, an entity, something we think of as a “thing,” something we can draw on our whiteboard, something we can refer to in our discussions, something our (non–computer science) textbook talks about, then we want that something to exist in our program as a named entity (a type) providing the operations we think
appropriate for it.
在基本层面,理想状态是当我们有一个想法,一个实体,或者说一个「东西」,我们可以画在白板上,有时来源于讨论,有时来源于教材,我们希望这个东西在我们的程序中是一个有名字的东西(类型),然后我们为它添加一些操作。
If we want to do math, we want a complex type for complex numbers and a Matrix type for linear algebra. If we want to do graphics, we want a Shape type, a Circle type, a Color type, and a Dialog_box. When we want to deal with streams of data, say from a temperature sensor, we want an istream type (i for input). Obviously, every such type should provide the appropriate operations and only the appropriate operations. These are just a few examples from this book. Beyond that, we offer tools and techniques for you to build your own types to directly represent whatever concepts you want in your program.
如果我们编写数学程序,我们可能需要Complex类型来存一个复数,用Matrix类型来算线性代数。如果我们写图形程序,我们呢需要一个Shape类型,一个Circle类型,一个Color类型,一个Dialog_box。如果我们想要处理流数据,比如从温度传感器来的数据,我们需要istream类型。显然这些类型可以提供基于这些类型的特有操作。本书有些这样的例子,此外我们还提供工具和方法来让你创建你自己的类型,来满足你程序的需求。
Programming is part practical, part theoretical. If you are just practical, you will produce non-scalable, unmaintainable hacks. If you are just theoretical, you will produce unusable (or unaffordable) toys.
For a different kind of view of the ideals of programming and a few people who have contributed in major ways to software through work with programming languages, see Chapter 22, “Ideals and History.”
编程既需要实践,又需要理论。如果上来就干,你会写出毫无扩展性,难以维护的程序。如果你纸上谈兵,你会写出毫无用处的玩具。至于编程的其他目标和那些在编程历史上作出贡献的伟人的介绍,你可以翻到12章进一步了解。
(本章完)
Robert A. Heinlein - Wikipedia ↩︎
A focus group is a specially selected group of people who are intended to represent the general public. Focus groups have discussions in which their opinions are recorded as a form of market research. 选自各阶层,讨论某专项问题;所得信息常为市场调查人员或某政党所用。 ↩︎
Geoffrey Chaucer - Wikipedia ↩︎
The Canterbury Tales - Wikipedia ↩︎
Miguel de Cervantes - Wikipedia ↩︎
Don Quixote - Wikipedia ↩︎