常量的 k 前缀从何而来?
常量以 k
为前缀(例如 k_pi
)是一种很常见的做法。但是k
是什么意思呢?
难道 c
已经意味着 char
了吗?
it's a pretty common practice that constants are prefixed with k
(e.g. k_pi
). But what does the k
mean?
Is it simply that c
already meant char
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是一个历史上的奇怪现象,在喜欢盲目应用他们不理解的编码标准的团队中仍然是常见的做法。
很久以前,大多数商业编程语言都是弱类型的。自动类型检查,我们现在认为是理所当然的,仍然主要是一个学术话题。这意味着很容易编写带有类别错误的代码;它可以编译并运行,但会出现难以诊断的错误。为了减少这些错误,一位名叫 Simonyi 的小伙子建议您在每个变量名称开始时添加一个标签来指示其(概念)类型,以便更容易发现它们何时被误用。由于他是匈牙利人,这种做法被称为“匈牙利表示法”。
一段时间后,随着类型语言(尤其是 C)变得越来越流行,一些被误导的人听说这是一个好主意,但不理解其目的。他们建议为每个变量添加冗余标签,以指示其声明的类型。它们的唯一用途是更容易检查变量的类型;除非有人更改了类型并忘记更新标签,在这种情况下它们是非常有害的。
第二种(无用的)形式更容易描述和执行,因此被很多很多团队盲目采用;几十年后,你仍然会不时地看到它被使用,甚至被提倡。
“c”是“char”类型的标记,因此它不能也用于“const”;所以选择了“k”,因为它是德语中“konstant”的第一个字母,并且广泛用于数学中的常数。
It's a historical oddity, still common practice among teams who like to blindly apply coding standards that they don't understand.
Long ago, most commercial programming languages were weakly typed; automatic type checking, which we take for granted now, was still mostly an academic topic. This meant that is was easy to write code with category errors; it would compile and run, but go wrong in ways that were hard to diagnose. To reduce these errors, a chap called Simonyi suggested that you begin each variable name with a tag to indicate its (conceptual) type, making it easier to spot when they were misused. Since he was Hungarian, the practise became known as "Hungarian notation".
Some time later, as typed languages (particularly C) became more popular, some misguided people heard that this was a good idea, but didn't understand its purpose. They proposed adding redundant tags to each variable, to indicate its declared type. The only use for them is to make it easier to check the type of a variable; unless someone has changed the type and forgotten to update the tag, in which case they are actively harmful.
The second (useless) form was easier to describe and enforce, so it was blindly adopted by many, many teams; decades later, you still see it used, and even advocated, from time to time.
"c" was the tag for type "char", so it couldn't also be used for "const"; so "k" was chosen, since that's the first letter of "konstant" in German, and is widely used for constants in mathematics.
我没见过那么多,但也许它来自某些语言(尤其是日耳曼语)单词constant的拼写 - konstant。
I haven't seen it that much, but maybe it comes from certain languages' (the germanic ones in particular) spelling of the word constant - konstant.
不要使用匈牙利表示法。如果您希望常量脱颖而出,请将它们全部大写。
附带说明一下:Google 编码标准中的很多内容都是不好的做法(就代码可读性而言)。这就是当您由委员会设计编码标准时会发生的情况。
Don't use Hungarian Notation. If you want constants to stand out, make them all caps.
As a side note: there are a lot of things in the Google Coding Standards that are poor practice (in terms of code readability). That is what happens when you design a coding standard by committee.
这意味着该值是 k 常数。
It means the value is k-onstant.
我认为数学惯例是先例。 k 在数学中一直用作常数。
I think mathematical convention was the precedent. k is used in maths all the time as just some constant.
K代表konstant,是关于常数的双关语。它与编码风格有关。
这只是一个偏好问题,有些人和项目使用它们,这意味着他们也接受匈牙利表示法,但很多人不接受。那没那么重要。
如果您不确定前缀或样式可能意味着什么,请务必检查项目是否有编码样式参考并阅读它。
K stands for konstant, a wordplay on constant. It relates to Coding Styles.
It's just a matter of preference, some people and projects use them which means they also embrace the Hungarian notation, many don't. That's not that important.
If you're unsure what a prefix or style might mean, always check if the project has a coding style reference and read that.
实际上,每当我在 typescript 中定义常量时,我都会做这样的事情 -
但最近,我看到 Flutter SDK 中使用了
k
前缀。对我来说,继续使用k
前缀是有意义的,因为它帮助您的编辑器/IDE 在代码库中搜索常量。Actually, whenever I define constants in typescript, I do something like this -
But recently, I saw that the
k
prefix is being used in the Flutter SDK. It makes sense to me to keep using thek
prefix cuz' it helps your editor/IDE in searching out constants in your codebase.我认为,它的意思是系数(数学中k的意思)
I think, it means coefficient (as k in math means)
这是一个约定,可能来自数学。但对于常量还有其他建议,例如 Kernighan 和 Ritchie 在他们的《The C language》一书中建议用大写字母编写常量名称(例如#define MAX 55)。
It's a convention, probably from math. But there are other suggestions for constant too, for example Kernighan and Ritchie in their book "The C language" suggest writing constants' name in capital letters (e.g. #define MAX 55).