Java Development without an IDE ...

Let's just accept that Java has become a more powerful programming language. But of course you don't remember all the principles defined in the Java Language Specification. An esteemed colleague recently said that he uses vim (!) for complex GlassFish v3 code base . First off, I love vim. I love its fundamentals -- the keyboard centric design, a touch-typist's true delight.

But still, it is required that any Java development uses an IDE and a good one like IDEA, Intellij. They are just too meticulous, which helps. For example, look at the following code:
class LocalVarInit {
        public static void main(String... args) {
                int x = 5;
                //bunch of code "not reading" x
                x = 10;
        }
}
For the above code, IDEA complains (mildly) that x (a local variable) has been redundantly initialized . Again, this example is not hypothetical. See here (around line 50) for a reference in production(?) code.
On close examination, you can figure out that the initialization of x is redundant as it is assigned before it is read! This is a subtle point, but achieving such results with other development environments (and Java) is hard. So, please consider using Java IDE (IDEA) for your development. It changes the way you do Java development. Thus, above code can be safely changed to below without generating any compiler warning/error:
class LocalVarInit {
        public static void main(String... args) {
                int x;
                //bunch of code "not reading" x
                x = 10;
        }
}
It is important to note the following:
  • x must not have been read before first assignment.
  • javac -Xlint:all does not give you any hint whatsoever in redundant initialization.
Comments:

There are many ways to attack this problem. Generally, I prefer to
assign to a variable exactly once and declare it final: that way the compiler
checks for an incorrect attempt to re-assign to the variable. Now, the
actual example in the admin CLI is more complex, because it has 3 paths
that can initialize the error code, and several more that read the error
code in order to classify the error. But a mild refactoring suggests a simple solution here: use a private method to return the value of the error
code (the method just encapsulates the code that is setting the error code), and
assign the error code to a final variable. That way it is initialized in exactly one place, and this is checked b the compiler.

As far as IDE vs. VIM, I use NB and VIM both, for different reasons
(and I find NB to be a lot better than IntelliJ, which is probably
surprising to many. NB also has a somewhat usable VIM key
binding). The IDE is good for modifying existing code: things like
code completion and in-line error checking are very nice.
But sometimes it's easier when creating a complex new class
to simply do it in vim (or your favorite powerful editor), then fix
the minor syntax issues later (either with compiler error integration
with the editor, or with the IDE). It also depends on your level of
familiarity with the codebase, and how many people work on it.

The biggest downside of using an IDE is complexity. NB builds very
complex representations of your project, and caches it. If ANY error
occurs in this, you'll have an interesting time figuring out the
problem. Editors of course almost never have this sort of problem.

Posted by ken Cavanaugh on April 05, 2009 at 05:45 AM PDT #

Eat your own dog food and use netbeans. Tools like Findbugs, PMD can do the same in any IDE or build environment.

Posted by guest on April 05, 2009 at 07:27 AM PDT #

@Ken Cavanaugh:

Yeah, some refactoring would come in handy. But declaring a \*local\* variable final has limited uses (e.g. the only legitimate case is that of preventing inadvertent reassignment from within an anonymous inner class). Of course, it has (arguably) readability advantages that assures the human reader that such variable won't be re-assigned (given the fact that it compiles). Thus, in a way, it clarifies programmer's "intent".

And yes, you are right. There are advantages and disadvantages here, like any other decision. But I find using an IDE more productive than not using one.

Posted by Kedar Mhaswade on April 05, 2009 at 09:58 AM PDT #

Anomymous@85.214.73.63,

In no way was I promoting IDEA. That is not the intent. You should see my other post at http://blogs.sun.com/bloggerkedar/entry/default_singleton_boilerplate_code_in about what I mean here.

Yes, I can say proudly that I have been a NetBeans user for quite some time. Comparing and contrasting the IDE's is not the point of the post and I am sorry you did not get it.

Posted by Kedar Mhaswade on April 05, 2009 at 10:04 AM PDT #

Kedar,

I must disagree with you about the use of final on local variables,
as well as on method parameters. There are quite a few references
to various discussions about declaring most local variables as final.
I find it to be a useful practice, because most local variables (except
for loop iterators of various sorts) can easily be made final, and this
both indicates the intent, and prevents accidental modification later.
Admittedly it's something I've only fairly recently adopted (e.g. in
gmbal). It's also most useful in long methods, which probably should
be refactored anyway.

Of course, you can also detect such problems with other methods,
but I like having the intent clearly indicated at the language level.

Posted by ken Cavanaugh on April 05, 2009 at 11:22 AM PDT #

I agree that final on local variables can be quite useful, especially when a method is large and the whole thing doesn't easily fit the human brain.

OTOH, I often see code like this:

private final int getID(final String key) {
return ids.get(key);
}

Where the added 'final' keywords aren't adding values that justifies their noise level. So it hurts when this rule is followed to the letter.

I also find it disappointing that Anomymous@85.214.73.63 doesn't see how much difference it makes when a dataflow analysis is done in your editor, vs when it's done later in the build process.

Posted by Kohsuke Kawaguchi on April 06, 2009 at 03:22 AM PDT #

Kohsuke,

I agree. Perhaps "private final" method specification should result in compiler warning: "redundant specification"?

I used to make local variables "final", but I have long left that habit because it got too verbose without enough benefits in return.

-Kedar

Posted by Kedar Mhaswade on April 07, 2009 at 12:33 AM PDT #

Can i do web programming using java and sun Glassfish not using Netbean IDE?
please help me>>

Posted by pradeek on November 23, 2009 at 06:17 PM PST #

Post a Comment:
Comments are closed for this entry.
About

Welcome to my blog where mostly my work related thoughts are expressed.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today