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?
4
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…
9
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.
1