10

I have started to see variable initialisations like that in Java libraries:

Class clazz = new Class();

or

Class klass = new Class();

Is there a particular reason for people to misspell the word "class" when declaring these variables, and to use a "kool boyz" sort of writing style? Also what is wrong with using something like cls or something similar?

Konrad Morawski
  • 9,721
  • 4
  • 37
  • 58
Sarp Kaya
  • 331
  • 2
  • 6
  • 1
    Honestly, I did not see this "often" before, as your question title presumes (except when the author of the code had some kind of dyslexia). – Doc Brown Jan 14 '14 at 07:25
  • It's not often and it should not be! If don't know how to call an object it clearly means you don't know what the code should do. This simply tells you that your code stinks: http://en.wikipedia.org/wiki/Code_smell – SOReader Jan 14 '14 at 09:43
  • 2
    I, as the OP, did not use the "often" adverb. My question was edited. – Sarp Kaya Jan 14 '14 at 10:18
  • 1
    I removed "often" from the title (someone else edited it in for you), but I voted to close it as a duplicate anyway. – Konrad Morawski Jan 14 '14 at 10:28

2 Answers2

20

This is a special case with the name class, as that is a reserved keyword in some languages such as Java and thus cannot be used as the variable name. Using clazz or klass is a way to workaround that. Other options would include e.g. myClass.

In Java it's pretty common, as even the JDK uses that convention. See also https://stackoverflow.com/a/2530174/160539

"class" is what you want, but abbreviating or inserting junk ("a", "the", "_", etc) reduces clarity. clazz just says class. "International" English speakers (those reading both English and American) are used to transposing 's' and 'z'.

Since Java has had disclosed source and a suitable culture right from the start, worthwhile Java code and tutorials pick up the same conventions...

msell
  • 576
  • 3
  • 6
  • 1
    Also note the use of C#'s "@" for variable-naming if you're really set on using a keyword (assuming that's your language of choice). – J Trana Jan 14 '14 at 05:55
  • I don't think it occurs often in real world cases that an object is intended to be named "class" - so this is a very pathological case. – Doc Brown Jan 14 '14 at 07:30
  • 1
    I'd consider such usage to be a code smell. –  Jan 14 '14 at 07:41
  • 1
    @DocBrown In Java it's pretty common, as even the JDK uses that convention. See also http://stackoverflow.com/a/2530174/160539 – msell Jan 14 '14 at 07:45
  • @msell: you may be right, but I don't see a use case description in that SO post as well. – Doc Brown Jan 14 '14 at 07:49
  • 2
    If Java programmers like to use this kind of variable naming, then Java programmers smell just like their code. – gbjbaanb Jan 14 '14 at 09:01
  • @DocBrown, I have seen this being used in JDK internal libraries such as: http://cr.openjdk.java.net/~shade/jmh/results-rework-1.webrev/jmh-core/src/main/java/org/openjdk/jmh/runner/MicroBenchmarkHandlers.java.sdiff.html – Sarp Kaya Jan 14 '14 at 10:20
  • 2
    @msell: looking at that link, I guess it's pretty common in Java for **meta programming**, where you have programs dealing with classes and methods itself (and not pretty common in general). That makes more sense for me. – Doc Brown Jan 14 '14 at 11:29
  • common to see `_` after the reserved word in Python e.g. `type_` or `class_` – s g Mar 26 '20 at 22:05
-1

If you really have to use a reserved word, then in .NET you can escape it like this:

var @class = new Class();

In my mind it's better than calling something 'klass'. I won't be surprised if other languages (JAVA) offer something similar.

Generally I would avoid 'cls', 'klass' or anything that requires additional mind mapping. It's a background noise and it's not needed.

CodeART
  • 3,992
  • 1
  • 20
  • 23