52

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?

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101

9 Answers9

111

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 22
    I could accept this answer if you remove the subjective and argumentative parts ;) – Johan Kotlinski Feb 16 '11 at 14:04
  • 3
    Can you give references to this story? That's not at all the story I know. They story I know is that Hungarian notation was broadly misused, particularly at Microsoft. The special prefix or suffix characters were meant to give extra information about the data, in a concise manner that the team understands. Static types were never meant to be part of it. Today people (myself too) still commonly append an underscore to member data names. That too is Hungarian notation. – wilhelmtell Feb 16 '11 at 16:35
  • 2
    @wilhelmtell: There's a reasonable account of it at http://en.wikipedia.org/wiki/Hungarian_notation, and Simonyi's paper on what became known as "Apps Hungarian" (the first variant) is available at http://msdn2.microsoft.com/en-us/library/aa260976(VS.60).aspx. I can't find any definitive account of "Systems Hungarian" (the useless variant); as you say, this originated in Microsoft, and presumably it vanished from history when they decided it wasn't a good idea. – Mike Seymour Feb 16 '11 at 16:54
  • It might entertain you to know, I worked some with Windows Media codec - the amount of Hungarian notation there was really bizarre. Personally I just enjoy some basic ones (const+scope), so I instantly get an idea about what some code does. – Johan Kotlinski Feb 16 '11 at 21:56
  • So people in Google are idiots? http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Constant_Names – szx Aug 31 '13 at 23:47
  • 4
    So you are saying everyone at Google is a genius? – jimpic Jul 31 '14 at 11:37
  • 3
    Joel Spolsky's [Making Wrong Code Look Wrong](https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/) article has an interesting (as usual) account of the history of Apps Hungarian and Systems Hungarian. – pjh Sep 18 '18 at 16:54
37

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.

misiu_mp
  • 909
  • 1
  • 12
  • 17
10

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.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • Was google coding standard designed by a committee? What kind of committee was it? – misiu_mp Feb 24 '11 at 16:15
  • 5
    It could be argued that all caps is just a slightly different form of Hungarian Notation. Both are simply a convention to communicate semantic meaning. – Nick Forge Jul 27 '13 at 08:18
  • 3
    Hungarian Notation is the practice of prefixing variable names (and in extreme cases, class names and method names as well) with abbreviations for their types. All caps maintains the readability of the code without causing a novice programmer to ask "Why is the 'k' there?". So no, it cannot be argued it is a "different form" of Hungarian Notation. – Zac Howland Aug 13 '13 at 17:06
  • 2
    Problem with all caps is it is generally harder to read, or so the studies say. – Steven Craft Oct 01 '13 at 16:23
  • 1
    If the entire program is all caps, yes. If you have a constant that is defined in all caps, no. It just makes it stand out: `const int MYCONST = 3; if (MYCONST == x) {...}` – Zac Howland Oct 01 '13 at 16:24
  • 3
    "Don't use use Hungarian Notation because of its arbitrary naming convention, use this arbitrary naming convention instead!" Is the vibe I get. Nick Forge is right. Using a novice programmer as an example isn't even valid. Because they would ask why a variable is named with all caps. And if you need to explain it to them. Then explaining why certain variables are prefixed with 'k' is no different. The only valid argument against Hungarian notation is prefixing it with types like u32 or f64. – A. L Apr 30 '21 at 06:17
6

It means the value is k-onstant.

awm
  • 6,526
  • 25
  • 24
6

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

sheepez
  • 986
  • 1
  • 10
  • 26
4

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.

Shinnok
  • 6,279
  • 6
  • 31
  • 44
3

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.

enter image description here

Piyush
  • 693
  • 1
  • 7
  • 12
2

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

0

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

BlackBear
  • 22,411
  • 10
  • 48
  • 86