Code template: do not reinvent the wheel

Photo by Luca Bravo on Unsplash

Code template: do not reinvent the wheel

When I am coding, there is always a bunch of lines that I have to search about, or that takes time to type even though I know them, or code that needs to be included but I don't remember what, or code that I just copy/paste from a project without looking if it is still up to date.

Does it also happen to you? I think so!

What to do?

Most of the time, those pieces of code are not the most interesting ones:

  • Write in a file

  • Create a mock

  • Helm chart values!

Even your idea helps you when you want to create a main, a constructor, getter and setters, toString, loops, if, switch, etc...

For example the main method: if you start tipping main you will see this popup:

And if you click (or press enter) then the code is injected:

How to create a template?

In IntelliJ, we have the notion of live templates. Those snippets will be proposed when you start typing the correct abbreviation in the correct context.

To create a snippet, I will first write the code, try to improve it, and see if it works. When I am satisfied, then I select it, open the actions and select Save as Live template:

A new page opens:

Those are all the information you will see on this page:

  1. This is your new template. As you have not defined any abbreviation (shortcut) yet, the default value is "abbreviation"

  2. The context in which your template will show up: you can restrict a template to a type of file. This will help to only display js template in js files, java ones in java files, and so on. Use user context to have your templates always available.

  3. The abbreviation is the text that will trigger the suggestion and the insert of the template. Try to find something that you will remember! Not something like template1, template2,...

  4. The description of the template. Be sure to describe what the template is pasting. For example, in the case of JUnit4 / JUnit 5 templates, it is important to specify it in the description.

  5. The template. It is what will be written in your code.

  6. You will be able to define variables to make your template dynamic!

I will just create a basic template for now with abbreviation demo1

When I type demo1 in the code, I have the suggestion and when I select it, the code is written:

How to create a dynamic template

It is nice to have a template, but it is nicer to have dynamic ones! Let's duplicate demo1 into demo2 and try to make it dynamic.

We can define variables by using $VARIABLE_NAME$

When I start typing demo2 and use the template, I will see that my cursor is set to where I have defined $MESSAGE$ and it is asking me to type something.

Nice! So now we have a variable. But what can we do with it? The edit variable is now available as IntelliJ detects the presence of variables in the templates. I will add a new variable to replace helloThere with a camel case of the string and use this new variable in the println:

Now I will edit the variables to insert $MESSAGE$ value first and make IntelliJ do the camel case for me. When I open the Edit Template Variables, I can see the order of the variables. IntelliJ will use the order used in the template. I can change that thanks to the 2 arrows.

I will move MESSAGE up.

Then I will in VARIABLE Expression use one of the functions available and pass MESSAGE as a parameter

And this is when I use it:

What is the next step?

Having templates is great, sharing templates is better! Imagine that all the "approved code" could be done by templates. All the snippets from API have templates. If you could review templates, updates, and share so you are sure to always use the most up-to-date code.

Let's see how to export your templates. Just go to File > Manage IDE Settings > Export Settings

Then select Live templates and Live templates (schemes) and export the zip to the best place for you:

Now we can import settings! Same path but click import settings instead. Then pick your zip file:

And select what you want to import from those settings:

And restart your IntelliJ to apply changes

Looking a bit into this export

If we open the zip file, we can see the templates folder with all templates per context.

If we dive into the user.xml, I can see my templates demo1 and demo2!

Next step: find out a workflow to share those templates with a broader audience!

Extra tips

When you go to a conference, and the presenter is doing a demo. And you see this kind of code :

And when he types function1, then a huge part of code shows up and you think: Wooooow, where does that come from ??? TEMPLATE

I am sure that he wrote the whole code, then extracted the part he wanted into templates, wrote down the abbreviation and when to play them and VOILA!

In conclusion, use templates!

What is more to say, repetitive code is a source of error. If you start to copy/paste snippets, and the source starts to be old, and you apply it everywhere, and it spreads, what will it look like after some time?

By managing templates, reviewing them, and sharing them, those templates will live and that redundant, unsafe and boring piece of code will become attractive, useful and safe!

Did you find this article valuable?

Support Sébastien NOIROT by becoming a sponsor. Any amount is appreciated!