Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the k prefix for constants come from?

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?

like image 702
Johan Kotlinski Avatar asked Feb 16 '11 12:02

Johan Kotlinski


People also ask

What is the naming convention for constant?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.

What is K in flutter?

As far as I know in Flutter only constants that are real constant "magic" values are prefixed with k . It's still up to you how you want to handle that. It might depend on the kind of code you write, library, framework, app, ... 👍 3.


1 Answers

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.

like image 144
Mike Seymour Avatar answered Oct 07 '22 18:10

Mike Seymour