开发者

Where does the k prefix for constants come from?

开发者 https://www.devze.com 2023-02-11 07:29 出处:网络
it\'s a pretty common practice that constants are prefixed with k (e.g. k_pi). But what does the k mean?

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 mean开发者_如何转开发t char?


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 idiots 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.


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.


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.


It means the value is k-onstant.


I think mathematical convention was the precedent. k is used in maths all the time as just some constant.


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.


Actually, whenever I define constants in typescript, I do something like this -

NODE_ENV = 'production';

But recently, I saw that the k prefix is being used in the Flutter SDK. It makes sense to me to keep using the k prefix cuz' it helps your editor/IDE in searching out constants in your codebase.

Where does the k prefix for constants come from?


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).


I think, it means coefficient (as k in math means)

0

精彩评论

暂无评论...
验证码 换一张
取 消