为什么 ColdFusion 的设计者决定从 1 而不是从 0 开始索引数组?

发布于 2024-07-11 03:40:18 字数 51 浏览 5 评论 0原文

我只是很好奇这一点,有谁知道他们为什么打破惯例?

谢谢, 夏兰

I'm just curious about this really, does anyone know why they broke convention on this one?

Thanks,
Ciaran

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

贱贱哒 2024-07-18 03:40:18

@Cory:如果你知道谁潜伏在 StackOverflow 上,你会感到惊讶。 :-)

你说得非常对。 CFML 的最初设计是为了允许非程序员构建复杂的 Web 应用程序。 ColdFusion\CFML 是第一种专门为构建 Web 应用程序而设计的语言。 早在 1995 年,网络主要是静态 HTML,典型的“网络开发人员”并没有进行太多编程。 该语言本身被设计得尽可能简单,这就是为什么它仍然是最快/最容易学习的语言之一。

它可能会导致一些混乱,尤其是当 ColdFusion 代码直接与 Java 或 .NET 交互时。 然而,它只是成为那些“怪癖”之一。 早在 2000/2001 年,当 CF 被重建为 Java EE 应用程序时,就重新考虑了这一决定,但向后兼容性阻止了这一改变。

@Cory: You'd be surprised to know who lurking on StackOverflow. :-)

You are very much right. The original design of CFML was to allow non-programmers to build complex web applications. ColdFusion\CFML was the first language designed specifically for building web applications. Back in 1995 the web was mostly static HTML and your typical 'web developer' wasn't doing too much programming. The language itself was designed to be as simple as possible which is why it's still one of the fastest/easiest languages to learn.

It can lead to a bit of confusion, especially when ColdFusion code interacts directly with Java or .NET. However, it's just become one of those 'quirks'. The decision was revisited back in 2000/2001 when CF was rebuilt as a Java EE application, but backward compatibility prevented the change.

蓝海 2024-07-18 03:40:18

有两种约定,一种是大多数编程语言通用的,另一种是大多数非程序员通用的。 他们可能是针对那些不从0开始数的人。

There's two conventions, the one common to most programming languages, and the one common to most nonprogrammers. They were probably aiming at the people who don't start counting with 0.

月野兔 2024-07-18 03:40:18

如果我不得不猜测,那是因为 ColdFusion 的目的是吸引新手,而基于 1 的数组可能更有意义 - 第一项是数字 1,第二项是数字 2 等等。

奇怪的是我们计算机科学家!

If I had to guess, it's because ColdFusion was aimed to appeal to the novice, and 1-based arrays might make more sense - the first item is number 1, second is number 2 etc.

It's us computer scientists who are weird!

初熏 2024-07-18 03:40:18

好吧,除非我们有任何原始设计师,否则除了推测之外我们很难做任何事情。 但前世用过CF,我有一些想法。

如果您查看原始语言,您会发现它是为 RAD 类型开发而设计的,适合那些想要构建动态应用程序而不需要太多复杂性的人。 我仍然记得当他们最终发布用户定义函数时的喜悦,这样我就不必到处使用标签了。

基于此,那么人们必须处理的语言的某些方面(例如数组)就会变得更加“友好”,这是有道理的。 对我来说,看到 array[0] 是很有意义的。 但对于刚接触该范式、尚未了解这一点的人来说,这没有任何意义。 为什么我要访问位置“0”的对象?

有趣的是,现在 CF 在后端是 Java,你实际上必须处理索引从 1 开始的情况,以及索引从 0 开始的情况。因此,通过尝试提供帮助,他们实际上增加了更多的复杂性,如下所示语言已经发展。

Well, unless we have any of the original designers, it's going to be tough to do anything but speculate. But having used CF in a previous life, I have some ideas.

If you look at the original language, it was designed for RAD type development for people who wanted to build dynamic applications without a lot of complexity. I still remember the joy when they finally released User-Defined Functions so I didn't have to use tags everywhere.

Based on that, then it would make sense that aspects of the language people had to deal with - such as arrays - they would make more "friendly". To me, seeing array[0] makes perfect sense. But to people new to the paradigm who haven't learned that, it wouldn't make any sense. Why would I access an object at position "0"?

The funny thing is that now that CF is Java in the backend, you actually have to deal with cases where your index starts at 1, and cases where it starts at 0. So by trying to be helpful, they actually added in more complexity as the language has grown.

以往的大感动 2024-07-18 03:40:18

作为一个不同的解释,让我们问为什么在某些语言中数组索引从零开始? 对于计算离散对象(如数组元素),这没有什么意义,从人类的角度来看也是不自然的。

这最初似乎源于像 C 这样的语言(尽管我并不是说它首先出现在 C 中:我不知道,而且这对于本文的目的来说并不重要),其中语言及其编程相当接近与内存管理(malloc 等)耦合。 一些 C 语言概念与内存中的内容非常接近。 变量就是一个例子:除了变量名之外,我们总是忙于处理变量所在的内存地址(或开始)以及指针等。

所以我们来看看 C 中的数组,它们的索引方式是内存中驻留一系列元素,从数组变量的基本内存位置开始,每个元素都按数据类型的大小进行偏移(例如:一个字符是一个字节,等等)。 因此,为了在内存中找到数组中的每个元素,我们这样做:

arrayBaseAddress + (whichElementItIsInTheArray * sizeOfDataType)

在用 C 语言做事情时,人们确实会发现自己确实是这样思考的,因为它与计算机在幕后寻找值所必须执行的操作相当紧密地映射代码想要。

因此,使用whichElementItIsInTheArray来偏移内存地址(以sizeOfDataType为单位)。

显然,如果数组索引从 1 开始,它将在内存中偏移一个 sizeOfDataType,无论出于何种目的,都会在 之间浪费 sizeOfDataType 内存量。 arrayBaseAddress 以及第一个元素实际所在的位置。

人们可能会认为这并不重要,但在以前,当这一切都在实施时,内存就像黄金一样:它不能像这样浪费。 因此,人们可能会想“好吧,只需在后台将 whichElementItIsInTheArray 偏移 -1 即可,然后就可以完成它了。但是,就像内存一样,时钟周期是黄金,所以我们的想法不是浪费处理,而是程序员只需要习惯一种不自然的计数方式,

因此在这些情况下从索引零开始数组是有正当理由的

(现在这已经进入编辑倾向了) 。 )当随后的“大括号”语言出现(如 Java)时,无论它是否真的相关,它们都只是效仿,因为“这就是它的完成方式”,而不是“这种方式有意义”

。更现代的语言,以及远离计算机内部运作的语言,有人停下来思考“我们为什么要这样做?”,以及“在这种语言及其预期用途的背景下,这有意义吗?”。同意这里,答案是 - 坚决 - “不”。在很多情况下,将数组索引偏移 -1 或简单地忽略第零个元素的内存所造成的资源浪费不再是相关的考虑因素。 那么,为什么要让语言和程序员必须将他们自然计数的方式抵消一,纯粹是出于遗留原因呢? 没有正当理由这样做。

在 C 语言中,数组 a[0] 中有一个元素。 这是数组的第一个元素(不是“第零个”元素),如果这是数组的完整范围,则它的长度为一个。 因此,这里的特殊行为是编程语言的一部分,而不是“现实生活中”(这是我们大多数人居住的地方)的事物计数/枚举方式的一部分。 那么为什么还要坚持呢?

这里的一些人反驳了这种“从哪里开始索引”的论点,“当我们出生时,我们不是一,我们是零”。 这是事实,但那是在测量连续的事物,并且是不一样的。 所以与对话无关。 数组是离散项的集合,当测量离散项的数量(即:对它们进行计数)时,我们从一个开始。

这如何增加对话? 嗯,没什么,但这是看待同一件事的不同方式。 我想这有点合理化/反应一些人认为从 1 开始数组索引在某种程度上是“错误的”。 这并没有错,从人的角度来看,这比从零开始更正确。 因此,让人类像人类一样编写代码,并让机器根据需要理解它。 基本上,只有针对遗留技术限制,我们才开始从零开始计数,如果我们不再需要,就没有必要延续这种做法。

当然,都是“IMO”。

As a different spin on it, let's ask why in some languages the array index starts at zero? For counting discrete objects (like array elements), this makes little sense and is not natural from a human perspective.

This originally seemed to stem from languages like C (although I'm not suggesting it first arose in C: I don't know, and it doesn't matter for the purposes of this) in which the language and its programming is rather closely coupled to memory management (malloc, etc). Some C language conceits map rather closely to what's going in in memory under the hood. Variables are an example of this: as well as variable names, we're always busying ourselves with the memory address the variable is at (or starts at) with pointers and the like.

So we come to arrays in C, and those are indexed in such a way that there's a series of elements which reside in memory, starting at the base memory location of the array variable, and each element is offset by the size of the data type (eg: a char is one byte, etc). So to find each element in the array in memory, we do this:

arrayBaseAddress + (whichElementItIsInTheArray * sizeOfDataType)

And one really does actually find oneself thinking like this when doing stuff in C, because it maps rather closely to what the computer has to do under the hood to find the value the code wants.

So the whichElementItIsInTheArray is used to offset the memory address (in units of sizeOfDataType).

Obviously if one starts the array index at 1, it would be offset in memory by one sizeOfDataType, for all intents and purposes wasting a sizeOfDataType amount of memory between the arrayBaseAddress and where the first element actually resides.

One might think that this hardly matters, but in days of yore when all this was being implemented, memory was like gold: it could not be wasted like that. So one might think "OK, well just offset whichElementItIsInTheArray by -1 under the hood, and be done with it. However like memory, clock cycles were gold, so instead of wasting processing, the idea was the programmer would just need to get used to an unnatural way of counting.

So there was a legitimate reason to start arrays at index zero in these situations.

It seems to me (and this is getting into editorial slant now) when subsequent "curly braces" languages came out (like Java) they simply followed suit whether it was really relevant still or not, because "that's the way it's done". Rather than "that way makes sense".

On the other hand, more modern languages, and ones further removed from the inner workings of the computer, someone stopped to think "why are we doing this?", and "in the context of this language and its intended uses, does this make sense?". I agree here than the answer is - firmly - "no". The resource wastage to offset the array index by -1, or simply just ignore the zeroth element's memory is no longer a relevant consideration in a lot of circumstances. So why make the language and the programmer have to offset the way they naturally count things by one, for a purely legacy reason? There is no legitimate reason to do so.

In C, there is an element of an array a[0]. This is the first element of the array (not the "zeroth" element), and if that's the full extent of the array, it's length is one. So the idiosyncratic behaviour here is on the part of the programming language, not on the part of the way things are counted / enumerated "in real life" (which is where most of us reside). So why persist with it?

Some people here have countered this "where to start the index" argument with "well when we're born, we're not ONE, we're ZERO". This is true, but that's measuring a continuous thing, and is not the same. So is irrelevant to the conversation. An array is a collection of discrete items, and when measuring the quantity of discrete items (ie: counting them), we start at one.

How this adds to the conversation? Well it doesn't much, but it's a different way of looking at the same thing. And I suppose it's a bit of a rationalisation / reaction to this notion some people have that starting array indexes at 1 is somehow "wrong". It's not wrong, from a human perspective it's more right than starting them at zero. So let the human write the code like a human would, and get the machine to make sense of it as needs must. Basically it's only for legacy technological limitations that we ever started counting them from zero in the first place, and there's no need to perpetuate that practice if we no longer need to.

All "IMO", of course.

め可乐爱微笑 2024-07-18 03:40:18

数一下你一只手有多少根手指。 你是从0开始数还是从1开始数呢?

与现实生活密切相关的抽象思想总是更容易理解和应用(例如:考虑物理“堆栈”,然后考虑抽象数据结构)。

Count the number of fingers you have on one hand. Did you start counting with 0 or with 1?

Abstract ideas that closely parallel real life are always more easily understood and applied (ex: think of a physical "stack" and then of the abstract data structure).

孤独陪着我 2024-07-18 03:40:18

从 0 开始数组的概念在 C 语言中流行起来。 FORTRAN 和 COBOL 等较旧的语言开始将数组计数为 1(实际上在 COBOL 中称为表)。

The notion of starting arrays at 0 was popularized with the C language. Older languages such as FORTRAN and COBOL started counting arrays at 1 (actually called Tables in COBOL).

我恋#小黄人 2024-07-18 03:40:18

数组的起始点没有约定。 例如,大多数基础知识也从 1 开始。 有些语言允许您在任意位置启动数组,或者允许数组索引为枚举等(例如 Ada)。 C 在 0 处使用了 sting 的概念,许多语言都遵循了这一概念,但并非所有语言都这样做。 他们不这样做的原因之一是从 1 开始的数组更加直观。

There is no convention on the starting point of an array. Most Basics start at 1 also for example. Some languages let you start the array wherever you like, or allowarray indexes to be enumerations etc (Ada for example). C used the notion of sting at 0 and many languages have followed, but not all do. One reason why they don't is that arrays starting at 1 is far more intuitive.

少跟Wǒ拽 2024-07-18 03:40:18

即使在 Java API 的编程世界中,也有一个有趣的例外,即从 0 开始的计数:JDBC API。 它从 1 开始计数,这让每个第一次访问数据库的程序员都大吃一惊。

Even within the programming world of Java APIs there is an interesting exception the 0-based counting: The JDBC API. It starts counting with 1, much to the surprise of every programmer doing her first database access.

殤城〤 2024-07-18 03:40:18

也许这不仅仅是外行人的事情……我认为大多数接触任何网络语言的人至少都玩过 javascript(10.15 年前也是如此)。 基于 0 的索引并不是那么陌生。

我喜欢从 1 开始索引/位置(对于字符串)的语言的一点是,您可以执行诸如

<cfif find("c","cat")>

如果找到 c 则计算结果为 true 之类的事情,而且它也会如此。

而基于 0 的语言(如 javascript)

if ("cat".indexOf("c")) { 

的计算结果为 false,因此您需要说类似 if ("cat".indexOf("c") >= 0) {

然而,语言之间的翻译是一个不能忘记的小麻烦,因为忘记这样做,或者忘记填充数组可能会导致数据翻译失败,并且在两种样式之间切换可能会导致挫败感。

我认为,如果 Allaire 知道网络最终会在哪里以及客户端和服务器如何真正协同工作,我们就会有基于 0 的索引。

Maybe it wasn't just a layman's thing... I think that most people approaching any web-language have at least fiddled with javascript (and the same was true 10,15 years ago). 0-based indexes weren't quite so foreign.

The thing that I love about languages that start indexes/positions (for strings) at 1 is that you can do things like

<cfif find("c","cat")>

which evaluates to true if c is found, and it will be.

whereas a 0-based language like javascript

if ("cat".indexOf("c")) { 

evaluates to false, so you need to say something like if ("cat".indexOf("c") >= 0) {

However, translating between languages is a minor nuisance that mustn't be forgotten, because forgetting to do so, or forgetting to pad your arrays can lead to a failure in data translate, and switching between the two styles can lead to frustration.

I think, had Allaire known where the web would be eventually and how client and server can really work together, we'd have 0-based indexes.

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