July 13, 2007
Enterprise Systems: The New Bounce in Suns Step. Dont look now, but Sun Microsystems Inc. has a new bounce in its step.
From its recent...
More »
June 24, 2007
Jazoon'07. Jazoon'07 brings together experts and users of Java and open source technologies from all...
More »
|
 |
home
> news
> developers
> code advice #10: place brackets with the declaration type, not the name
Code Advice #10: Place brackets with the declaration type, not the name
(See intro for a background and caveats on these coding advice blog entries.)
The most important code style rule is that when you modify somebody else's code, you preserve the coding style in that file. Even when it hurts. The only thing worse than an "incorrectly" styled source file is an inconsistently styled one!
One of my coworkers writes Java code in a very C-inspired way. One of the habits I have difficulty with is the bracket placement for arrays. I thought I would dedicate a blog entry to this, because it occurs in many other code bases. A quick scan revealed a couple thousand matches in the JDK source code (which, granted, is a tiny fraction of the overall codebase).
Briefly stated, the correct way (as far as I'm concerned) to place brackets in an array declaration is with the type, not with the variable name. Therefore, this is okay:
int[] scores;
and this is not:
int scores[];
Here's a real-world example, from String.java:
@Deprecated
public String(byte ascii[], int hibyte, int offset, int count) {
checkBounds(ascii, offset, count);
char value[] = new char[count];
...
As you can see, there are two violations here - both in the parameter usage, and in the declaration
of local variable "value".
Most of you will probably just nod at this and move on, since it's what you're already doing. But if there's one thing I've noticed, it's that coding style blog posts always generate controversy - and that especially those of you doing it the Other Way are reluctant to change.
So, let me spend a couple of paragraphs arguing why you should place the brackets with the type name, not the variable name. The most obvious answer is that brackets-with-type is what by far most Java developers are using, so you should simply do it to make your code consistent with the accepted practice.
However, it does have some logical reasons too.
First, let's take a look at a method declaration that returns an array - this is java.util.Arrays.copyOf:
public static int[] copyOf(int[] original, int newLength) {
Notice how the brackets appear with the type declaration. You do not have the luxury of moving them to the end of the line, or even just past the variable name:
public static int copyOf[](int[] original, int newLength) { // WRONG!
Note that if you have code like this:
int[] a, b;
Here both a and b will be integer arrays. This is different than in C
where the similar
int* a, b;
would leave only a an array, and b a scalar!
Let me anticipate the objections. I can think of two advantages for placing brackets with variable names.
First, it's your only option if you want to mix and match plain and array declarations of a type in the
same statement:
int a, b[], c, d[];
However, we can dispense with this quickly: you should not compress declarations this way. Use a separate
statement for each.
The second objection, which I have some sympathy for, is that placing brackets with variable names
is consistent with the way array instances are used:
int foo[];
...
foo[i] = -1;
However, between declaration and usage we have the initialization, and the initialization uses the
typename with brackets:
int[] foo = new int[50];
foo[i] = -1;
An easy way to find violations of this coding style is to use Checkstyle. Note that while javac will
happily compile both declaration styles, the error message from checkstyle is:
Array brackets at illegal position.
I guess that's stretching the definition of "illegal" a bit (since the
Java Language Specification defines what is legal and what is not, and both forms are allowed). However, in my opinion it is good practice to place the brackets where most Java developers place them - with the typename in declarations.
Date: July 17, 2006
Category: Developers
More »
Others News
|