FDT – Templates are your friend!
Jason Nussbaum suggests on his blog the use of a standard naming convention in all your get and set functions to save you a few seconds thinking time. This is all good, but how about taking it one step further by ensuring you always use standard names while also saving some typing? If you’re using FDT I just might be able to help…
A nifty feature of FDT that not many people seem to know about is Templates. Templates allow you to easily insert and customise chunks of code, anything from one line to whole classes, in a few keystrokes. Being a lazy git at heart I have a lot of love for this feature, and use it often. The best thing is you can also easily make your own Templates to automate your regular tasks, and in this post I’ll show you to add a simple Template to write a pair of ‘get’ & ’set’ data access functions in a record time…
Getting Stuck In
If you don’t know already, FDT comes with a set of Templates built in. To see one in action, create a new class and hit CTRL+A, type ’sing’ and hit CTRL+SPACE…. BAM, an entire singleton class ready for your use, how cool?! Some other good ones to try out are ‘fore’ and ‘fori’, you can also just hit CTRL+SPACE on it’s own to pick from a list (the entries with a little page by them are Templates)
Modding the Templates
To view the existing templates and add your own, you have to go to the slightly burried property panel under Window->Preferences->FDT->Editor->Templates
![]()
You can click around to take a look at the ones already there to get an idea of how they work. Basically they are much like most template engines, with a bunch of boiler-plate text and some variables to be replaced, in this case enclosed in ‘${‘ & ‘}’, e.g. ${cursor}, ${user} etc. To see the standard predefined variables you can insert hit the ‘insert variable’ button for a list. Alternatively you can add your own vars and these will be requested from the user when the template is run.
By now you’ve probably got a good idea of how to make your own template functions (it’s not too hard once you’ve found where to do it!), so before you get bored of reading me ramble on and go off to watch youTube, here’s some code…
//get & set ${param} public function get ${param}() : ${type} { return _${param}; } public function set ${param}( value:${type} ) { _${param} = value; }
Just hit ‘New’, enter ‘getset’ as the name and past the above into the ‘pattern’ box then OK.
If you now type ‘getset’ and hit CTRL+SPACE you should just need to enter the variable name and type and job done.
Another nice side effect of this is it assumes (and kind of enforces) the use of an underscore prefix on all private local members, although you can of course change that in the template if you want.
So there you go, get & set functions in record time thanks to the mighty Templates. I use this at least half a dozen times a day when coding, saving myself whole seconds in the process, and although I can’t guarantee your productivity breakthroughs will be as astounding I encourage you to give it a try
Feel free to add any of your own handy templates to the comments.
Right you are. I’ve done exactly the same thing for a while now.
Another handing FDT tip and general piece of lovliness is the use of Event Metadata.
If your class dispatches an event, then at the top of the class between the imports and the class declaration you add metadata for each event
[Event("onDataParsed")] for example
then whenever you reference an instance of the class and type myInstance.addEventListener you can control+space after the opening bracket and lo and behold all the events are there. This means you don’t have to remember, or open up the class to refresh your memory of the event type string, without a nasty static variable string for no reason. Every little helps
This is a terrific feature in Eclipse, but what about users of Flex 2 Builder? There is currently no access to the built-in class templates, but is it possible to use the kind of templates you mention here without going back to plain Eclipse?