区分大小写与不区分大小写的语法

发布于 2024-07-20 05:54:49 字数 891 浏览 6 评论 0原文

任何人都可以为区分大小写提供一个很好的案例(:-))吗?

C#:区分大小写 VB.NET:区分大小写 C++:区分大小写 ...

更糟糕的是:在像 VB.NET 这样的语言中使用的 XML 区分大小写。

我认为这是荒谬的,并且只有在我们发现系统中存在错误之后才会造成伤害,因为 XML 同时具有 Value 和 value 节点...

评论中一遍又一遍地问我

“也许你可以想出一个 为什么不区分大小写的争论 在这样的世界里,正确的选择是什么?”

这是一个例子: 我认为它类似于以下问题:URL 应该区分大小写? www.cnn.com>> www.cNN.com? 当然应该是一样的,盗号天堂! 因为人类不会过多关注两个相同但大小写可能不同的字符串。 程序员也是人。 所以 getAge() 和 getage() 在大多数人的心目中是相同的。


请注意:我不认为我们希望代码实际上有一个定义为 getAget() 的函数,然后让代码调用它 getage(),VS (vb.net) 会自动将 getaget 更正为 getAge。 因此代码很清晰,程序员也知道正确的大小写。 我的观点是:好的 IDE 使问题变得无关紧要,但它在像 vb.net 这样的非区分大小写的语言中工作得更好,然后让我们说 c#。 参考:此处

Can anyone make really a good case ( :-) ) for being case sensitive?

C#: case sensitive
VB.NET: not case sensitive
C++: case sensitive
...

Worse part: XML which is used inside a language like VB.NET is case sensitive.

I was making the case that it is ridiculous and can only cause harm after we found a bug in our system due to the fact that XML had both Value and value nodes...

I am asked over and over in comments

"Perhaps you can come up with a single
argument for why case insensitive is
the right choice in such a world?"

Here is an example:
I see it analogous to the issue of: URL's should be case sensitive?
www.cnn.com <> Www.cNN.com ?
Of course they should be the same, ID theft heaven! because humans don't put that much attention to 2 strings that are the same but might have otherwise different casing. Programmers are humans. So getAge() and getage() are the same in most human's minds.


Please notice: I do not think we want the code to actually have a function defined as getAget() and then have code calling it getage(), VS (vb.net) will automatically correct getaget to getAge. So the code is clear and the programmer is aware of the correct capitalization. My point is: good IDE makes the issue non relevant, but it works better in a non case-sesnsetive language like vb.net then lets say c#.
Reference: here

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

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

发布评论

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

评论(7

遥远的绿洲 2024-07-27 05:54:49

案例规则取决于文化。 您想要一种编程语言,其中变量i有时被认为与名为I的变量相同并且有时它们是不同的变量? (顺便说一句,这不是一个虚构的例子。在土耳其语中,I不是大写的 i

老实说,这非常简单。当你犯错时,你希望编译器纠正你,还是希望它猜测你的意思?后者会导致错误,正如你发现的那样“哦,你的意思可能是一样的,就是这样。”好吧,我们不会阻止你”,XML 从字面上理解了你。

你的错误不是因为大小写敏感度不好而发生的,而是因为马虎是不好的。任意改变大小写最多可能不会导致任何问题,并且在最坏的情况会导致错误。顺便说一句,无论您的工具是否区分大小写,程序员都必须与您的情况保持一致。 em>应该区分大小写。只要世界上有不区分大小写的工具和敏感的工具,如果我们可以重塑世界,那么一切都是如此。不区分大小写,很多支持敏感的理由就会消失。 但我们不能。

当然还有一点补充:
在许多语言中,通常给变量和类型赋予相同的名称,但大小写不同:

Foo foo; // declare a variable foo of type Foo

当然你可能会说“你不应该这样做”,但它很方便,它立即告诉读者变量的类型有。 它允许我们创建一个 Log 类和一个日志对象。 由于该对象的目的是记录日志,因此名称很明显。

最后要考虑的一点是:

在实际语言中大小写很重要。 以大写开头的单词与相同但以小写开头的单词不同。 “wordD”这个词不是正确的英语。 信息被编码在外壳中,这使得文本更易于阅读。 例如,它告诉我们何时遇到一个名字,或者何时开始一个句子,这很方便。 允许人们忽略大小写规则会使文本更难阅读。 既然代码通常应该写得尽可能可读,为什么我们在编程中不应该这样做呢? 让外壳编码重要信息。 在许多语言中,Foo 是一种类型,foo 是一个变量。 这是重要的信息。 我想在编程时知道这一点。 如果我看到一个名为“Getage”的函数,我想知道这是否是我以前从未听说过的英语单词。 但当我看到“GetAge”时,我立即知道它应该读作“Get”一词后面跟着“Age”一词。

顺便说一句,这里是一个让您惊喜的有趣示例可能会遇到区分大小写的语言。

Case rules depend on culture. Do you want a programming language where a variable i is sometimes considered to be the same as one called I and sometimes they're different variables? (That's not a made-up example, btw. In Turkish, I is not an upper-case i.

Honestly, it's pretty simple. Do you want the compiler to correct you when you make a typo, or do you want it to guess at what you meant? The latter leads to bugs, as you found out. VB assumes "oh, you probably meant the same thing, that's ok, we won't stop you", and XML took you literally.

Your bug didn't occur because case sensitivity is bad, it occurred because being sloppy is bad. Arbitrarily changing case may, at best, cause no problems, and at worst it will cause errors. Assume the worst, and be consistent with your case. Which, incidentally, is what case sensitive languages force you to do. Whether or not your tools are case sensitive, the programmer should be case sensitive. Being case sensitive saves you a lot of trouble as long as the world features insensitive as well as sensitive tools. If we could remake the world so that everything was case insensitive, a lot of the reasons in favor of sensitivity would go away. but we can't.

A little side note of course:
In many languages, it is common to give variables and types the same names, but with different capitalization:

Foo foo; // declare a variable foo of type Foo

Of course you could argue that "you shouldn't do that", but it's convenient, and it immediately tells the reader what type the variable has. It allows us to create a Log class, and a log object. And since the purpose of this object is to log, the name is kinda obvious.

And a final point to consider:

Case matters in real languages. A word that begins with upper-case is different from the same one but with leading lower-case. The word "worD" is not correct english. Information is encoded in the case, which makes text easier to read. It tells us when we encounter a name, for example, or when a sentence begins, which is handy. Allowing people to ignore case rules makes text harder to read. And since code should generally be written as readable as possible, why shouldn't we do the same in programming? Allow the case to encode important information. In many languages, Foo is a type, and foo is a variable. That's important information. I want to know this when I program. If I see a function called "Getage", I wonder if that's some English word I've never heard before. But when I see "GetAge", I immediately know that it should be read as the word "Get" followed by the word "Age".

By the way, here's a nice example of the fun surprises you can run into in case sensitive languages.

掌心的温暖 2024-07-27 05:54:49

在编程语言中,Slop 从来都不是一个好主意。 您希望事情尽可能具体。 你永远不希望你的语言猜测任何事情,它应该允许尽可能少的方法来解决给定的问题。

至于具体的答案,可读性怎么样? 是不是 stoRetroData 在视觉上与 storeTRodAtA 有很大不同? 并不是说任何人都会做这样的事情,但是允许它有什么意义呢?

我想不出任何理由允许忽略大小写。

至少这是我的观点——但你的情况可能会有所不同。

编辑:我可能应该从免责声明开始:

我学会了基础编程,大约 18 年前短暂地有过同样的想法。 相信我,这是你 20 年后回顾时会说“哦,是的,我当时错得很”的事情之一(就像我现在一样)

Slop is never a good idea in a programming language. You want things to be as specific as possible. You never want your language to guess at anything and it should allow as few ways to solve a given problem as possible.

As for a specific answer, how about readability? Doesn't stoRetroData visually differ quite a bit from storeTRodAtA? Not that anyone would do such a thing, but what's the point in allowing it?

I can't come up with any reason to allow ignoring case.

At least that's my opinion--but your mileage may vary.

Edit: I probably should have started this out with a disclaimer:

I learned to program in basic and had this same thought fleetingly about 18 years ago. Trust me, it's one of those things you'll look back on in 20 years and go "Oh, yeah, I was pretty wrong about that" (as I am right now)

生生漫 2024-07-27 05:54:49
  1. 历史 这就是它的做法。 VB.NET 的 XML 区分大小写,因为 XML 标准要求它
  2. 国际化 我们是否要支持所有语言(法语、日语、希伯来语、克林贡语等)的大小写?
  1. History It is the way it has been done. The XML is VB.NET is case sensitive because the XML standard requires it
  2. Internationalization Are we going to support case in all languages (French, Japanese, Hebrew, Klingon, etc.)?
一向肩并 2024-07-27 05:54:49

现在有几种区分大小写的语言就是这样,因为它们所基于的语言是区分大小写的,并且转换会更容易。 就我个人而言,我更喜欢区分大小写,但 Jeff Atwood 写了一篇非常好的文章来解释原因可能不再需要区分大小写。

Several case sensitive languages nowadays are that way because the languages they were based on were case sensitive and the transition would be easier. Personally I prefer case sensitive, but Jeff Atwood wrote a pretty good article on why case sensitivity may no longer be necessary.

明明#如月 2024-07-27 05:54:49

有几个原因。

  • 要查找不区分大小写的东西,我必须在所有地方都有“不区分大小写”标志。 使用UTF-8,还应该知道克林贡语小写。

  • 更重要的是,CamelCasing、CAMelcaSing。 它并不漂亮,但它被使用了很多并且相当理智。 不区分大小写几乎是不可能的。

  • 语言奇偶校验,例如 xsd.exe(随 VS200x 一起提供)可以为您提供的 xsd 生成类。 当你也有“价值”时,你的“价值”会被命名为什么? 因此,这消除了另一个可能的阻抗。

Couple of reasons.

  • Finding things, with case-insensitive I'd have to have 'case-insensitive' flag about everywhere. With UTF-8 that should be also aware of Klingon smallcase..

  • More importaintly, CamelCasing, CAMelcaSing. It's not pretty, but it's used a lot and is fairly sane. Is nigh impossible with case-insensitivity.

  • Language parity, for example xsd.exe (shipped with VS200x) can generate you classes for xsd that you supply. What would be your "Value" named when you also have "value"? So this takes out yet another possible impedance.

一曲爱恨情仇 2024-07-27 05:54:49

我发现不区分大小写很愚蠢。 您应该遵循原始声明的大小写。 除了懒得输入 TheRealName 而不是 therealname 之外,我看不出有什么好的理由不这样做。

事实上,我永远不会考虑使用不区分大小写的语言。

I find case-insensitivity to be just silly. You should follow the capitalization of the original declaration. I can't see any good reasons for not doing so beside being too lazy to type TheRealName instead of therealname.

In fact, I would never even consider using a case-insensitive language.

怂人 2024-07-27 05:54:49

大小写在编程语言中很好,但我们不应该在符号名称中使用它,而应该按照最初的意图使用它 - 分隔句子或命令或专有名称的开头。 例如:

Var test = 0;
Console.writeline(test);
Test = test + 1;
Console.writeline(test);

真漂亮,...:P

Case is good in programming languages, but rather than use it in symbol names we should use it as it was originally intended- to delimit the beginning of a sentence or command or a proper name. For example:

Var test = 0;
Console.writeline(test);
Test = test + 1;
Console.writeline(test);

So beautiful,... :P

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