views:

205

answers:

4

I've been quoting this segment from Sun's document for the past few days, and only now do I stop and think about what it's saying, and I can't make sense out of it. Please keep in mind that English is not my first language.

Naming conventions << clickable to official source

Variables: Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

How is this making sense? Isn't this saying that class names are in mixed case with a lowercase first letter? Like I should name it class myClass? And class constants are also in mixed case with a lowercase first letter? Like instead of Integer.MAX_VALUE, it should've been named integer.maxValue?

And is it really saying anything about how variables themselves should be named?

Am I not parsing this properly or is this actually a blatant error?

+2  A: 

This is almost certainly a badly-edited version of what is actually meant:

Variables: All variables names (excluding constants) are in mixed case with a lowercase first letter.

Michael Petrotta
Yes, that would've made sense, as that _should_ be the official recommendation. I'm just surprised that this presumably official document contained such a glaring error and it's uncaught after all this time.
polygenelubricants
The statement as written makes no sense. I stared at it for a while with a funny look on my face, trying to make sense of it. But in hundreds of pages of recommendations, these things get missed.
Michael Petrotta
So how am I suppose to quote the authority on this recommendation in my answers? I always fall back on this quote whenever I see people name their variables with uppercase first letter; only now do I realize that it's not making any sense, and is probably making things worse for them.
polygenelubricants
The text you're referencing is clearly nonsensical. Contact Oracle, ask for a fix. Refer to other coding convention sites, like http://geosoft.no/development/javastyle.html: "Variable names must be in mixed case starting with lower case."
Michael Petrotta
+3  A: 

This is a few boxes above it:

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

class Raster;
class ImageSprite;

and below that:

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

static final int MIN_WIDTH = 4;

static final int MAX_WIDTH = 999;

static final int GET_THE_CPU = 1;

Contradicting, no? "Class constants" should be excluded from that variables convention statement, even if they mean non-static constants of the public final kind, as it confuses people.

As for "except for variables," I believe they mean primitive local variables should only be one word.

loliii
The question is not what the other boxes are saying. The question is what is this box saying. Because it doesn't make any sense whatsoever.
polygenelubricants
The best way to get around this is to look at the context.
loliii
+1  A: 

You need to read the whole thing to get the context:

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

So [local] variables are to be short (meaning they may not have enough "words" to have mixed case).

All other variables (those at the instance or class level, the non-local ones) are to be have mixed case and start with a lower case letter (and presumably be "wordy" enough to be able to have mixed case, like "lineNumber" instead of "number");

EDIT (forgot about constants).

There are two ways of looking at constants in Java:

  1. anything with the word final
  2. anything with a guaranteed value that is always the same

so:

class Foo
{
    public final int variable;
    public final int CONSTANT;

    static
    {
        variable = // generate a random number.
        CONSTANT = 5;
    }
}

Here "variable" is "constant" in that once it is assigned a value it cannot change, but it is not a constant like "CONSTANT" since "CONSTANT" will always have a value of 5.

I consider #2 to be the only constants in Java.

EDIT #2 (in response to the comment below).

I would re-write that as:

Except for local variables, all instance, class, and class blank finals [and I might also specify that the blank final be without have a single compile time value] are in mixed case with a lowercase first letter.

You can look at http://www.codeguru.com/java/tij/tij0071.shtml for some more of a description.

TofuBeer
That first sentence still doesn't make any sense to me, and at least a few others ("Except" being an important keyword). If it makes sense to you, you need to explain it better to us.
polygenelubricants
+4  A: 

Sun has accepted this as a bug, with low priority:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4311597

describing the convention for naming variables (the penultimate row in the table). It is plainly wrong - class constants are not in mixed case, as the next row in the table shows. And what is this "except for variables" business? The text is supposed to be describing variables!!

The text should read:

"All instance and class variables are in mixed..."

thus dropping the words "except for variables" and "class constants"

Kobi
Yeah, 10 years ago!
loliii
Awesome. Finally a definitive admittance that it's a mistake, and I've not gone crazy and forgotten how to parse English. Well, I may still be going crazy nonetheless.
polygenelubricants
Well, it says low priority, I guess they mean it... +1 the bug until it is fixed, it only has one vote...
Kobi
@Kobi - I did. Your turn now.
Stephen C
@Stephen - Done. It seems I even had an account. Amazing.
Kobi