Monday 27 May 2013

Live Templates

One of my favourite features of IntelliJ is Live Templates because it enables me to be more productive and makes my work a lot more enjoyable. In this post I want to share with you:
  1. An introduction to Live Templates
  2. How it makes me more productive
  3. Why I really enjoy using them!

What are Live Templates?


In a nutshell, Live templates are short-cuts for writing code. They can be incredibly simple but are also very powerful, allowing developers to quickly and reliably produce commonly used code constructs (such as for loops, covered later).

[A more detailed explanation and documentation can be found here: http://www.jetbrains.com/idea/webhelp/live-templates.html]

To find Live Templates within IntelliJ, open up the Settings (Default shortcut: Ctrl + Alt + S)


Above you can see the setting window and some pre-defined Live Templates. So how do you actually use them?

Gaining productivity


Using Live Templates is incredibly simple and there are many pre-defined ones we can you right now. One of the best ones that everyone one can mostly likely benefit from is the "iter" or for-each Live Template.

Suppose we have the following code:

  1. Collection<String> listOfNames = getFriendNameList();  
  2. // I want to wish all my friends a Merry Christmas  
Figure 2.1 - Initial code

Normally the next step would be you would start typing out a for-each loop manually. However, what you can do instead is:

  1. Collection<String> listOfNames = getFriendNameList();  
  2. // I want to wish all my friends a Merry Christmas  
  3. iter // type out "iter"  
Figure 2.2 - About to use the for-each live template

Now hit the "Tab" key and...



Hey presto! IntelliJ has constructed a for-each loop for you using your Collection! Note that IntelliJ automatically highlights the collection and you can navigate up and down between multiple collection candidates.

Hitting enter will then allow you to rename your item variable:


Again IntelliJ jumps straight to the variable and you can give it a sensible name. Finally hitting enter will pop you inside the loop, ready for you to start coding:



Here I used the "sout" Live Template to quickly produce the println statement, try this yourself!

This is a really simple example but it highlights how you can use Live Templates to automate the mundane loop construction and launch straight into the important functionality xD

Sidestepping the mundane!



Sometimes a coding task can be pretty repetitive and typing out the same sort of code over and over gets pretty mundane pretty quickly. This is where custom Live Templates can come into play quite nicely!

Let's begin with a simple example, creating a Logger.

Above you can see my custom "logg" Live Template which does three things:

  1. Constructs a Logger variable called log.
  2. Automatically imports org.apache.log4j.Logger
  3. Detects what class I am in and places it in the constructor argument.

Firstly you can see that the code is simply a variable declaration, which is what is inserted when I type "logg" and hit Tab.

Note the fully qualified Logger classpath, this is so that IntelliJ knows to import the log4j Logger. You can see on the bottom right of Figure 3.1 that I have checked "Shorten FQ names" which means the produced code will only have the simple name.

Finally I have used a Template variable "$CLASS_NAME$" to insert the classname. This is done by the "Edit variables" button which pops up the "Edit Template Variables" dialog. You can see I used one of the built in functions to return the classname.

[Read more about Template variables here: http://www.jetbrains.com/idea/webhelp/live-template-variables.html]

Conclusion


Live templates are fantastic and enable developers to spend less time writing scaffolding code and more time focused on the actual business logic and functionality. Although I only really found out about them relatively recently, I have woven it into the way I work and enjoying them ever since :)

For all those that use IntelliJ but haven't tried out Live Templates, I hope I have pipped your interest!

No comments:

Post a Comment