7

This question already has an answer here:

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?

marked as duplicate by gnat, amon, Kilian Foth, Konrad Morawski, AProgrammer Jan 14 '14 at 10:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 7: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: en.wikipedia.org/wiki/Code_smell – SOReader Jan 14 '14 at 9:43
  • 1
    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
17

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

  • 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 5:55
  • 1
    I'd consider such usage to be a code smell. – World Engineer Jan 14 '14 at 7:41
  • 1
    @DocBrown In Java it's pretty common, as even the JDK uses that convention. See also stackoverflow.com/a/2530174/160539 – msell Jan 14 '14 at 7:45
  • 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 9:01
  • 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
-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.

  • Java doesn't "offer anything similar" – gnat Jan 14 '14 at 10:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.