开发者

How is this statement making sense? (Sun's naming convention for Java variables)

开发者 https://www.devze.com 2022-12-28 16:41 出处:网络
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 any sense of it. Please keep in mind that Englis

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 any sense of it. Please keep in mind that English is not my first language.

Naming conventions

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 lower-case first letter? Like I should name it class myClass? And class constants are also in mixed case with a lower-case first letter? Like instead of Integer.MAX_VALUE, it should've been named integer.maxValue?

And is 开发者_StackOverflow社区it really saying anything about how variables themselves should be named?

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


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"


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号