使用几种同样抽象的语法变体来编码某些东西有什么价值?
我目前正在阅读有关 Ruby 的内容。我认为这是一种很好的语言,但我有点烦恼有这么多等效的方法,这些方法只是在语法上略有不同,用于编码相同的操作。例如,unless conditional
语句,它完全等同于编写 if !conditional
。
对我来说,这并没有增加任何表达能力,只是让遵循其他人的代码变得更加费力。我缺少什么好处吗(除了迎合不同的口味之外,我觉得这没有说服力,因为人们通常不会因为某些语法关键字与他们的口味不符而拒绝一种语言)?
可能每种语言都有更多这样的例子。我只是用这个例子,因为我认为它特别缺乏合理的辩护。
I am currently reading up on Ruby. I think it is a nice language, but I am a bit bothered by having so many equivalent ways, that are only slightly different in syntax, for coding the same action. For example, the unless conditional
statement, which is fully equivalent to writing if !conditional
.
To me, this does not add any expressive power, just makes it more taxing to follow other people's code. Is there a benefit that I am missing (other than catering to different tastes, which I don't find convincing, since people don't usually reject a language because some syntactic keywords didn't match their taste)?
There are more examples of this in probably every language. I am only using this example because I though it particularly lacking a reasonable defense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
迎合不同的口味是可能的解释。这一理念改编自 Perl,被称为 TIMTOWTDI (有不止一种方法可以做到这一点)它)。
这确实有优点,例如,它有助于在 Ruby 中创建丰富的特定于领域的语言,因为现有的语法结构可以以新的、有趣的方式组合起来。
但这种技术也有很多批评者,尤其是 Python 力求相反 :
Catering to different tastes is the likely explanation. This philosophy is adapted from Perl and it’s known as TIMTOWTDI (There's more than one way to do it).
This does have advantages, for example it facilitates the creation of rich domain-specific languages within Ruby, since existing syntax constructs can be combined in new, interesting ways.
But this technique also has many detractors, and Python in particular strives for the opposite:
Perl 也有一个“除非”结构,我相信它源于 Larry Wall 作为语言学家的背景。
如果我们能够在心里快速确定语义,那么分支结构就更容易理解,例如,采用这样的双重否定:
导致我虚弱的大脑跳过一个节拍“所以,如果没有禁用......等等......对,所以如果启用?”
除非
可以让这更像自然语言Perl also has an 'unless' construct, and I believe it stems from Larry Wall's background as a linguist.
Branching constructs are easier to understand if we can mentally determine the semantics quickly, for example, take a double negative like this:
causes my feeble brain to skip a beat "so, if not disabled...wait....right, so if enabled?"
unless
can make this a little more like natural language不同的语法变化可以在不同的时间更方便、更容易阅读。对于开发人员来说,它是一个有用的工具,能够为特定任务选择最易读和最明显的语法。
这可以在您的
unless
示例中显示:对于一个简单的布尔表达式,使用 except 或 if 没有真正的优势,尽管可能存在个人偏好,具体取决于特定开发人员认为它看起来有多好:
这些同样易于阅读(和编写)。
然而,对于更复杂的布尔表达式,
if
形式有缺点:这里,unless 形式更容易阅读,因为我们确信 not 适用于整个表达式,而 if表单中,我们必须在视觉上匹配括号,这在如此复杂的表达式中可能很困难,尽管编辑器中的语法突出显示对此有所帮助。
Different syntactic variations can be more convenient and easy to read at different times. It is a useful tool for the developer to be able to choose the most readable and obvious syntax for a particular task.
This can be shown for your
unless
example:With a simple boolean expression there is no real advantage of using unless or if, although there may be a personal preference depending on how good a particular developer thinks it looks:
These are equally easy to read (and write).
However, with more complex boolean expressions, the
if
form has disadvantages:Here, the unless form is a bit easier to read because we know for sure that the not applies to the whole expression, whereas with the if form, we must visually match the brackets, which can be hard in such complex expressions, although syntax highlighting in editors helps this somewhat.
一般来说,为开发人员提供实现同一目标的多种方法是很正常的事情,当然,选择一种方法而不是另一种方法可能是开发人员的某种偏好。但您必须知道,当智能编译器将代码转换为其二进制执行格式时,它会将所有方法转换为相同的结果。
In general, it is something normal to provide developers with many ways to achieve the same thing, and of course it would be some kind of developers preference to choose a way over another. But you have to know that a smart compiler would convert all the ways to the same result when it converts code to its binary executive format.