Home > Articles

  • Print
  • + Share This
From the author of

Microsofties

Some programmers come to Java from Windows. Such programmers are easy to spot because of their unusual naming style, which clashes with the standard Java style. For example, the Win32 API begins function names with a capital letter. This is just the opposite of most Java conventions, where all methods (except for constructors, which are a little bit messy anyway) begin with a lowercase letter.

Those used to working with Microsoft's Component Object Model (COM) name their classes beginning with C and their interfaces beginning with I. COM is, in effect, a way to add some Java features to C++. Java makes these distinctions as part of the language. C++ doesn't have actual interfaces and must substitute abstract classes, and they indicate the distinction by following the naming convention.

The COM style of programming also requires you to use Hungarian notation, such as this charming example:

int cwSz;
int cch;
int *pbsy;

// ...
if (cch>=2)
  cwSz=(cch-2/sizeof(int)+1;
*pbsy=(int *)(psy=PsyCreate(cwSY+cwSz))-rgwDic;

The c prefix stands for count, w stands for words, and p stands for pointer. pbsy is a pointer into the middle of a field of type sy, and cwSz refers to the size of an Sz structure, measured in words (as opposed to cbSz, which would be the same thing measured in bytes).

This particular programmer prefers very abbreviated names for most of the identifiers: the name rgwDic spends half its letters telling you what type it is and only three more for what the variable is actually about (presumably a dictionary). Similarly, cwSz is 50% type and 50% name. (Would Size really have cost so much more than Sz?)

Hungarian notation was originally invented for C, and later applied to C++. In these languages, it can be hard to remember what is a pointer to what, and you have to get the number of levels of indirection correct for the program to work. Because Java eliminates the pointers, the code is simpler, and the naming convention is less necessary.

In addition, C programs used to require that all identifiers be declared at the beginning of the function, which meant that the type definition of a variable could often be far removed from the first use of it. In Java and C++, you can declare a variable at its first use, making the convention of coding the type with the name less necessary, since it's right there anyway.

Still, some Java programmers have adopted a Hungarian-like notation for Java, like this sample I found on the Web:

int iBytesRead;
byte[] abyBuffer = new byte[4096];
  • + Share This
  • 🔖 Save To Your Account