Speed up boilerplate code with Eclipse templates

Take the following piece of code:

for (int i = 0; i < arguments.length; i++) {
    String argument = arguments[i];
    // ... Do something with argument...

How many times have you typed out a similar for-loop, mistyped arguments the 2nd time, made a syntax error by forgetting a semicolon, etc, etc. Same goes for writing a main method or a try-catch block. Even typing an enhanced for-loop in the above example is slow.

To me they’re code zombies. Nobody likes them, everybody wants to get rid of them asap and just when you think you’ve killed one, it wakes up again and doesn’t stop… ever.

This is where templates bash in with modified shotguns and spiked baseball/cricket bats. You use templates to rid yourself of these code zombies, because life’s too short and fast to have to write long and slow code.

Why use templates?

Templates are an easy and fast way to add common, boilerplate code. They’re like bits of snippets, but usually shorter and more generic. Just type the name of the template, press a key and the code is magically inserted.

Existing code can also be affected by templates that support selections. For example, you can select a block of code and use the try template to wrap the entire block in a try-catch block.

Templates can also contain variables to make your generated code a bit smarter. Variables are like placeholders allowing you to easily edit various parts of the template.

How do you use templates?

Watch the video to get an idea of how templates work. It shows how to add a main method, with a for-loop that prints out all command line arguments using templates. Notice the smooth movement between variables (the green boxes):

Some notes:

  • To open a template, type the name then press Ctrl+Space, choose the template and press Enter.
  • The green boxes are template variables. You can use the Tab key to move from one variable to another. Forget about the mouse or arrow keys.
  • You can also wrap existing code in templates (eg. a for loop). Simply select the code, press Ctrl+Space twice and select the template.

How does this make you faster?

You did watch the video, right? In 20 seconds we wrote code that would normally take you around 40 to 50 seconds (and that’s if we don’t make any mistakes). The video is deliberately slowed down as well for clarity. We can improve that to 10-12 seconds for all that code.

So, 400% faster = time for 4 more cups of coffee a day = 100 x happier developer!

Templates have some smarts in them as well. The for-loop in the video immediately recommended the array args as it was the nearest collection/array in scope. This works 80% of the time, so that cuts down on some time as well.

Some useful templates to get you going

Eclipse has a lot of built-in templates. Have a look at them by going to Window > Preferences > Java > Editor > Templates.

For the lazy, here are some useful ones to speed things up a bit:

  • main – inserts a main method, arguments and all.
  • for – generates for-loops, eg. one with an index over an array or with a temporary variable; can wrap a selected block of code.
  • foreach – inserts an enhanced for-loop; can also wrap an existing selection in a for loop; can wrap a selected block of code.
  • try – produces a try-catch block with the default Exception and a todo to remind you to complete the exception block; can also wrap an existing selection in a try-catch; can wrap a selected block of code.
  • switch – adds a switch statement together with a case-block and default-block.
  • while – inserts a while-loop, optionally with an enumerator; can wrap a selected block of code.

Massive time savers already right there.

How do you create/edit a template

A very important topic, so I discuss this in a separate post.

Support Eclipse On E

Leave a Reply

Your email address will not be published. Required fields are marked *