语法糖vs.功能 [英] Syntactic sugar vs. feature

查看:72
本文介绍了语法糖vs.功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#(和Java)中,字符串只不过是具有存储长度和附加一些方法的char数组.同样,(除了引用与值之类的东西)对象不过是带有继承和接口的美化结构.

In C# (and Java) a string is little more than a char array with a stored length and a few methods tacked on. 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.

当您说对象实际上是带有方法和继承的美化结构时,您是对的.但是,这仅仅是实现细节.对象允许以不同的方式思考.考虑对象时,您可以更轻松地与现实世界实体关联.当我们回到过去的时候,发生了同样的事情,我们从使用定位语言跳到了过程编程.在引擎盖下,处理器仍在不断地从OP过渡到OP,但是我们可以用另一种更黑盒的方式来思考.

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. 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.

话虽这么说,但在极端情况下,您可以说所有东西都是语法糖,但是其中的一些糖是您可以从不同角度思考的一种功能.

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.

这篇关于语法糖vs.功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆