|
|
|
||
|
It seems that Kris has been lured toward the sirens that are the proposed JDK 1.5 static imports. I am convinced that static imports will reduce code clarity and therefore increase the bug rate. I offer a contrived example to demonstrate my position: I am working on a class that staticly imports According to the updated JLS: A static-import-on-demand declaration never causes any other declaration to be shadowed. That's fine but it does create confusion on the order of that that would be caused by operator overloading (which is not in Java due in part to "added complexity" associated with it). For the sake of this example, let's say that public static float sin(float angle); public static float sin(double angle); (again, this is contrived to prove a point) and the signature that I added to my class is: float sin(float angle); The call to ... float angle; ... rotated = PI * sin(angle); ... In this exmaple, it may be easy to determine which public static float sin(int angle); that I want to staticly import. I can't do it according to the JLS: If two single-static-import declarations in the same compilation unit attempt to import members with the same simple name, then a compile-time error occurs, unless the two members are the same member of the same type, in which case the duplicate declaration is ignored. So you hopefully see the mess that I'm in. I'll attempt to illustrate to drive the point home.
import static java.lang.Math.*;
import com.someco.MathFunctions; // can't be static
private float sin(final float angle) { ... }
public class ContrivedExample {
...
float angle;
...
rotated = PI * sin(angle); // from local sin()
...
other = sin(angle / 1.5); // from java.lang.Math (1.5 is double)
...
uugh = MathFunctions.sin((int)(angle / 65535)); // from MathFunctions
...
}
That is a debuggers worst nightmare. Of course placing strict coding constraints on how, when, where, and under what conditions static imports are used will help alleviate these problems but given a large set of imports, it might be easier said than done. David Flanagan has also touched on some other issues -- specifically, how one can import a method with the same name but different signatures. Static imports "solves" something that was never a problem to begin with (i.e. explicit names are a good thing). A more suituable solution to this "problem" would be in-line (or horizontal) code folding; just as some IDE's provide the ability to vertically fold various scopes, horizontal folding would fold the qualifier of the name. Updated April 23 at 11:45AM |
| Comments | ||||
|
| Post a comment |
|
|
Unless otherwise expressly stated, all original material of whatever nature created by Rob Grzywinski and included in this weblog and any related pages, including the weblog's archives, is licensed under a Creative Commons License. |