爱思英语编者按:从电梯到汽车,从航班到智能手机,当代文明是由软件——允许计算机以及它们控制的设备进行计算和对它们的环境做出反应的数字指令——驱动的。软件是如何做到的?有人必须写出软件。但是,代码并不完全就是软件。前者是程序员绞尽脑汁创作出来的符号的结果,后者是计算机执行的指令的结果。那么,到底什么是代码呢?

What is code?
什么是代码?

code.jpg

FROM lifts to cars to airliners to smartphones, modern civilisation is powered by software, the digital instructions that allow computers, and the devices they control, to perform calculations and respond to their surroundings. How did that software get there? Someone had to write it. But code, the sequences of symbols painstakingly created by programmers, is not quite the same as software, the sequences of instructions that computers execute. So what exactly is it?

从电梯到汽车,从航班到智能手机,当代文明是由软件——允许计算机以及它们控制的设备进行计算和对它们的环境做出反应的数字指令——驱动的。软件是如何做到的?有人必须写出软件。但是,代码并不完全就是软件。前者是程序员绞尽脑汁创作出来的符号的结果,后者是计算机执行的指令的结果。那么,到底什么是代码呢?

Coding, or programming, is a way of writing instructions for computers that bridges the gap between how humans like to express themselves and how computers actually work. Programming languages, of which there are hundreds, cannot generally be executed by computers directly. Instead, programs written in a particular “high level” language such as C++, Python or Java are translated by a special piece of software (a compiler or an interpreter) into low-level instructions which a computer can actually run. In some cases programmers write software in low-level instructions directly, but this is fiddly. It is usually much easier to use a high-level programming language, because such languages make it easier to express complex, abstract ideas or commands efficiently and accurately; they also absolve programmers from having to worry about tedious details relating to the innards of the particular computer on which the program will eventually run. A program written in a high-level language can therefore be made to run on all sorts of different computers.

写代码也叫编程,它是一种为计算机编写指令的方式,它为人们喜欢表达自己的方式与计算机实际运行方式之间的鸿沟搭建了一座桥梁。编程语言有上百种之多,它们通常是不能够被计算机直接执行的。相反,用某种特殊的“高等级”语言,如C⁺⁺, Python或者是Java 写出来的程序,能够被软件的某一特殊部分(一种编译器或者一种解释器)转化成计算机能够实际运行的低等级指令。有时,程序员会直接使用低等级指令编写软件。但是,这太难了。通常来说,使用一种高等级编程语言要容易得多。因为,这样的语言能够让表达复杂、抽象的思想或高效准确地指令变得更容易;它们还能够让程序员从不得不为涉及程序最终要在其上运行的某些特殊类型计算机的内部结构的繁琐细节而操心中解脱出来。因而,一个用某种高等级语言写出来的程序能够在所有不同类型的计算机上运行。

Programming languages exist in many families and styles, rather like human languages. There are many dialects of C, for example; there are families of “functional” programming languages; and there are languages optimised for “parallel processing” (where several programs run alongside each other to accomplish a particular task, such as image processing or weather forecasting). As with human languages, these programming languages are all capable of expressing the same ideas, and in theory any program can be written in any language. But in practice some languages are better suited to some uses than others, just as French is traditionally used for diplomacy and English is the international language of business. And just as knowing a few different spoken languages makes it easier to learn another one, the same is true of programming languages. Once you understand common features (loops, recursion, conditionals, regular expressions and so on) you can usually pick up a new language quickly, particularly if it's reasonably close to another language you already know.

如同人类语言一样,编程语言种类繁多。例如,C语言就有许多分支;“函数”编程语言也有多种;还有专为“并行进程”(数个程序一起运行以完成某个特殊任务,如图像处理和天气预报)而优化的各种语言。如同人类语言,这些编程语言都能表达同样的思想;而且,从理论上来说,凡是程序就能够用一种语言写出来。但是,在实践中,有些语言比其他语言更适合于某些用途,这就像法语传统上用于外交,而英语则是国际商业用语一样。而且,就如同懂得几门不同的语言能够让学习其他语言变得更容易一样,编程语言也是如此。一旦了解了它们的共同特点(循环、递归、条件式、正则表达式等等),通常就能够迅速地掌握一门新语言,尤其是在这门新语言与你早已了解的另外一门语言非常接近的情况下。

Writing a program and then running it is magic, in a way. The numbers, letters and symbols of code are transmuted into instructions executed by microscopic circuits to achieve the desired results. Pixels appear on the screen; lifts move; airline tickets are ordered; lists are sorted; e-mails are delivered. But just because the results seem magical does not mean that coding is mysterious and inaccessible. Indeed, one of the joys of coding is that computers are the opposite of mysterious: they operate in an unforgivingly predictable, consistent and deterministic manner. Most people do not need to be able to write code to do their jobs, any more than they need to be able to speak foreign languages or do algebra. But it is useful to have some basic experience in coding, and not just to demystify how computers work. As Marc Andreessen, the co-creator of the Netscape web browser, likes to say, in future there will be two kinds of jobs: those that involve telling computers what to do, and those that involve being told what to do by computers. Worried that your job is in danger of being automated away by software? Learning to code could be a useful insurance policy. Even if you're not, it can also be fun.

从某种程度上来讲,编写一个程序,然后再去运行这个程序是有魔力的。代码中的数字、字母和符号被转化为可以由微电路去执行以便得到预想结果的指令。图像现身屏幕;电梯上升;机票被预定;表单得到分类,电子邮件得以发送。但是,仅仅因为这些结果似乎是用魔力的,并不意味着编程就是神秘的、难以企及的。事实上,编程的一大乐趣就是,计算机是神秘的对立面:它们是以一种可预测性极高的、持续的和具有确定性的方式运行的。大多数人在工作中不需要具备编程的能力,这就如同他们不需要具备说外语或者是做代数题的能力一样。但是,具备一些编程的基本经验而不仅仅是了解计算机的运行方式是有用的。正如网景浏览器的共同创作者马克·安德森喜欢说的那样,未来有两类工作:一类与命令计算机去干什么有关,一涉及被计算机指令去干什么。还在为工作由于软件的存在而正处于被自动化所淘汰的危险之中而担忧吗?学习编程也许是一种有用保险策略。就算学不会,也可能是有趣的。