Eclipse has an often under-utilized feature called Templates. You’ve probably used the built in templates when Eclipse auto-completes for loops or try-catch blocks, etc… However, you can create your own templates for commonly used chunks of code. This can save you a lot of time, and more importantly can facilitate you writing better quality code.

ATG Eclipse Templates

ATG Eclipse Templates

By making commonly used code blocks available with a couple of keystrokes it is much easier to write best practices code, that otherwise you might shortcut around.

You can view, import, and create new templates by going to Eclipse’s Preferences, then opening Java->Editor->Templates. Each Template has a Name, this is what triggers it for auto-complete so making this simple and unique is good, a Description, a Context, a checkbox to Automatically insert, and then the Pattern which is the code that will get inserted for you. Within the Pattern you have access to a substantial number of variables, such as the current class or method name, variables, method arguments, dates, etc… which allow you do some very useful things.

For ATG coding I’ve created nine Templates for common code blocks.

My templates isBlank and isNotBlank create if blocks based on ATG’s StringUtils.isBlank method, which checks for null, empty, or blank Strings. Checking Strings is something that I do all the time (validating user input, query params, etc…) so being able to do it in a few keystrokes is very useful.

ls.isBlank(${cursor})) {

}

I have three templates for ATG Performance Monitor operations. Adding in ATG Performance Monitor hooks when you’re coding can make diagnosing performance problems later much easier. Using templates means you don’t have to remember the syntax or even type in the class and method names:

PerformanceMonitor.startOperation("${enclosing_type}", "${enclosing_method}");

I also have four templates for the primary ATG logging methods with the respective if blocks needed to prevent unnecessary String concatenations.

if(isLoggingDebug()) {
	logDebug("${enclosing_type}.${enclosing_method}:" + "${cursor}");
}

I’ve exported these nine ATG templates so you can easily import and start to use them.
Eclipse ATG Coding Templates.

What templates have you created?