If you’re a student – or, indeed, a teacher! – you will often have no choice about what programming language to use in a course you are committed to. By now, I hope you have some techniques for getting to grips with the prescribed language, whatever it is.

如果你是一名学生——或者,实际上是一名教师!——你通常无法选择在你所学习的课程中使用什么编程语言。现在,我希望你有一些技巧来掌握规定的语言,不管它是什么。

What if you do have a choice of what language to use for a project, though, or a choice of what programming language to learn next? Many factors may influence your decision, from the nature of a program you want to write, to your own state of mind.

但是,如果您确实可以选择在项目中使用什么语言,或者选择下一步要学习什么编程语言呢?很多因素可能会影响你的决定,从你想写的程序的性质到你自己的心态。

1 Questions to Consider 需要考虑的问题

1.1 Are You Choosing Just for Yourself?

Or are you writing a program with someone else, or that someone else will have to maintain in future? You may have to take other people’s needs and preferences into account.

你只为自己选择吗?或者你是在和其他人一起写一个程序,还是其他人将来必须维护这个程序?你可能需要考虑其他人的需求和偏好。

1.2 What Is the Task?

Each language is better for some tasks than others. Actually, though, this isn't as big a factor as you might think. All the languages you are likely to consider are Turing complete, so you can, in principle, program anything you like in them. However, depending on the tools and libraries available, some may be more practical than others. If you can describe the general area of a relevant task (say "data analysis" or "VR game") in a few words, you might like to search to get some ideas of languages to consider.

任务是什么?每种语言都比其他语言更适合某些任务。实际上,这并不像你想象的那么重要。你可能考虑的所有语言都是图灵完备的,所以原则上你可以用它们编程你喜欢的任何东西。然而,根据可用的工具和库,有些可能比其他更实用。如果你能用几句话描述相关任务的一般领域(比如“数据分析”或“VR游戏”),你可能会想通过搜索来获得一些语言的想法。

In 1936, Alan Turing described a simple mathematical model of a computer, which we now call, in his honour, the Turing machine. The Turing-computable functions are the mathematical functions (the mappings from input to output) that can be expressed using such a machine. You might expect that your choice of programming language would have an important effect on which functions you could express. Remarkably, this turns out not to be so. Although programming languages differ in what can be conveniently expressed, modern programming languages do not have, in principle, any more expressive power than very old ones, or even than Turing machines. We call any language in which you can program all the Turing-computable functions Turing complete.

1936年,阿兰·图灵描述了一个简单的计算机数学模型,为了纪念他,我们现在称之为图灵机。图灵可计算函数是可以使用这种机器表达的数学函数(从输入到输出的映射)。您可能希望您选择的编程语言会对您可以表达的函数产生重要影响。值得注意的是,事实并非如此。尽管编程语言在可以方便地表达的内容上有所不同,但原则上,现代编程语言没有比非常古老的语言,甚至比图灵机更强大的表达能力。我们把任何一种可以用图灵机编程的语言称为图灵完备。

1.3 Which Language(s) Do You Know Already?

This factor can operate eitherway. Youmay prefer to use a language you’re already familiar with, even if it isn’t ideal for the task, so that you can focus on things other than the nitty gritty of how you make the machine do what youwant. However, it can bemore fun to use a language that’s new to you, and – especially early in your programming life – it may be positively sensible to take the opportunity to widen your range. Writing a program you want written is the best way to develop fluency in a language. It’ll take a bit longer than if you do it in a language you already know, of course, but you may learn more in the process.

你已经知道哪种语言?这一因素可以以任何方式起作用。你可能更喜欢使用一种你已经熟悉的语言,即使它不适合做这项任务,这样你就可以专注于你如何让机器做你想做的事情。然而,使用一种新语言可能会更有趣,而且——尤其是在编程生涯的早期——抓住机会扩大范围可能是非常明智的。写一个你想写的程序是培养语言流利性的最好方法。当然,这要比你用一种你已经知道的语言做的时间长一点,但你可能会在这个过程中学到更多。

1.4 How Long Will the Program Need to Last?

If it must last for many years, youmay prefer to use a long-established language with a reputation for maintaining backwards compatibility, like Java, rather than a fast-moving language in which old programs often have to be changed if they are to work with the latest version of the language, like Haskell. Note, though, that you don't always know how long your program will last. If it turns out to be useful, it may last much longer than you expect!

该计划需要持续多久?如果它必须持续多年,你可能更喜欢使用一种久负盛名的语言,以保持向后兼容性而闻名,比如Java,而不是一种快速发展的语言,在这种语言中,如果旧程序要与最新版本的语言一起工作,比如Haskell,就必须对其进行更改。不过,请注意,你并不总是知道你的计划会持续多久。如果它被证明是有用的,它可能会比你预期的持续时间长得多!

I once wrote a program that was intended to be used only for a few months. This was in the early 1990s, and, alongside colleagues at the company where I was working at the time, I had just been trained on what we needed to do to make sure that our code still worked after the year 2000 – that is, how to make it "Y2K compliant". Just for fun, I made sure my code was Y2K compliant and added a comment that said so, even though there was no expectation it would still be in use in the year 2000. Many years later, after I had left the company, I met someonewhoworked there, having arrived after I left. She knew my name because she had seen it in a program. You've guessed it – it was that program, which was still in use many years after I would have expected it to be retired.

我曾经写过一个计划只使用几个月的程序。这是在20世纪90年代初,与我当时工作的公司同事一起,我刚刚接受了关于我们需要做什么的培训,以确保我们的代码在2000年后仍然有效——即如何使其“符合Y2K”。只是为了好玩,我确定了我的代码符合Y2K,并添加了一条评论,这样说,尽管没有人预期它会在2000年仍然使用。多年后,我离开公司后,我遇到了一个在那里工作的人,他是我离开后才来的。她知道我的名字,因为她在一个节目中看到过。你已经猜到了——就是那个程序,在我预期它会退役多年后,它仍然在使用。

1.5 Do You Plan to Distribute It?

If you want other people to be able to compile and/or run your program, you need to pay attention to how easy it will be for them. What will they need in the way of compiler, run-time software, libraries, etc. , and how likely are they to have the necessary things installed already?Will you need to provide instructions? Are there other dependencies or constraints? For example, what are you assuming about what operating system these people will be using?

你打算分发它吗?如果你想让其他人能够编译和/或运行你的程序,你需要注意对他们来说有多容易。他们在编译器、运行时软件、库等方面需要什么,他们已经安装了必要的东西的可能性有多大?您需要提供说明吗?是否存在其他依赖关系或约束?例如,您假设这些人将使用什么操作系统?

What Kind of Libraries or Components Will You Need? E.g. does your program need a graphical user interface (GUI), or to use a database? Check that good, suitable software that interacts well with your language exists.

您需要什么样的库或组件?比如,您的程序需要图形用户界面(GUI),还是使用数据库?检查是否存在与您的语言良好交互的良好、合适的软件。

Be quite cautious: sometimes you may find that there is a library, but that it is out of date, poorly documented, or hard to use. Unless you are really looking for a challenge, you should look for evidence that plenty of other people currently use the software you are considering.

要非常谨慎:有时你可能会发现有一个库,但它已经过时、文档不足或难以使用。除非你真的在寻找挑战,否则在使用考虑的软件之前,你应该寻找证据,证明目前有很多其他人在使用你正在考虑的软件。

1.6 What Kind of Mistakes Are Most Important to Guard Against?

This question, and its implications, are harder to get a handle on than some of the others, but are worth thinking about. For example, if your program will manipulate complex data structures, you may find the security of static type-checking important. On the other hand, if your program will involve a frequently changing interaction at the command line and lots of file handling, it might be more important to you that your language has convenient operating system interaction and string manipulation features.

什么样的错误最需要防范?这个问题及其含义比其他一些问题更难理解,但值得思考。例如,如果您的程序将操作复杂的数据结构,您可能会发现静态类型检查的安全性很重要。另一方面,如果您的程序将涉及命令行上频繁变化的交互和大量的文件处理,那么您的语言具有方便的操作系统交互和字符串操作功能对您来说可能更为重要。

2 A Few Languages You May Encounter

你可能会遇到的几种语言

Here is a list of just a few languages, focusing on the ones you are most likely to need to learn for university study. Perhaps you might challenge yourself to learn a little of each?

这里列出了一些语言,重点是你在大学学习中最可能需要学习的语言。也许你可以挑战自己,每一种语言都学一学?

Any such list is bound to be incomplete and controversial, however. If you come across a language that interests you, do not be put off learning it by the fact that it is not included here!

然而,任何这样的清单都必然是不完整的和有争议的。如果你遇到一门让你感兴趣的语言,不要因为它不在这里而推迟学习!

2.1 C

C is a low-level language, in the sense that it is "close to the machine": every computer has a C compiler. It may be a good choice if you want to understand how your program really works; for example, by learning about pointer arithmetic, you can improve your intuition for howdata is stored in the computer's memory. It is still a popular language for embedded systems. If you are interested in hardware, you are very likely to need to learn it.

C是一种低级语言,从“接近机器”的意义上说:每台计算机都有一个C编译器。如果你想了解你的程序是如何工作的,这可能是一个不错的选择;例如,通过学习指针算术运算,您可以提高数据如何存储在计算机内存中的直觉。它仍然是嵌入式系统的流行语言。如果你对硬件感兴趣,你很可能需要学习它。

2.2 C++

C++ layers object-oriented features on top of C. It is harder to write in than Java, but even today it is more efficient. If C++ is the right language for you, you probably don't need me to explain why that is.

C++将面向对象的特性层叠在C之上。它比Java更难编写,但即使在今天它也更高效。如果C++是适合你的语言,你可能不需要我解释为什么会这样。

2.3 Fortran

Fortran dates back to the 1950s and is now thought of as an oldfashioned language. However, it is still widely used in scientific computing.

Fortran可以追溯到20世纪50年代,现在被认为是一种老式语言。然而,它仍然被广泛用于科学计算。

2.4 Haskell

Haskell is one of the best established functional languages. It has a powerful static type system which can help you to avoid many kinds of bug. It is the language of choice for many experts in programming language theory. This has pluses and minuses: a plus is that learning Haskell will make many advanced features and a body of research available to you, while a minus is that it can be hard to get simple answers to simple questions, and that the compilers and libraries tend to change fast and unpredictably.

Haskell是最成熟的函数式语言之一。它有一个强大的静态类型系统,可以帮助您避免各种错误。它是许多编程语言理论专家的首选语言。这有好处也有坏处:一个好处是学习Haskell将使您可以使用许多高级功能和大量研究,而另一个缺点是很难获得简单问题的简单答案,而且编译器和库往往变化迅速且不可预测。

2.5 Java

Java is a very widely used, robust language, well-supported by tools, books, tutorials, etc. It is especially popular for enterprise systems, that is, for systems that support the business process of complex organisations. It can be verbose.

Java是一种应用非常广泛、健壮的语言,得到了工具、书籍、教程等的支持。它尤其适用于企业系统,即支持复杂组织业务流程的系统。它可能很冗长。

2.6 JavaScript

JavaScript is a natural choice for web applications. There is a huge range of frameworks and libraries, so that the process of learning JavaScript is arguably more about learning some of its frameworks. JavaScript is dynamically typed, but if that is a problem for you you might want to consider TypeScript, which is essentially a statically typed variant of JavaScript, which compiles down to JavaScript. Be aware that the "Java" in "JavaScript" is there essentially for historical marketing reasons: when JavaScript came out, in 1995, Java was new and fashionable. The two languages are very different.

JavaScript是web应用程序的自然选择。有大量的框架和库,因此学习JavaScript的过程可以说更多的是学习它的一些框架。JavaScript是动态类型的,但如果这对你来说是个问题,你可能需要考虑TypeScript,它本质上是JavaScript的静态类型变体,可以编译成JavaScript。请注意,“JavaScript”中的“Java”本质上是出于历史营销原因:1995年JavaScript问世时,Java是一种新的时尚。这两种语言非常不同。

2.7 MATLAB

MATLAB is a language for numerical computing, widely used by scientists and engineers. Unlike most of the languages in this list, it has no open-source implementation.

MATLAB是一种数值计算语言,被科学家和工程师广泛使用。与此列表中的大多数语言不同,它没有开源实现。

2.8 Perl

Perl stands for practical extraction and report language, or for pathologically eclectic rubbish lister. A Perl motto is "There's more than one way to do it. "Once you know the language well, it is extremely convenient for many tasks, especially those that involve interacting with the operating system and manipulating strings. But it can be difficult to track down bugs in your Perl programs. I love Perl (which is really why it's in this list: it's not commonly taught as a first programming language, though you may come across it in passing, almost anywhere) but I find it hard to argue for you learning it now, unless you have existing Perl code to maintain. Do at least consider using Python instead.

Perl代表实用的提取和报告语言,或者说是兼收并蓄的垃圾桶。Perl的座右铭是“有不止一种方法可以做到这一点。”一旦你熟悉了这门语言,它对许多任务都非常方便,尤其是那些涉及到与操作系统交互和操作字符串的任务。但追踪Perl程序中的错误可能很困难。我喜欢Perl(这就是为什么它会出现在这个列表中的原因:它通常不是作为第一种编程语言来教授的,尽管你可能会在任何地方偶然遇到它),但我觉得很难支持你现在学习它,除非你有现有的Perl代码需要维护。至少考虑改用Python。

2.9 PHP

PHP is a scripting language, widely used for web development (especially server-side scripting). It is easy to get started with, and is popular in web applications courses that have to cover a lot of ground. It has a reputation as a language which tends to encourage the writing of unmaintainable, insecure code – but this may be somewhat unfair, especially as recent versions of the language have improved.

PHP是一种脚本语言,广泛用于web开发(尤其是服务器端脚本)。它很容易入门,并且在必须涵盖很多领域的web应用程序课程中很受欢迎。它被誉为一种倾向于鼓励编写不可维护、不安全代码的语言——但这可能有些不公平,尤其是随着该语言的最新版本的改进。

2.10 Prolog

Prolog is an old language which is having a resurgence with the AI boom. It has similar pattern-matching to functional languages like Haskell, but otherwise feels quite different from any of the other languages in this list: it's described as a logic programming language, and the fundamental idea is that you encode some facts and then ask questions about them.

Prolog是一种古老的语言,随着人工智能的繁荣而重新兴起。它与Haskell等函数式语言有相似的模式匹配,但在其他方面感觉与本列表中的任何其他语言都截然不同:它被描述为一种逻辑编程语言,其基本思想是对一些事实进行编码,然后提出关于它们的问题。

2.11 Python

By some measures Python is the most popular first programming language. I still rather resent Python for stealing the place in the language ecosystem that used to be occupied by Perl, one of my all-time favourite languages. There's no denying, though, that Python has many advantages over Perl. Perhaps the easiest way to summarise the difference is to point out that one of the principles included in the Zen of Python2 is "There should be one – and preferably only one – obvious way to do it. "

从某种程度上说,Python是最流行的第一种编程语言。我仍然对Python窃取了语言生态系统中曾经被Perl(我一直最喜欢的语言之一)占据的位置感到愤慨。但不可否认,Python比Perl有很多优势。也许总结差异的最简单方法是指出Python2禅宗中包含的原则之一是“应该有一种——最好是只有一种——显而易见的方法来做到这一点。”

Python is very easy to get started with, very popular, and a good all-purpose language. It's not statically typed, which means you may regret it if your program gets big and complicated. It's a popular language for data science and for machine learning, and has good facilities for manipulating strings. Its popularity means that there are libraries and frameworks for everything. There are significant differences between Python versions 2 and 3, so be sure you know which you are to use.

Python非常容易入门,非常流行,是一种很好的通用语言。它不是静态类型的,这意味着如果你的程序变得庞大和复杂,你可能会后悔。它是一种用于数据科学和机器学习的流行语言,并且具有很好的操作字符串的功能。它的流行意味着有各种各样的库和框架。Python版本2和3之间有很大的区别,所以一定要知道要使用哪一个。

2.12 R

R is a language for doing statistics with data. It provides convenient facilities for graphing and analysis. Unlike MATLAB, which is sometimes used for the same tasks, it is an open-source language.

R是一种用数据进行统计的语言。它为绘图和分析提供了便利的设施。与有时用于相同任务的MATLAB不同,它是一种开源语言。

2.13 Racket/Scheme

Racket/Scheme Widely used as a teaching language, but not so popular commercially, Scheme is a functional language from the Lisp family. Racket, originally a renaming of a version of Scheme, is now more popular. It has a minimalist language design philosophy, and is a good language to learn if you're interested in how programming languages work.

Racket/Scheme广泛用作教学语言,但在商业上并不流行,Scheme是Lisp家族的一种功能语言。Racket最初是Scheme版本的更名,现在更受欢迎。它具有极简主义的语言设计理念,如果您对编程语言的工作方式感兴趣,它是一种很好的学习语言。

Since all of these languages are widely used, and most of them often act as first programming languages, there is good teaching material in all of them. Experiment!

由于所有这些语言都被广泛使用,而且大多数语言通常充当第一编程语言,因此所有这些语言中都有很好的教材。放心深度吧!

3 The Changing Landscape of Languages 语言环境的变化

The languages as above in the previous section date back to the last century. But new programming languages are being invented all the time, and there are many other old ones I could have mentioned, too. You might like to search as well as, of course, watching out for information on which languages are used by people and organisations whose work you find interesting. The TIOBE index attempts a regularly updated summary of the popularity of programming languages – but, unsurprisingly, its methodology is contested.

上述的所有语言都可以追溯到上个世纪。但新的编程语言一直在被发明,我也可以提到许多其他的旧编程语言。你可能想搜索,当然,也可以留意那些你觉得工作有趣的人和组织使用哪些语言的信息。TIOBE index试图定期更新编程语言流行程度的摘要,但不出所料,它的方法受到了质疑。

Above all, be aware that if you plan to have a career that involves programming, the language you learn first is unlikely to be the one you use most in your life. It is important to learn to write good programs in your first language, but it is equally important to set yourself on the path towards writing good programs in languages that have not yet been invented. To do this, make a habit of thinking about the why, as well as the what, of your programming decisions.

最重要的是,要知道,如果你打算从事编程职业,你首先学习的语言不太可能是你一生中使用最多的语言。学会用你的第一语言编写好的程序是很重要的,但同样重要的是让自己走上用尚未发明的语言编写好程序的道路。要做到这一点,要养成一种习惯,思考编程决策的原因和内容。

ref:

Perdita Stevens - How to Write Good Programs_ A Guide for Students-Cambridge University Press (2020)

-End-