Tuesday, January 8, 2008

Code Templates in Eclipse

Today I read an interesting blog(Developing at the speed of thought) which discussed how little time developers should be coding versus the amount of time they should be designing and thinking about how to code something. I find that the stronger I get as a developer the more time I spend thinking about the design of a process. Its an interesting though considering that when I started developing I used to write code, lots of it, and then turn around and remove quite a bit during refactoring. More often than not these days I find that the majority of code I write needs very little refactoring or altering whatsoever. Several things have caused this change in my abilities. Two that come immediately to mind are coding standards and code templates. The first of which should be self explanatory and the second is partially about using the power of an IDE. I use Eclipse for almost all of my development and therefore I use their code templates quite often. I also have setup quite a few of my own templates and that is what I am going to talk about today.

Code templates in Eclipse are defined in the Eclipse parameters. The templates are per language and it is generally found under the language's editor section in the preferences. I am going to show you a few PHP code templates using PDT. To follow these steps you would need to open your preferences in Eclipse and expand PHP and then click on Templates. This will open the Templates section and allow you to preview, create, edit, or remove templates. As you will see PDT ships with some helpful code templates for most conditional statements and control structures, as well as function and class definitions. When viewing the templates screen the name is what you would type to trigger the template and the context is when it is available to be triggered. Most of the PDT templates will have a context of PHP or HTML therefore you don't have to worry about these template conflicting when you are editing a Java source file or SQL file, for example.

I work with Zend Framework and there are many calls that I have setup code templates for. One of the more frequently used templates that I use is for Zend_Debug::dump(). So let's create a template for this now. Click New and this will open the Edit Template dialog box. Again, the name is the text that you will type to trigger the template. Let's name this template 'dump'. The description will be something like 'Zend Debug dump method call' and its sole purpose is to be descriptive text for identification. The pattern is where you define the actual template and in this case let's enter the pattern like this:

Zend_Debug::dump($$${variable});${cursor}

You might notice the numerous dollars signs in use, Ill come back to that in a minute. First let me tell you that templates can have pattern variables which allow you to autopopulate values such as an array variable, a class name, a variable name, or date and time. Pattern variables are defined by the syntax ${variablename} in the pattern. You are not constrained to the pattern variables found in the insert variable dialog box, you can define your own variable names but they may or may not resolve to anything when you trigger the template. The important thing to note is that when the template is applied to your code you will be able to tab from the first pattern variable to the last and edit it to be whatever you want. Yes, shift tab will take you back from the last to the first as well. Lastly, the double dollar sign resolves to a single dollar sign when the template is applied.

Once you have entered this in the pattern field click ok and then apply in the preferences window. Then go into a php source file and type 'dump' and then click cntl+space to trigger the template to see how it all works. When this template is applied you will receive a selectable list of recently used variable names that are in scope. This is triggered by the pattern variable ${variable}. When you hit the tab key it will take you to the position of the cursor pattern variable where you can quickly hit enter or maybe you want to add a comment at the end of the line. The point is that you don't have to move your hand to the mouse to continue your development.

Here are a few other simple but helpful Zend Framework templates:

Zend_Registry::get('${index}')${method};${cursor}
Zend_Registry::set('${index}', $$${variable});${cursor}

Templating can save you tons of time in the long run, investing the time to set up useful templates will pay off ten fold. =] Enjoy!