2.3.5. Code Templates

Many times the type of code written is repetitive. (If you don’t agree, you have not yet heard about design patterns.) Eclipse support many kinds of templates. Higher level templates for files/function. Lower level templates for loops and similar constructs. You can even have your own custom templates.

What makes these templates special, their own inherent intelligence.

e.g. The for loop template:

for (${var} = 0; ${var} < ${max}; ++${var}) {
    ${line_selection}${cursor}
}

When a for template is created, you need to enter the var only once. The other two entries (comparison and increment) are filled automatically.

2.3.5.1. Sample — For Loop

Here is an example to introduce for loop over a selection of code.

  1. Select the code block

    ../../../_images/e-code-templates-for-1.png
  2. Press Ctrl + Space. Eclipse would also show a preview of the changes

    ../../../_images/e-code-templates-for-2.png
  3. Eclipse has now put in the template. Note that colored blocks and the cursor mark (Circled for reference)

    ../../../_images/e-code-templates-for-3.png
  4. Type the value for the iterator. (In this case iTest and press TAB

    ../../../_images/e-code-templates-for-4.png
  5. Now you can update value of the MAX limit.

    ../../../_images/e-code-templates-for-5.png