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.
[fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”]
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.
[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][java]
ls.isBlank(${cursor})) {
}
[/java]
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:
[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][java]
PerformanceMonitor.startOperation(“${enclosing_type}”, “${enclosing_method}”);
[/java]
I also have four templates for the primary ATG logging methods with the respective if blocks needed to prevent unnecessary String concatenations.
[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][java]
if(isLoggingDebug()) {
logDebug(“${enclosing_type}.${enclosing_method}:” + “${cursor}”);
}
[/java]
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?
[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]
Leave a Reply