October 16, 2007

Effective Eclipse: III. Don't write the code, generate it


I hope that we will never be able to generate applications. I would be without job then and you probably too. But what we can generate are various repetitive and boring pieces of code.


The first thing that can be generated is class file. You are using it, but you have probably never realized how much time did it save you. If there was no class skeleton generation, you would have to: create new file in the proper directory (with respect to package (and create the folders too)) and place the package statement at the beginning of a class.
I noticed that all people generate their classes, but not all of them specify implemented interfaces and extended class. Surely, you can generate the class and write the extends and implements part afterwards, but it involves moving the cursor, writing and eventually generating abstract or implemented methods. I found it much easier to press ALT + E (extend) to add extended class and ALT + A (add interface) to add interfaces.


I you look at the Source menu you can find more "Generate" commands. But none of them is so valuable and useful as the Eclipse templates. They are the true gem in my daily work and its pity they are hidden out of sight. And they are hidden well.


What are they and how to get them?

Templates serve as a shorthand for a snippet of code. You type the magical word and it will be transformed into the snippet.

Some examples:

Type sysout and press CTRL + SPACE (the autocompletion, in case you didn't know) and it will be automagically changed to System.out.println(); with the caret waiting right in the middle between the parenthesizes.



While we are here, let's explore the next interesting feature. Type "for" and press CTRL + SPACE to change it to a skeleton of a for loop.



Notice the blue boxes around some of the commands. They represent the caret stop and you can move between them using the TAB key. Now, with the caret on "iterator", type the name of the variable representing the iterator. It's name will be changed in the whole loop as you type (the occurrences are marked with a light blue background). Now just two more TABs to change the collection variable and the type. Noticed the green line sitting in the empty line? It marks the position where the caret will jump when Enter key is pressed. So after changing the variable type you can just press Enter and the caret will jump to the empty line. Notice, that when you use autocompletion the green marker is always there, waiting for you to press Enter.


The templates are defined under: Window -> Preferences -> Java -> Editor -> Templates
where you can define your own templates.


That pesky CTL + SPACE shortcut offers some more surprises. If you get used to it, you will be hitting it often, even in situations where there is nothing to autocomplete. Or is it?



Guess what is on the picture above? It is eclipse trying to autocomplete the name of a variable.

It is pity it cannot read my mind. I often find myself hitting the shortcut in the middle of the string ever wondering why it doesn't complete the word :)


Never can remember the signature of a method you want to override? Never mind, hit CTRL + SPACE and you will get a list.


7 comments:

Jeremy Weiskotten said...

Nice intro to Eclipse templates. I used to love this feature when I was a Java developer using Eclipse. There are similar features in TextMate, which is what I use for Rails development now. Not quite as powerful as Eclipse, but good enough IMO.

carrion said...

Hi! Great post!
I'm a auto-completion maniac and I try to use keyboard shortcut whenever possible.
I could add Alt+/ to auto complete words. It's very useful and somehow complements Ctrl-Space.

Sébastien said...

Hi,
Nice post for those who don't know yet about this wonderful feature :)

I must shamefully admit that I have a template for singleton hidden somewhere... :)

Anonymous said...

@carrion:
Thank you! I have been hoping there was an "autocomplete a word" command in eclipse! A while ago I learned of VIM's CTRL+P to autocomplete, and I totally loved that and wished Eclipse had that... well sure enough it does!

Anonymous said...

:(
Why my ctrl+space dont work.
How can i fix this

Tomáš Kramár said...

Maybe you have set this shortcut for something else in your Desktop Environment. When you press it, it gets consumed by your DE and never propagates to Eclipse.

Poria said...

Interesting to know.