程序语言区分大小写有什么好处?
可能的重复:
区分大小写有什么好处吗编程语言?
我的第一次编程经历是使用 Basic 系列(MSX Basix、Q-basic、VB)。 这些都不区分大小写。现在,可能是因为这些第一次经历,但我从未领会语言区分大小写的好处。相反,我认为它是不必要的开销和错误的来源,当我使用 Java 或 C 时,它仍然让我烦恼。
现在,我刚刚阅读了 Clojure(一种 Lisp 方言)并注意到 - 令我惊讶的是 -与 Lisp 的区别之一是区分大小写。
那么:使用区分大小写的语言实际上有什么好处(对程序员来说)?
我能想到的唯一的事情是:
- 符号视觉反馈数量加倍
- ,并且使用复杂变量更容易阅读像 CamelCase 这样的技术,例如
HopCount
但是,第一个参数不成立,因为它是 bug 的主要来源(在一个方法中使用 hopcount 和 HopCount 是不好的做法)。
第二个论点也不成立,因为一个像样的 IDE 也可以通过其他方式提供这一点。一个很好的例子是 VBA IDE,它有一个非常好的方法:语言不区分大小写,但是一旦您键入变量,它就会将其更改为其定义中使用的大小写。例如,如果您将 Dim thisIsMyVariable as string
定义,它会将所有出现的 thisismyvariable
更改为 thisIsMyVariable
)。这为程序员提供了一个直接线索,表明变量实际上输入正确(因为它改变了外观)。
编辑:添加...给程序员带来好处 ...
Possible Duplicate:
Is there any advantage of being a case-sensitive programming language?
My first programming experiences where with the Basic family (MSX Basix, Q-basic, VB).
These are all not case-sensitive. Now, it might be because of these first experiences, but I've never grasped the benefit of a language being case sensitive. On the contrary, I think it is a source of unneeded overhead and bugs, and it still annoys me when I use e.g. Java or C.
Now, I just read on Clojure (a Lisp-dialect) and noticed - to my surprise - that one of the differences with Lisp is case-sensitivity.
So: what is actually the benefit (to the programmer) of having a case-sensitive language?
The only things I can think of are:
- double the number of symbols
- visual feedback and easier reading for complex variables using techniques like CamelCase, e.g.
HopCount
However, the first argument doesn't hold because of being a major source for bugs (bad practice to use hopcount and HopCount in one method).
The second argument doesn't hold either, as a decent IDE can provide this also in an other way. A good example is the VBA IDE, which has a very good approach: the langauge is case-insensitive but as soon as you type a variable it will change it to the case used in its definition. For example, if you defined Dim thisIsMyVariable as string
, it will change any occurrence of thisismyvariable
into thisIsMyVariable
). That provides the programmer with an immediate clue that the variable was actually typed-in correctly (because it changed appearance).
Edit: added ... benefit to the programmer ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,其中一点是视觉辅助。大多数编程语言(甚至框架)都有关于如何大写变量、名称等的约定。
此外,它强制在所有地方使用统一的名称,因此您不会对称为“var”、“Var”甚至“VaR”的相同变量感到混乱。
我不记得曾经有过与大写相关的错误,所以这一点对我来说似乎有点做作。
对我来说,使用两个同名但大小写不同的变量听起来像是有意识地搬起石头砸自己的脚。不同的大写约定几乎在任何地方都表示完全不同类型的对象(类、变量、方法等),因此由于完全不同的语义,很难犯这样的错误。
我想这样想:不区分大小写我们能得到什么?
我们引入歧义,我们鼓励草率和糟糕的风格。
当然,这是一个稍微主观的问题。
One point is, like you said, visual aid. Most programming languages (and even frameworks) have conventions on how to capitalize variables, names, etc.
Also, it enforces using uniform names everywhere, so you don't have a mess with the same variable referred to as "var", "Var" or even "VaR".
I can't remember of ever having bugs related to capitalization, so that point seems kind of contrived to me.
Using 2 variables of the same name but different capitalization to me sounds like a conscious attempt to shoot yourself in the foot. Different capitalization conventions almost everywhere signify objects of completely different type (classes, variables, methods and so on), so it's pretty hard to make such a mistake due to the completely different semantics.
I'd like to think of it in this way: what do we gain by NOT having case-sensitivity?
We introduce ambiguity, we encourage sloppiness and poor style.
This is a slightly subjective matter of course.
许多命名约定要求表示来自不同语义类(类型、函数、变量)的对象的符号具有自己的名称大小写规则。例如,在 Java 中,类型名称始终以大写字母开头,而变量、成员函数名称等以小写字母开头。这有效地将类型名称放在不同的命名空间中并给出了语句实际含义的视觉线索。
Many naming conventions demand that symbols denoting objects from different semantic classes (types, functions, variables) have their own name casing rules. In Java, for example, types names always begin with a upper case letter, while variables, member function names etc. begin with a lower case letter. This effectively puts type names in a different namespace and gives a visual clue what a statement actually means.