是什么让一种语言可读或不可读?
我听人们说一年后他们可以理解他们的 python 代码,但不能理解他们的 XYZ 代码。为什么?我不知道 python 语法有什么好处,或者其他语法有什么坏处。我喜欢 C#,但我感觉 VB.NET 代码更容易阅读。我正在做语言设计,那么你发现什么使代码/语法/语言可读或不可读?
I heard people say they can understand their python code a year later but not their XYZ code. Why? I dont know what is good about python syntax or what is bad about another. I like C# but i have a feeling VB.NET code is easier to read. I am doing language design so what do you find makes code/syntax/language readable or not readable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
经验。
Experience.
在我看来,最重要的事情之一就是重要的空白区域。块缩进有很长的路要走,像 Python 和 F# 这样提供一定程度的重要空白的语言可以帮助提高可读性。
像 Java 和 C# 这样的代码往往是结构化的,可读性成为其编码方式的焦点,而不是语言本身的焦点。
IMO, one of the big things is significant white space. Block indention goes a long ways and languages like Python and F# that provide a level of significant white space can help with readability.
Code like Java and C# tend to be structured and the readability becomes a focal point of how it was coded to begin with and not of the language itself.
当代码以明确的“陈述你想要做什么”的方式编写时,它是可读的。
这只取决于语言,
剩下的取决于你用来编写代码的风格(甚至 Perl 也可以理解!),但某些语言更容易hacky语句。
清楚:
不清楚:
清楚:
不清楚:
清楚:
不清楚:
Code is readable when its written in a style of explicit "stating what you want to do".
This only depends on the language in sofar
The rest depends on the style you use to write code (even Perl can be understandable!), but certain languages make it easier to hacky statements.
Clear:
Unclear:
Clear:
Unclear:
Clear:
Unclear:
一般来说,Python 之所以被认为具有可读性是因为它强制采用标准化缩进。这意味着您永远不会被迫怀疑自己是处于 if 块还是函数中,这一点一目了然。因此,即使是写得不好的代码也会变得显而易见。
由于同样的原因(或者更确切地说,相反),我通常认为难以阅读的一种语言是 PHP。由于允许程序员随意缩进并将变量存储在任何地方,因此它很快就会变得令人困惑。此外,由于 PHP 历史上没有区分大小写的函数名称(我相信 PHP < 4.4.7),这意味着核心语言的实现实际上也没有一致性......(别明白我的意思)错了,我喜欢这种语言,但糟糕的编码员真的会弄得一团糟)。
对于不守纪律的开发人员来说,JavaScript 也存在很多问题。您会发现自己想知道变量是在哪里定义的以及您所处的范围。代码不会集中在一个位置,而是分布在多个文件中,并且经常潜伏在意想不到的地方。
ActionScript 3 好一些。一般来说,已经采取了让每个人都使用类似语法的举措,Adobe 甚至定义了其标准并使它们易于访问和通用。不难看出一家营利性公司支持的 ECMAScript 实现比通用实现如何优越。
Generally what makes Python considered readable is that it forces a standardized indentation. This means that you'll never be forced to wonder whether you're in an if block or a function, it is clear as day. Even poorly written code therefore becomes obvious.
One language which I generally consider difficult to read is PHP for the same reason (or rather, its opposite). Since programmers are allowed to indent at will, and store variables anywhere, it can get convoluted very quickly. Further, since PHP historically did not have case sensitive function names (PHP < 4.4.7 I believe), this means that there really isn't a consistency in the implementation of the core language either... (Don't get me wrong, I like the language, but a bad coder can REALLY make a mess).
JavaScript also has a lot of problems with undisciplined developers. You'll find yourself wondering where variables have been defined and what scope you're in. Code will not be in one consolidated place, but rather spread across multiple files, and often lurking where unexpected.
ActionScript 3 is a bit better. Generally, there has been a move to have everyone use similar syntax's, and Adobe has gone so far as to define its standards and make them accessible and common. It does not take much to see how the ECMAScript implementation which is supported by a for-profit company is superior to the generalized one.
可读性是一个需要大量输入的函数。我认为不可能编制一份完整的影响语言可读性的列表。最通用的描述方式是“最小化认知负荷”。几个主要因素:
Readability is a function that takes a lot of inputs. I don't think it's really possible to compile a full list of things that can affect a language's readability. The most general way to describe it is "minimizing cognitive load." A few major factors:
程序员使代码可读或不可读,而不是语言。不这么想只是在欺骗自己。这是因为,唯一有资格判断可读性的人是那些了解该语言的人。对于非程序员来说,所有语言都同样难以阅读。
The programmer makes code readable or unreadable, not the language. Thinking otherwise is just fooling yourself. This is because the only people who are qualified to judge readability are those who know the language. To the non-programmer, all languages are equally unreadable.
首先,我不认为人们只是根据语法这么说。还有很多其他因素需要考虑,仅举几例:
话虽如此,我个人对 Python 的看法是,许多人称其为“可执行伪代码”。它支持各种往往出现在伪代码中的事物,并且作为扩展,它们是思考事物的标准方式。
此外,Python 的非 C 语法是很多人不喜欢它的特性之一,也使 Python 看起来更像伪代码。
嗯,这就是我对 Python 可读性的看法。
Firstly, I don't think that people say that based solely on syntax. There are a lot of other factors to take into consideration, to name just a few:
Having said that, my personal take on Python is the fact that many people call it "executable pseud-code". It supports a wide variety of things that tend to appear in pseudo-code, and as an extension, are the standard way to think about things.
Also, Python's un-C-like syntax, one of the features that make it so disliked by so many people, also makes Python look more like pseudocode.
Well, that's my take on Python's readability.
老实说,当谈到什么使语言可读时,它似乎确实可以归结为简单性和个人喜好的结合。 (当然 - 如果你足够努力,总是有可能用任何语言编写不可读的代码)。由于个人偏好无法真正控制,因此它归结为表达的难易程度 - 使用简单功能的语言越复杂,从可读性的角度来看,该语言通常可能越困难。
To be honest when it comes to what makes a language readable it is really seems to boil down to a combination of simplicity and personal preference. (Of course - it is always possible to write unreadable code in any language if you try hard enough). Since personal preference can't really be controlled, it comes down to ease of expression - the more complicated it is in a language to use simple features, the more difficult that language is likely to be in general from a readability standpoint.
当一个字符就足够时,需要一个词 - Pascal 和 VB 花园中的一块石头。
比较:
与
它需要额外的大脑处理来阅读一个单词并在心理上将其与一个概念联系起来,而单个符号可以立即通过其图像识别。
这与自然语言、普通文本语言与象形文字符号语言(亚洲语族)的区别是一样的。第一组的处理速度较慢,因为基本上文本被解析为一组概念,而象形文字代表概念本身。将其与您已知的内容进行比较 - XML 的序列化/反序列化会比二进制格式的自定义搜索更快吗?
A word required when one character will suffice - a stone in the garden of Pascal and VB.
Compare:
vs.
It requires extra brain processing to read a word and mentally associate it with a concept, while a single symbol is immediately recognized by its image.
It is the same thing as the difference with natural languages, usual textual languages vs. symbol languages with hieroglyphs (Asian group). The processing of the first group is slower because basically a text is parsed to a set of concepts while hieroglyphs represent concepts themselves. Compare it with what you already know - will a serialization/deserialization from an XML be faster than a custom search over a binary format?
恕我直言,计算机语言越类似于口语,它的可读性就越高。举个极端的例子,比如 J 、 Whitespace 或 Brainfuck 这样的语言……对于未经训练的人来说是完全无法阅读的。
但类似英语的语言更容易理解。正如 COBOL 可以证明的那样,这并不意味着它是最好的语言。
IMHO, the more a computer language resembles a spoken language, the more readable it is. For extreme examples, take languages like J or Whitespace or Brainfuck... completely unreadable to the untrained eye.
But a language that resembles English can be more easily understood. Not that this makes it the best language, as COBOL can attest.
我认为这更多地与编写代码的人有关,而不是与实际的语言本身有关。您可以用任何语言编写非常可读的代码,也可以用任何语言编写不可读的代码。即使是复杂的正则表达式也可以进行格式化和注释,以使其易于阅读。
I think it has more to do with the person writing the code rather than the actual language itself. You can write very readable code in any language, and unreadable code in any language. Even a complex Regular expression can be formatted and commented so as to make it easy to read.
我的一位同事曾经说过这样一句话:“你可以用任何语言编写垃圾代码。”我喜欢它并想今天分享。是什么让代码可读?这是我的想法
这些都与语言无关,这一切都与编码人员及其工作质量有关。
a coworker of mine use to have a saying: "You can write crap code in any language." I liked it and wanted to share today. What makes code readable? Here are my thoughts
None of these have anything to do with the language, it's all about the coder, and the quality of their work.
我会尝试说代码因其简单而可读。
你必须第一眼就明白它的作用和目的是什么。为什么要编写一千行代码,而只需几行代码就可以满足要求?
例如,这就是像 F# 这样的函数式语言的精神。
I will try saying that a code is readable by its simplicity.
You got to get at first sight what it does and what its purpose is. Why write a thousand lines of code when only a few does what is required?
This is the spirit of a functional language like F#, for instance.
对我来说,主要问题是该语言是否允许您开发更可读的抽象,从而防止迷失在细节中。
这就是 OOP 在隐藏细节方面非常方便的地方。如果我可以将任务的详细信息隐藏在具有常见概念(即 C++ 中的迭代器)行为的接口后面,我通常不必阅读实现细节。
For me its mainly the question of wether the language allows you to develop more readable abstractions which do prevent getting lost in details.
This is where OOP comes in very handy with the hiding of details. If i can hide the details of a task behind an interface that has the behavior of a common concept (i.e. iterators in C++) i usually don't have to read the implementation details.
我认为,语言设计(对于普通语言,不是 Brainfuck :))并不重要。为了使代码可读,您应该遵循标准、代码约定,并且不要忘记重构。
I think, language design (for normal languages, not brainfuck :) ) not matters. To make code readable you should follow standards, code conventions, and don't forget about refactoring.
这都是关于干净的代码。
保持它小、简单、命名好、格式好。
It's all about clean code.
Keep it small, simple, well named, and formatted.
读者的识字水平。
The literacy level of the reader.
确实有两个不同的方面。首先是语法和空格。 Python 强制执行空白标准,删除不必要的 {、} 和 ;人物。这对眼睛来说很容易。其次,也是最重要的,表达的清晰度——即将代码映射回您的思维方式是多么容易。编程语言中有几个特性(和非特性)有助于后一点:
几个例子:
和
Two distinct aspects, really. First is syntax and whitespace. Python enforces a whitespace standard, dropping unnecessary {, } and ; characters. This makes it easy on the eyes. Second, and most importantly, clarity of expression- i.e. how easy is it to map code back to the way you think. There are several features (and non-features) in programming languages that contribute to the latter point:
A couple of examples:
and
关于语法,我认为它必须具有相当的描述性。例如,在许多语言中,都有
foreach
语句,每种语言的处理方式都略有不同。老实说,我觉得PHP和Python有最清晰的理解方式,但是,这一切都归结为程序员想要变得多么聪明和清晰。例如,一个糟糕的程序员可能会写出以下内容:
我的猜测是,除非您回溯并尝试找出
$user
是什么以及为什么,否则您几乎不知道代码到底在做什么你正在迭代它。清晰简洁就是让小块代码变得有意义,而不必回溯程序来找出变量/函数名称是什么。另外,我必须完全同意空白。制表符、换行符和运算符之间的间距确实会产生巨大的差异!
编辑1:我可能还会插话,一些语言具有现成的语法和工具,可以让事情变得更加清晰。以 Ruby 为例:
诗句,比如 Java:
其中一个(恕我直言)肯定比另一个更清晰和可读。
Concerning the syntax, I think it it imperative that it be fairly descriptive. For instance, in many languages, you have the
foreach
statement, and each one handles it a bit differently.Honestly, I feel that PHP and Python have the clearest means of understanding, but, it all boils down to how smart and clear the programmer wants to be. For instance, a bad programmer could write the following:
My guess is that you would have almost no idea what the heck the code is doing unless you tracked back and attempted to figure out what
$user
was and why you were iterating over it. Being clear and concise is all about making small chunks of code make sense without having to trace back through the program to figure out what variables/function names are.Also, I would have to completely agree with whitespace. Tabs, newlines and spacing in-between operators really make a huge difference!
Edit 1: I might also interject that some languages have the syntax and tools readily available to make things more clear. Take Ruby for example:
verses, say Java:
One of these (IMHO) is certainly more clear and readable than the other.