小驼峰式变量大小写约定(例如 thisVariable)有什么意义?
我希望这不会因为太宽泛而被关闭。我知道这取决于个人喜好,但所有大小写约定都有一个起源,我想知道这个约定来自哪里,以及人们为什么使用它的逻辑解释。
这就是你要去的地方,就像 var empName;
一样。我称其为低等骆驼,尽管从技术上讲它可能有其他名称。就我个人而言,我喜欢 var EmpName
。我称那是真正的骆驼,我喜欢它。
当我第一次开始编程时,我从较低的骆驼约定开始。我不知道为什么。我只是效仿所有老家伙们树立的榜样。变量和函数 (VB) 的驼峰度较低,而子项和属性的驼峰度则适当。然后,在我最终牢牢掌握了编程本身之后,我开始能够轻松地质疑导师的策略。对我来说,使用小骆驼没有逻辑意义,因为它不一致,特别是如果您有一个由一个单词组成的变量,而该单词最终全部为小写。也没有适当的验证机制来确保您正确使用下骆驼和上骆驼,所以我问为什么不只使用正确的骆驼来处理所有事情。它是一致的,因为所有变量名称都经过适当的驼峰化。
深入研究后发现,这对于许多程序员来说是一个非常敏感的问题。他们通常会回答:“嗯,这只是个人喜好”或“我就是这么学的”。经过进一步的刺激,当我试图找到他们使用低等骆驼背后的逻辑原因时,它通常会引起人们的一种教条反应。
那么有人想了解一下正确的骆驼品种背后的历史和逻辑吗?
I hope this doesn't get closed due to being too broad. I know it comes down to personal preference, but there is an origin to all casing conventions and I would like to know where this one came from and a logical explanation as to why people use it.
It's where you go all like var empName;
. I call that lower camel, although it's probably technically called something else. Personally, I go like var EmpName
. I call that proper camel and I like it.
When I first started programming, I began with the lower camel convention. I didn't know why. I just followed the examples set by all the old guys. Variables and functions (VB) got lower camel while subs and properties got proper camel. Then, after I finally acquired a firm grasp on programming itself, I became comfortable enough to question the tactics of my mentors. It didn't make logical sense to me to use lower camel because it wasn't consistent, especially if you have a variable that consists of one word which ends up being in all lowercase. There is also no validation mechanism in place to make sure you are appropriately using lower vs. upper camel, so I asked why not just use proper camel for everything. It's consistent since all variable names are subject to proper camelization.
Having dug deeper into it, it turns out that this is a very sensitive issue to many programmers when it is brought to question. They usually answer with, "Well, it's just personal preference" or "That's just how I learned it". Upon prodding further, it usually invokes a sort of dogmatic reaction with the person as I attempt to find a logical reason behind their use of lower camel.
So anyone want to shed a little history and logic behind casing of the proper camelatory variety?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它是两件事的组合:
当然,它是否被使用取决于制定管理正在编写的代码的编码标准的人。下划线与驼峰命名法、小写变量与大写变量。驼峰命名法 + 小写变量 = 驼峰命名法
It's a combination of two things:
Whether or not it gets used is of course up to whomever is setting the coding standards that govern the code being written. Underscores vs CamelCase, lowercasevariables vs Uppercasevariables. CamelCase + lowercasevariable = camelCase
在 C# 或 VB 等语言中,标准是以小写字母开头私有内容,以大写字母开头公共/受保护内容。这样,只需查看第一个字母,您就可以判断您正在搞乱的东西是否可以被其他类使用,因此任何更改都需要更多的审查。此外,还有一些工具可以强制执行这样的命名约定。 Microsoft 内部创建/使用的一款名为 StyleCop,可以免费下载。
In languages like C# or VB, the standard is to start private things with lowercase and start public/protected things with uppercase. This way, just by looking at the first letter you can tell whether the thing you are messing could be used by other classes and thus any changes need more scrutiny. Also, there are tools to enforce naming conventions like this. The one created/used internally at Microsoft is called StyleCop and is available as a free download.
从历史上看,C(一种区分大小写的语言)中命名良好的变量由单个小写单词组成。大写字母是为宏保留的。
然后出现了 C++,其中类通常采用大写和驼峰式命名,由多个单词组成的变量/函数采用驼峰式命名。 (请注意,C 语言的人往往不喜欢驼峰式命名法,而是写标识符_this_way。
从那里开始,它就传播开来了。
而且,是的,可能其他区分大小写的语言也产生了一些影响。
Historically, well named variables in C (a case-sensitive language) consisted of a single word in lower case. UPPERCASE was reserved for macros.
Then came along C++, where classes are usually CapitalizedAndCamelCased, and variables/functions consisting of several words are camelCased. (Note that C people tend to dislike camelCase, and instead write identifiers_this_way.
From there, it spread.
And, yes, probably other case-sensitive languages have had some influence.
我认为 lowerCamelCase 之所以流行是因为 java 和 javascript。
在java中,明确定义了为什么第一个单词应该是小写字母的动词,而其余单词以大写字母开头。
java之所以选择lowerCamelCase我想取决于他们想要解决什么问题。 Java 于 1995 年推出,是一种让编程变得简单的语言。经常使用的 C/C++ 通常被认为很困难且技术性太强。
这是java声称要解决的问题,更多的人将能够编程,并且相同的代码可以在不同的硬件上运行。代码就是文档,您不需要注释代码,只需阅读,一切都会很棒。
lowerCamelCase 使得编写“技术”代码变得更加困难,因为它删除了使用大写和小写字母的选项,以便从技术角度更好地描述代码。 Java 不想太难,Java 是一种每个人都可以学习编程的语言。
浏览器中的javascript是由Brendan Eich于1995年在10天内创建的。为什么javascript选择lowerCamelCase我认为是因为java。它与java无关,但它的名称“javascript”中带有“java”。
lowerCamelCase I think has become popular because of java and javascript.
In java, it is specifically defined why, that the first word should be a verb with small letters where the remaining words start with a capital letter.
The reason why java chose lowerCamelCase I think depends on what they wanted to solve. Java was launched in 1995 as a language that would make programming easy. C/C++ that was often used was often considered difficult and too technical.
This was something java claimed to solve, more people would be able to program and the same code would work on different hardware. The code was the documentation, you didn't need to comment code, just read and everything would be great.
lowerCamelCase makes it harder to write "technical" code because it removes options to use uppercase and lowercase letters to better describe the code from a technical perspective. Java didn't want to be hard, java was the language to use where everyone could learn to program.
javascript in browsers was created in 10 days by Brendan Eich in 1995. Why javascript selected lowerCamelCase I think is because of java. It has nothing to do with java but it has "java" in its name "javascript".