Eclipse tip: Templates

Author: Kasper B. Graversen, 19/08/08
Keywords: Java, Eclipse, Testing
Abstract: A quick tip for the Eclipse IDE on how to make Eclipse code for you rather than you code in Eclipse! The mechanism is called templates. While this tip focuses on making constants easy to create, templates are a general concept. Constants were chosen as they are easy to make templates for and are particularly useful when writing test helpers.
subscribe to my RSS feed


Bookmark and Share


Eclipse tip: Templates

Many people don't realize the amount of configuration that is possible in the Eclipse IDE. Generally, if you get the feeling that what you are doing could be automated, chances are that it can. This tip is concerned with making eclipse program for you, rather than you programming in eclipse! (sounds nice, doesn't it :-) The idea is to set up templates of code and make use of these whilst programming.

Templates are general in their form. I've chosen to introduce them using a real-life case. A template I rather enjoy. When writing structured unit tests, you often declare a lot of helper classes to create populated domain objects for you (see e.g. the "object mother pattern"). This means you'll do loads of public static final declarations. And typing public static final really isn't much fun. Fortunately, it is quite easy in Eclipse to set up a short cut for this.

The idea is that when you type psf Eclipse offers the option to expand this into the character sequence public static final. In Eclipse terminology this is called templates. To achieve this you simply go to Window → Preferences → Java → Editor → Templates.

Setting up a template in Eclipse to automate programming tasks.
Setting up a template in Eclipse to automate programming tasks.

Now fill out the window as detailed in the above image. You are now able to type psf and click ctrl-space to get a suggestion for auto completion. This is really great! If your Eclipse does not respond to your gestures, try giving it the good ol' restart. While my tip may seem a bit dull, once installed, you'll quickly feel how much better your coding "flows" and the drag of constantly having to declare the blasted public and static and final just disappears!

While there are limits to what you can express using templates, they do enable you to automate quite a lot of code. I recall writing templates for automating code when writing business delegates (a useless J2EE pattern forced upon you should you be so unlucky as to code EJB2). I advice you to set up the psf template use it a bit and from there configure templates for everything you hate typing again and again.

Enjoy your coding and have fun...



Comments

If you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.


If you like this site consider subscribing to my RSS feed or how about subscribing by Email.


Help spread the word

Share this post on your favorite social bookmarking sites:
If you enjoyed this article, found it thought provoking, educative or otherwise good, please link to this page from your page or social bookmarking page. If you have any texts you think are worth publishing on First Class Thoughts, don't hesitate to send me a mail! Quality always welcome.


Bookmark and Share


The most recent contributions
28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers.
13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise"
20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba".
16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
Nothing of interest? Try browsing the entire article archive...