Sunday, February 26, 2006

More on the Java IDE comparison

Important: you can read the entire article, as it builds up, at this URL

Code template expansion

Template expansion is another time saving feature mainstrem java IDEs support nowadays. The feature consists in a setting up an abbreviation which when typed and followed by a specific key combination expands to a full featured piece of code. For example, you could set up psf+TRIGGER to expand to public static final. Or sout+TRIGGER to expand to System.out.println. Once you get used to this feature you become addicted. Templates are written in a simpe language, which can do more or less advanced things, depending on the implementation.

IDEA's code template expansion feature is the most advanced, mainly because the template definition language takes advantege of the state of the art code querying mechanism that is built into this IDE. When defining templates in IDEA you can query variables of a certain type, or of certain types.

For example, the following template could expand into while (iterator.hasNext()) { Person person = (Person) } and the cursor will be placed where the END tag is.

while($ITER$.hasNext()){ $TYPE$ $VAR$ = $CAST$ $ITER$.next(); $END$ }

This is cool already, but it gets even better when you notice that you can ask IDEA to replace $ITER$ with variables of a certain type - in this case, Iterator. This can be done by specifying that $ITER$ is a variableOfType("java.util.Iterator"). After doing so, when the code is expanded, IDEA will ask you to choose one of iterators in scope. You can also specify that $TYPE$ should take the value rightSideType(). This takes effect with typed collections in jdk 1.5 - at expansion time TYPE will automatically be replaced with the type of the elements contained in the collection. Regarding $VAR, you can ask IDEA to suggest a variable name, by setting it to suggestVariableName(). If you do so IDEA will propose you variable names based on the contained type. For example, if the type is ImageDescriptor IDEA will propose descriptor and imageDescriptor. All you have to do is choose the name you like most or type your own if your not happy with any of the default ones. Choosing one of the proposed choices, however, tends to result in very readable code.

It's possible and very easy to write your own templates. I wrote one to help me initialize log4j loggers. This is the typical situation where template code expansion is very handy. Initializing a logger is an annoing, repetetive task which doesn't have anything to do with the business logic you write. Here is a small template that helps you deal with the problem:

private static final Logger log = Logger.getLogger($CLASS_NAME$.class);

Then you insruct IDEA to give $CLASS_NAME$ the value className(), register your template under the logdecl abbreviation and you're set. All you have to do from now on is type logdecl+TAB and that IDEA will declare that annoying logger for you.

Aparently Eclipse offers code template expansion too, but I haven't been able to make it work. Just couldn't find it. But apparently it's there. But if I can't find it it's just like it wouldn't be.... hmmm......

Netbeans offers code template expansion with almost the same features as IDEA - it's slightly less flexible though. You can define your templates, define shortcuts - I have the feeling there are less options then in IDEA. On the other hand, I've not been able to change the activation key - which is SPACE, by default. Also, the activation sequence is kinda weird: the template is only expanded if you press SPACE just after the abbreviation. If you type the abbreviation move your cursor, then comme back and try to expand, it doesn't work. Have to delete the last letter, retype it and quickly press SPACE . I haven't been able to change the activation key - the config dialog let's you change it, SPACE doesn't work any more, but the new key doesn't work either.

The winner here is IDEA again, with Netbeans comming in second and Eclipse third.

Getter / Setter / Constructor generation

Getter / setter / constructor generation features are comparable in IDEA and Eclipse. Both products offer quite complete features and there is nothing much to say on this matter. Netbeans has a slightly different approach on getter / setter generation: the feature is not available under the "source" menu, but under "refactoring", and it's called "encapsulate fields". A little wierd - I wouldn't think about this as refactoring - but hey, the feature is there. If I had some trouble finding the getter setter generation feature, I was totally unable to find an action to let me generate constructors. This is not a very good point for Netbeans, as constructor generation is a very widely used feature. The feature may be there but if I need more the 5 minutes to find it, it's just like it's missing. So the winners in this category are IDEA and Eclipse with similar and equally easy to use features with Netbeans comming in third, offering hard to find, not so easy to use functionality.