是什么让一种语言可读或不可读?

发布于 2024-08-05 22:23:58 字数 146 浏览 12 评论 0原文

我听人们说一年后他们可以理解他们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

枕花眠 2024-08-12 22:23:58

经验。

Experience.

仙气飘飘 2024-08-12 22:23:58

在我看来,最重要的事情之一就是重要的空白区域。块缩进有很长的路要走,像 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.

幸福%小乖 2024-08-12 22:23:58

当代码以明确的“陈述你想要做什么”的方式编写时,它是可读的。
这只取决于语言,

  • 它允许你表达你想要的东西(函数式编程!),
  • 它不强调神秘的语句,

剩下的取决于你用来编写代码的风格(甚至 Perl 也可以理解!),但某些语言更容易hacky语句。

清楚:

expr = if not (ConditionA and ConditionB) then  ...  else  ... 

不清楚:

expr = (!(conditionA && conditionB)) ? ... : ...

清楚:

foreach line in lines:
    if (line =~ /regex/):
        // code

不清楚:

... if /regex/ foreach(@lines);

清楚:

x = length [ x | x <- [1..10], even x ]

不清楚:

int x = 0;
for (int i = 1; i <= 10; ++i)
    if ((i&&1)==0) ++x;

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

  • it allows you to express what you want (functional-programming!)
  • it doesn't emphasize on cryptical statements

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:

expr = if not (ConditionA and ConditionB) then  ...  else  ... 

Unclear:

expr = (!(conditionA && conditionB)) ? ... : ...

Clear:

foreach line in lines:
    if (line =~ /regex/):
        // code

Unclear:

... if /regex/ foreach(@lines);

Clear:

x = length [ x | x <- [1..10], even x ]

Unclear:

int x = 0;
for (int i = 1; i <= 10; ++i)
    if ((i&&1)==0) ++x;
稳稳的幸福 2024-08-12 22:23:58

一般来说,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.

悟红尘 2024-08-12 22:23:58

可读性是一个需要大量输入的函数。我认为不可能编制一份完整的影响语言可读性的列表。最通用的描述方式是“最小化认知负荷”。几个主要因素:

  • 含义的微妙性。如果两个代码片段乍一看非常相似,但执行不同的操作,则会损害可读性,因为读者必须停下来推断实际发生的情况。
  • 无意义的代码——又名样板文件。这并不一定意味着代码什么都不做,而是代码不能告诉我我们实际在做什么。每一段没有表达函数或对象实际意图的代码都会大大降低可读性。
  • 临时抱佛脚的意思——又名高尔夫。这与样板问题相反。有可能将代码压缩得如此之远,以至于读者被迫停下来并几乎逐个字符地检查它。发生这种情况的确切路线有些主观(这就是为什么有些人喜欢 Perl 而有些人讨厌它的部分原因),但这绝对是一个真实的现象。

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:

  • Subtleties of meaning. If two code snippets look very similar at a glance but do different things, it hurts readability because the reader has to stop and deduce what's actually happening.
  • Meaningless code — aka boilerplate. This doesn't necessarily mean code that does nothing, but code that doesn't tell me anything about what we're actually doing. Every bit of code that doesn't express the actual intent of a function or object reduces readability by that much.
  • Cramming meaning — aka golf. This is the opposite of the boilerplate problem. It's possible to compress code so far that the reader is forced to stop and examine it pretty much character by character. The exact line where this occurs is somewhat subjective (which is part of why some people love Perl and some people hate it), but it's definitely a real phenomenon.
哭了丶谁疼 2024-08-12 22:23:58

程序员使代码可读或不可读,而不是语言。不这么想只是在欺骗自己。这是因为,唯一有资格判断可读性的人是那些了解该语言的人。对于非程序员来说,所有语言都同样难以阅读。

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.

深爱成瘾 2024-08-12 22:23:58

我听到人们说一年后他们可以理解他们的 python 代码,但不能理解他们的 XYZ 代码。为什么?

首先,我不认为人们只是根据语法这么说。还有很多其他因素需要考虑,仅举几例:

  1. 事实上,某些语言倾向于只提倡一种正确的做某事的方法(例如 Python),而其他语言则提倡许多不同的方法。方式(例如 Ruby,据我所知[免责声明:我不是 Ruby 程序员])
  2. 该语言拥有的库。设计得更好的产品往往非常容易理解,不需要文档,这也有助于记忆。因此,具有良好库的语言将使事情变得更容易。

话虽如此,我个人对 Python 的看法是,许多人称其为“可执行伪代码”。它支持各种往往出现在伪代码中的事物,并且作为扩展,它们是思考事物的标准方式。

此外,Python 的非 C 语法是很多人不喜欢它的特性之一,也使 Python 看起来更像伪代码。

嗯,这就是我对 Python 可读性的看法。

I heard people say they can understand their python code a year later but not their XYZ code. Why?

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:

  1. The fact that some languages tend to promote only one right way to do something (like Python), and others promote many different ways (Ruby for example, from what I hear [disclaimer: I am not a Ruby programmer])
  2. The libraries the language has. The better designed ones tend to be incredibly easy to understand without needing documentation, and this also tends to help remember. A language with good libraries will therefore make things easier.

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.

浅黛梨妆こ 2024-08-12 22:23:58

老实说,当谈到什么使语言可读时,它似乎确实可以归结为简单性和个人喜好的结合。 (当然 - 如果你足够努力,总是有可能用任何语言编写不可读的代码)。由于个人偏好无法真正控制,因此它归结为表达的难易程度 - 使用简单功能的语言越复杂,从可读性的角度来看,该语言通常可能越困难。

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.

场罚期间 2024-08-12 22:23:58

当一个字符就足够时,需要一个词 - Pascal 和 VB 花园中的一块石头。

比较:

Block ()
Begin
  // Content
End

Block
{
  // Content
}

它需要额外的大脑处理来阅读一个单词并在心理上将其与一个概念联系起来,而单个符号可以立即通过其图像识别。

这与自然语言、普通文本语言与象形文字符号语言(亚洲语族)的区别是一样的。第一组的处理速度较慢,因为基本上文本被解析为一组概念,而象形文字代表概念本身。将其与您已知的内容进行比较 - XML 的序列化/反序列化会比二进制格式的自定义搜索更快吗?

A word required when one character will suffice - a stone in the garden of Pascal and VB.

Compare:

Block ()
Begin
  // Content
End

vs.

Block
{
  // Content
}

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?

自此以后,行同陌路 2024-08-12 22:23:58

恕我直言,计算机语言越类似于口语,它的可读性就越高。举个极端的例子,比如 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.

断舍离 2024-08-12 22:23:58

我认为这更多地与编写代码的人有关,而不是与实际的语言本身有关。您可以用任何语言编写非常可读的代码,也可以用任何语言编写不可读的代码。即使是复杂的正则表达式也可以进行格式化和注释,以使其易于阅读。

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.

雨夜星沙 2024-08-12 22:23:58

我的一位同事曾经说过这样一句话:“你可以用任何语言编写垃圾代码。”我喜欢它并想今天分享。是什么让代码可读?这是我的想法

  1. 阅读语言语法的能力。
  2. 格式良好的代码。
  3. 有意义地命名的变量和函数
  4. 用于解释复杂处理的注释。请注意,过多的注释会使代码难以阅读。
  5. 短函数比长函数更容易阅读。

这些都与语言无关,这一切都与编码人员及其工作质量有关。

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

  1. The ability to read the syntax of the language.
  2. Well formatted code.
  3. Meaningfully named variables and functions
  4. Comments to explain complex processing. Beware, too much commentes can make the code hard to read
  5. Short functions are easier to read than long ones.

None of these have anything to do with the language, it's all about the coder, and the quality of their work.

ゞ花落谁相伴 2024-08-12 22:23:58

我会尝试说代码因其简单而可读。

你必须第一眼就明白它的作用和目的是什么。为什么要编写一千行代码,而只需几行代码就可以满足要求?

例如,这就是像 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.

韬韬不绝 2024-08-12 22:23:58

对我来说,主要问题是该语言是否允许您开发更可读的抽象,从而防止迷失在细节中。
这就是 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.

笑红尘 2024-08-12 22:23:58

我认为,语言设计(对于普通语言,不是 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.

满身野味 2024-08-12 22:23:58

这都是关于干净的代码。

保持它小、简单、命名好、格式好。

class ShoppingCart { 
  def add(item) {
    println "you added some $item"
  }

  def remove(item) {
    println "you just took out the $item"
  }
}

def myCart = new ShoppingCart()

myCart.with {
  add "juice"
  add "milk"
  add "cookies"
  add "eggs"
  remove "cookies"
}

It's all about clean code.

Keep it small, simple, well named, and formatted.

class ShoppingCart { 
  def add(item) {
    println "you added some $item"
  }

  def remove(item) {
    println "you just took out the $item"
  }
}

def myCart = new ShoppingCart()

myCart.with {
  add "juice"
  add "milk"
  add "cookies"
  add "eggs"
  remove "cookies"
}
看透却不说透 2024-08-12 22:23:58

读者的识字水平。

The literacy level of the reader.

心碎无痕… 2024-08-12 22:23:58

确实有两个不同的方面。首先是语法和空格。 Python 强制执行空白标准,删除不必要的 {、} 和 ;人物。这对眼睛来说很容易。其次,也是最重要的,表达的清晰度——即将代码映射回您的思维方式是多么容易。编程语言中有几个特性(和非特性)有助于后一点:

  1. 禁止跳转。 C中的goto语句就是一个典型的例子。不会不断耗尽结构化块的代码更容易阅读。
  2. 最大限度地减少副作用。全局变量是邪恶的,还记得吗?
  3. 使用更多定制功能。您的头脑如何跟踪具有 5 个迭代变量的 for 循环? Common Lisp 循环 更容易阅读(虽然很难编写,但这是一个不同的故事)
  4. 词法闭包。您只需查看变量即可算出它的值,而不是在头脑中运行代码,然后找出哪个语句隐藏了哪个语句。

几个例子:

(loop
   do for tweet = (master-response-parser (twitter-show-status tweet-id))
   for tweet-id = tweet-id then (gethash in-reply-to tweet)
   while tweet-id
   collecting tweet)

listOfFacs = [x | x <- [1 ..], x == sumOfFacDigits x]
    where sumOfFacDigits x = sum [factorial (x `div` y) | y <- [1 .. 10]]

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:

  1. Disallowing jumps. The goto statement in C is a typical example. Code that doesn't keep running out of structured blocks is easier to read.
  2. Minimizing side-effects. Global variables are evil, remember?
  3. Using more tailored functions. How can your head track a for loop with 5 iteration variables? The Common Lisp loop is much easier to read (although VERY difficult to write, but that's a different story)
  4. Lexical closures. You can figure out a variable's value by just looking at it, as opposed to running the code in your head, and then figuring out which statement is shadowing which.

A couple of examples:

(loop
   do for tweet = (master-response-parser (twitter-show-status tweet-id))
   for tweet-id = tweet-id then (gethash in-reply-to tweet)
   while tweet-id
   collecting tweet)

and

listOfFacs = [x | x <- [1 ..], x == sumOfFacDigits x]
    where sumOfFacDigits x = sum [factorial (x `div` y) | y <- [1 .. 10]]
吹泡泡o 2024-08-12 22:23:58

关于语法,我认为它必须具有相当的描述性。例如,在许多语言中,都有 foreach 语句,每种语言的处理方式都略有不同。

// PHP
foreach ($array as $variable) ...

// Ruby
array.each{ |variable| ... }

// Python
for variable in array ...

// Java
for (String variable : array)

老实说,我觉得PHP和Python有最清晰的理解方式,但是,这一切都归结为程序员想要变得多么聪明和清晰。例如,一个糟糕的程序员可能会写出以下内容:

// PHP
foreach ($user as $_user) ...

我的猜测是,除非您回溯并尝试找出 $user 是什么以及为什么,否则您几乎不知道代码到底在做什么你正在迭代它。清晰简洁就是让小块代码变得有意义,而不必回溯程序来找出变量/函数名称是什么。

另外,我必须完全同意空白。制表符、换行符和运算符之间的间距确实会产生巨大的差异!

编辑1:我可能还会插话,一些语言具有现成的语法和工具,可以让事情变得更加清晰。以 Ruby 为例:

if [1,2,3].include? variable then ... end

诗句,比如 Java:

if (variable != 1 || variable != 2 || variable != 3) { ... }

其中一个(恕我直言)肯定比另一个更清晰和可读。

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.

// PHP
foreach ($array as $variable) ...

// Ruby
array.each{ |variable| ... }

// Python
for variable in array ...

// Java
for (String variable : array)

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:

// PHP
foreach ($user as $_user) ...

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:

if [1,2,3].include? variable then ... end

verses, say Java:

if (variable != 1 || variable != 2 || variable != 3) { ... }

One of these (IMHO) is certainly more clear and readable than the other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文