简体   繁体   English

句法糖与特征

[英]Syntactic sugar vs. feature

In C# (and Java) a string is little more than a char array with a stored length and a few methods tacked on. 在C#(和Java)中,字符串只是一个具有存储长度的char数组,并且添加了一些方法。 Likewise, (reference vs. value stuff aside) objects are little more than glorified structs with inheritance and interfaces added. 同样地,(参考与值之外的东西除外)对象只不过是带有继承和接口的美化结构。

On one level, these additions feel like clear features and enhancements unto themselves. 在一个层面上,这些新增功能感觉就像是清晰的功能和增强功能。 On another level, they feel like a marginal upgrade from the status of "syntactic sugar." 在另一个层面上,他们觉得从“语法糖”状态的边缘升级。

To take this idea further, consider (I may have some details wrong, but the point remains): 为了进一步考虑这个想法,请考虑(我可能有一些细节错误,但重点仍然是):

transistor
elementary logic gate
compound gate
  |         |
 ALU    flip-flop
   |    |       |
   | register  RAM
   | |
   CPU
   microcode
   assembly
   C
   C++
   | |
MSIL JavaScript
C#   jQuery

Many times, any single layer of abstraction looks a lot like syntactic sugar but multiple layers of separation feel very removed from each other. 很多时候,任何单一的抽象层看起来都像语法糖,但是多层分离感觉彼此之间的距离非常小。

How do you know when something has stopped being syntactic sugar and started being a bona fide feature? 你怎么知道什么东西停止了语法糖并开始成为一个真正的功能?

It turns out to be a feature instead of syntactic sugar when it implies a different way of thinking. 当它意味着不同的思维方式时,它被证明是一种特征而不是语法糖。

You are right when you say objects are in fact glorified structs with methods and inheritance. 当你说对象实际上是带有方法和继承的美化结构时,你是对的。 That, however, is just the implementation detail. 然而,这只是实施细节。 What objects allow is to think in a different way. 对象允许以不同的方式思考。 You can relate more easily to real world entities when thinking about objects. 在考虑对象时,您可以更轻松地与现实世界实体联系。 The same thing happened when even further back in time, we jumped from using go-to's to procedural programming. 同样的事情发生在更进一步的时候,我们从使用go-to跳到程序编程。 Under the hood, the processor still keeps on jmp'ing from OP to OP, but we could think in a different, more black-box, way. 在引擎盖下,处理器仍然保持从OP到OP的jmp'ing,但我们可以用不同的,更黑盒子的方式思考。

Having said that, in extreme, you can say everything is syntactic sugar, but some of that sugar is a feature when it allows you to think differently. 话虽如此,在极端情况下,你可以说一切都是语法糖,但是当它允许你以不同的方式思考时,一些糖是一个特征。

句法糖一个特征。

All of software is a giant stack of abstractions built on top of other abstractions. 所有软件都是基于其他抽象构建的大量抽象。 A string may be nothing more than an array of characters, but there are many operations that feel natural on strings, but awkward on character arrays. 字符串可能只是一个字符数组,但有许多操作在字符串上感觉很自然,但在字符数组上却很笨拙。 The goal of all of these abstractions is the same: remove irrelevant details so that the developer can focus on the important parts of the problem. 所有这些抽象的目标都是相同的:删除不相关的细节,以便开发人员可以专注于问题的重要部分。

As you point out, all modern programming languages could be eliminated, and we could go back to working in assembly language. 正如您所指出的,所有现代编程语言都可以被淘汰,我们可以回到使用汇编语言。 But our productivity would plummet. 但是我们的生产力会直线下降。

I guess people call something syntactic sugar when they feel they get little benefit from it, and a feature when they feel the get a large benefit from it. 我猜人们在感觉到它们从中获得的好处时会称之为语法糖,并且当他们觉得从中获得很大的好处时就会有这个特征。 That makes the distinction very fuzzy, and quite subjective. 这使得区别非常模糊,而且非常主观。

When the change provides value? 当变化提供价值? I have coded in assembler. 我用汇编语言编写了代码。 I switched to C and looked at the output from the compiler. 我切换到C并查看编译器的输出。 It's code was 95+% as good as my hand crafted assembler and it was much easier to write. 它的代码是95 +%和我手工制作的汇编程序一样好,而且编写起来要容易得多。 For me that provided value so I'd say it wasn't sugar. 对我来说,它提供了价值所以我说它不是糖。

C++ helps me translate my object oriented thoughts into code. C ++帮助我将面向对象的思想转化为代码。 As long as the overhead isn't terribly high then I think it's a feature. 只要开销不是很高,我认为这是一个功能。

I'm a practical sort. 我是一个实用的人。 "If I can see it's valuable" is my answer “如果我能看到它有价值”是我的答案

It seems that syntactical surgar is a syntax that changes nothing about the abilities of the language, and using a different construct accomplishes exactly the same thing. 似乎语法surgar是一种语言,它不会改变语言的能力,使用不同的构造完成完全相同的事情。 A String (thinking in Java) is not just syntatical sugar over a char array. String(以Java思考)不仅仅是char数组的合成糖。 A char array is mutable (in content if not in length). char数组是可变的(如果不是长度,则在内容中)。 You could not make a char array immutable with an existing language feature without a String array. 如果没有String数组,则无法使用现有语言功能使char数组不可变。

On the other hand, the plus operator working on Strings is indeed syntatical sugar for using a StringBuilder and calling append. 另一方面,处理字符串的加号运算符确实是使用StringBuilder并调用append的合成糖。

I would have to say when the same result is cannot be achieved simply by writing different code, with the same type of "time-constraint" as using the syntactical sugar. 我不得不说,只是通过编写不同的代码,使用与使用语法糖相同类型的“时间约束”,无法实现相同的结果。

My Example would be a Lambda expression, writing a foreach loop doesn't take a lot of effort, but using .Foreach() sure is nice too; 我的示例将是一个Lambda表达式,编写一个foreach循环不需要花费很多精力,但使用.Foreach()肯定也很好; versus rewriting the whole HttpRequest class on your own. 而不是自己重写整个HttpRequest类。 One is syntactical, one is a feature. 一个是语法,一个是特征。 Both save time, one in a much bigger way than the other. 两者都节省了时间,一个比另一个更大。

“Syntactic sugar”是你不喜欢的功能

Generally the term "syntactic sugar" refers to language features which never allowed a programmer to do something which could not be done before, but rather provided a nice means of expressing something that could already expressed in the language , even if somewhat more awkwardly. 通常,术语“语法糖”是指语言特征,它从不允许程序员做某些以前无法完成的事情,而是提供了表达已经用语言表达的东西的好方法,即使有些更笨拙。

Certain constructs may be unambiguously regarded as syntactic sugar. 某些构建体可以明确地视为语法糖。 For example, in VB.NET, code to test for whether two references weren't equal used to require If Not (ref1 Is Ref2) but newer versions of the language allow If ref1 IsNot Ref2 . 例如,在VB.NET中,用于测试两个引用是否相等的代码用于要求If Not (ref1 Is Ref2)但该语言的较新版本允许If ref1 IsNot Ref2 Nothing can be expressed in the new syntax that couldn't be expressed in the old, but the new syntax is cleaner, introduces no ambiguities, and the only reason not to use it would be if code had to be back-compatible with old versions of the language. 新语法中没有任何东西可以用旧语法表达,但新语法更清晰,没有引起歧义,并且不使用它的唯一原因是代码必须与旧版本反向兼容的语言。

Some constructs may be a bit harder to define as sugar. 一些结构可能有点难以定义为糖。 In particular, if a language adds constructs which will work identically to existing construct when used with other types, but will fail compilation with others, such constructs may provide a means of compile-time type verification which did not exist previously. 特别是,如果一种语言添加的构造在与其他类型一起使用时将与现有构造完全相同,但是与其他类型的编译失败,这种构造可能提供以前不存在的编译时类型验证的方法。 Java generics may generally be viewed in this light. 通常可以从这个角度来看待Java泛型。 One can add a Cat to an ArrayList<Cat> just as easily as to an ArrayList ; 可以像对ArrayList一样轻松地将Cat添加到ArrayList<Cat> ; what the ArrayList<Cat> adds is a guard to reject Dog s at compile time. ArrayList<Cat>添加的内容是在编译时拒绝Dog的守卫。 Since compile-time constraints don't allow one to write any program that couldn't be written without them, some people may view them as syntactic sugar. 由于编译时约束不允许编写任何没有它们而无法编写的程序,因此有些人可能会将它们视为语法糖。 On the other hand, even though type verification is performed at compile-time rather than run-time, it might still be viewed as one of the jobs of a program. 另一方面,即使在编译时而不是在运行时执行类型验证,它仍可能被视为程序的一个作业。

“懒惰是进步的动力”当你可以立即将你的想法映射到机器上时,这个过程就会停止。

Syntactic sugar and language feature are basically describing the same thing, even if syntactic sugar is sometimes used in a pejorative way whereas feature is often associated with deeper changes in the language architecture (introducing lambdas etc.). 句法糖和语言特征基本上描述了同样的事情,即使句法糖有时以贬义的方式使用,而特征通常与语言架构的更深层次的变化相关(引入lambda等)。 But this distinction is very dependent on a individual point of view (and its subjectively felt usefulness). 但这种区别在很大程度上取决于个人的观点(以及它主观上的有用性)。

Regarding language-design aspects and your example with strings and char-arrays , I would say that this should be neither a feature nor sugar, but simply expressible in the languages basic syntax ( LOP - language-oriented programming). 关于语言设计方面以及带有stringschar-arrays示例,我想说这既不是功能也不是糖,而只是在语言基本语法( LOP - 面向语言编程)中可以表达。 Generic concepts (typeclasses, metaprogramming etc.) allow you to express many new and useful constructs by yourself without waiting for the language to get a new feature. 通用概念(类型类,元编程等)允许您自己表达许多新的和有用的结构,而无需等待语言获得新功能。 Just look at Haskell or C++'s metaprogramming capabilities. 看看Haskell或C ++的元编程功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM