Wasting time is sad, create it instead. True story!

Introduction

After stumbling over a eclipse feature I wish someone had shown me the day i started using it, I feel like my productivity has doubled. I am talking about Code Templates. The fact that this feature was new to me, even after three years of using eclipse, I decided to write an tutorial about it.

Every eclipse user with more than one week experience knows that by typing sysout followed by literally smashing the ctrl + space and Return keys on your keyboard the sysout input magically autocompletes to System.out.println();. But did you know that you are able edit existing templates and even create your own ones? No? Well, you are not alone.

The path to glory

To let the magic happen you need to go to Eclipse -> Preferences -> Java -> Editor -> Templates. There you see a list of all existing templates and are able to edit them. A very useful addition to the sysout template might be

[pastacode lang=”java” manual=”%2F%2F%20TODO%3A%20remove%20syso%20-%3E%20%24%7Benclosing_package%7D.%24%7Benclosing_type%7D” message=”” highlight=”” provider=”manual”/]

Since I tend to forget about all console outputs, this little modification at least puts another instance of reminder for myself to make sure I delete all unnecessary lines before pushing into the git repository.

So my personal sysout template looks like:

[pastacode lang=”java” manual=”%2F%2F%20TODO%3A%20remove%20syso%20-%3E%20%24%7Benclosing_package%7D.%24%7Benclosing_type%7D%0ASystem.out.println(%24%7Bword_selection%7D%24%7B%7D)%3B%24%7Bcursor%7D” message=”” highlight=”” provider=”manual”/]

An example output looks like

[pastacode lang=”java” manual=”%2F%2FTODO%3A%20remove%20syso%20-%3E%20de.langhorst.application.Main%0ASystem.out.println(%22Stuff%20noone%20cares%20about%20anymore%22)%3B” message=”” highlight=”” provider=”manual”/]

Tools made for gods

Next up I will share the list of code templates I use almost on a daily basis. This is no complete list by all means but at least I can provide you with little more input than just the simple sysout example. To create a new template you just press the New… Button and type in the name, the description and the Pattern you want to use. The name  will actually be the part you will be typing in your code to get eclipse to show you the Template Proposals after pressing ctrl + space. Like sysout for System.ou … well you know it by now. The description will then be displayed in the other box. Finally, let’s get into some examples.

Logging

Name: logger

[pastacode lang=”java” manual=”%24%7B%3Aimport(org.slf4j.Logger%2Corg.slf4j.LoggerFactory)%7D%0Aprivate%20static%20final%20Logger%20LOGGER%20%3D%20LoggerFactory.getLogger(%24%7Benclosing_type%7D.class)%3B” message=”Instantiates a Logger and automatically imports all necessary libraries.” highlight=”” provider=”manual”/]

Name: loge

[pastacode lang=”java” manual=”LOGGER.error(%22%24%7Bcursor%7D%22%2C%20e)%3B%0A” message=”For error logging” highlight=”” provider=”manual”/]

Name: logi

[pastacode lang=”java” manual=”LOGGER.info(%22%24%7Bcursor%7D%22)%3B%0A” message=”For information logging” highlight=”” provider=”manual”/]

Null checks

Name: null

[pastacode lang=”java” manual=”if(%20%24%7Bword_selection%7D%20%3D%3D%20null%20)%7B%0A%09%24%7Bcursor%7D%0A%7D%0A” message=”Checks if the selection is null” highlight=”” provider=”manual”/]

Name: nnull

[pastacode lang=”java” manual=”if(%20%24%7Bword_selection%7D%20!%3D%20null%20)%7B%0A%09%24%7Bcursor%7D%0A%7D%0A” message=”Checks if the selection is not null” highlight=”” provider=”manual”/]

Name: nnullEmtpy

[pastacode lang=”java” manual=”if(%20%24%7Bword_selection%7D%20!%3D%20null%20%26%26%20!%24%7Bword_selection%7D.isEmpty())%7B%0A%20%20%20%20%24%7Bcursor%7D%0A%7D” message=”Checks if the selection is not null and not empty” highlight=”” provider=”manual”/]

JavaFx

Name: platr

[pastacode lang=”java” manual=”%24%7B%3Aimport(javafx.application.Platform)%7D%0APlatform.runLater(()-%3E%7B%0A%09%24%7Bcursor%7D%0A%7D)%3B” message=”For Runnable future execution in JavaFX Application thread. And it imports the necessary library” highlight=”” provider=”manual”/]

Patterns

Name: singleton

[pastacode lang=”java” manual=”private%20static%20%24%7Benclosing_type%7D%20instance%3B%0Aprivate%20%24%7Benclosing_type%7D()%7B%7D%0Apublic%20static%20%24%7Benclosing_type%7D%20getInstance()%7B%0A%09if(null%20%3D%3D%20instance)%7B%0A%09%09instance%20%3D%20new%20%24%7Benclosing_type%7D()%3B%0A%09%7D%0A%09return%20instance%3B%0A%7D%0A” message=”Applies the singleton pattern on a class.” highlight=”” provider=”manual”/]

Name: factory

[pastacode lang=”java” manual=”public%20static%20%24%7Breturn_type%7D%20create(%24%7Bswitch_type%7D%20%24%7Bswitch_value%7D)%7B%0A%09switch%20(%24%7Bswitch_value%7D)%20%7B%0A%09%09case%20%24%7Bcursor%7D%3A%0A%09%09%09return%20%3B%0A%09%0A%09%09default%3A%0A%09%09throw%20new%20IllegalArgumentException(%22No%20%24%7Breturn_type%7D%20found%20for%20%24%7Bswitch_value%7D%22)%3B%0A%09%7D%0A%7D” message=”Applies the factory pattern on a class.” highlight=”” provider=”manual”/]

Since I just got to know this feature I am pretty sure this list will expand over the next following weeks. Maybe there will be an follow up article about some more code templates I found or figured out by myself.

Do you know any code templates which helped you become more efficient? Share them in the comments!

Categories: Blog Posts