Friday, December 22, 2006

Visual Studio Templates and Code Snippets are great, but ...

Anyone who has developed any Object Orientated code has re-written the same code patterns many times including creating a class (with comments), implementing getters and setters (or properties in .Net) and creating constructors. This is where templates and code snippets come to the rescue.

Visual Studio (Item and Project) Templates - when you create a new code file in Visual Studio you select from one of many default templates such as a Class, Interface or even a Windows Service. These then use code generation to fill in the blanks (such as a class name) and create your code for you. VS 2005 allows users to create their own custom templates, which is Great! I already have a class template, interface template and exception template customised with default comments, namespaces etc (actually the exception class is entirely generated for me). This is already saving time for myself and my team. Next up a Test Case template.

Visual Studio Snippets - these allow you to insert chunks of code into your file and then fill in the blanks, sort of like code-by-numbers. This is also great and I have created a code snippet for a property that cannot be initialised to null (I use this regularly as it is good design-by-contract programming). See example:

private $type$ $field$;

public $type$ $property$
return $field$;
if (value == null)
throw new ArgumentNullException("value");
$field$ = value;

Microsoft lovingly wraps a whole load of metadata around my code snippet in xml (the code is embedded into an xml CDATA section). This is also great and saves me time re-writing the same code.

And now for the 'but'... ...BUT this is entirely symptomatic of a broken programming paradigm. And in English - Object Orientated languages such as Java, .Net and C++ are flawed, as is the concept of Code Snippets and Templates.

The simplified logic as a Powerpoint bullet argument (hey, its late and I'm getting tired):
  1. In a good programming language we would only need to write the property code ONCE, this point will not be explained as it is self-evident
  2. We would therefore NOT need to insert and customize the same chunks of code in multiple locations
  3. Which means Code Generation and therefore Templates and Code Snippets are redundant!!!

Templates and Snippets suffer from the code generation problem that once created, if the snippet or template are changed the code is NOT. This means that the programmer must then go through all their code manually finding and replacing the generated code.

A thought experiment - what if we were to utilise code generation NOT when we insert the Snippet into our class file, but when we compile the code and then refer to the Snippet instead. Hmmm, well this would be an improvement in that we would then only write our Snippet once, and wouldn't have to manually update any changes. Our new code could look like:

Snippet Property (type = String, field = myString, Property = MyString);

Or something along these lines. :-) The Snippet of code is now effectively a "Template" for a Property. And why not take it a little further, lets make everything a Snippet and make it so that Snippets can include Snippets and the compiler will generate all our code from Snippets. Oh S***, what about the metadata, that's in XML and our Snippets are in C#, how would we generate those, and hey, haven't we just changed our language into some new Snippet language.

The problem with Code Generation - is that we end up creating meta-languages (which is fine) that have a new syntax etc, because the language that the code is turned into is inadequate. A number of code generation frameworks use XML to store the customisable fields and XSLT to then generate the code (into C# or Java or whatever). Makes you wonder really, so what are the options:

  1. Include a decent templating concept into the core language: Jura is an example
  2. Use functional programming - C# is starting to do this, but the starting blocks of classes and properties and interfaces are hard to then abstract out.

And while i'm at it: any programming paradigm (Object Orientated programming) that requires design patterns is flawed. Mind you my hat goes off to the people who have managed to turn the design patterns into libraries using advanced OO techniques such as generics, reflection and code generation (there is a point to some of these techniques)!!!

To round up, code generation, templates, snippets and design patterns are all good concepts starting from a flawed language, namely an Object Orientated language.

Rant over :-)

P.S. just found this conference on Code Generation down the road from me - I might well attend!


At 1:13 am, Blogger Jew Paltz said...

I believe what you are looking for is "Aspect Oriented Programming".


Post a Comment

<< Home