Code Generation

ctodx
13 April 2006

I was thinking this morning about code generation. Yes, I know, weird, but it's part of my job to consider our markets and where we're going and whatnot.

(Aside: I always used to spell weird "wierd" and the Word spellcheck would simply squiggly-underline it for me in red. I could never remember which way round the i and the e went, until one day I read somewhere that the way to remember it is that the word weird is spelt weird. Geddit? In other words, weird is spelt in a way you wouldn't expect; that is, the opposite of "i before e except after c". Problem solved; normal service is now being resumed.)

I own both CodeRush and CodeSmith. CodeRush is our add-in to Visual Studio that helps you write code faster by mapping simple key sequences in Visual Studio to code templates, whereas CodeSmith is a program that converts ASP.NET-alike templates to gobs of source code.

As an example of CodeSmith's power consider the .netTiers templates. With this set of templates you can point CodeSmith at your database and it will autogenerate a complete data access layer and business object layer for your schema that uses the Microsoft Enterprise Library. Get your schema right (no small undertaking, that!), and blam in less than 15 minutes you have a Visual Studio solution that provides two tiers of your application. Phenomenal.

Of course, you can't (or, rather, shouldn't) change this code since the next time you change the database schema you'll want to regenerate all the code again. Still it's a great bonus to productivity (and writing a bazillion data access and business objects by hand is enough to make you go crazy).

CodeRush, on the other hand, acts in a more localized scope, as you are writing code in the editor. There it gives you immediate feedback. You want a new class? Type c, space, and fill in the name of the class. A method returning a bool? m, b, space, and then fill in the name again. Of course, you don't have to stop there. The coll template produces a complete typed collection for a particular type. Properly commented, in a region and ready to go. Other templates produce an equivalent chunk of code there and then.

There's a template for CodeSmith that produces a typed collection too, but it's much more convenient to use the CodeRush one because it's right there in the editor when you realize you need one.

You could even work with both CodeRush and CodeSmith; they are very complementary. The hard thing about using CodeSmith is that you have to work out the template first. Once you have the template, it's simplicity itself to apply it ad infinitum at practically no cost. To create the template, the easiest thing is to write the code you want for one exemplar object in your problem space. Use Visual Studio, CodeRush, and Refactor! to get the best designed, most efficient and maintainable code. Make sure you write and use all the verification code and pre-conditions and invariants. Compile and test to your heart's content. Take your time, make it right. Once it's ready and it works the way you want it, convert it into a template, and using CodeSmith you get the support for all your other objects for free.

CodeSmith evens uses "code-behind" source files written in C# so that you can provide extensibility methods to do things standard CodeSmith cannot do. Using CodeSmith Studio to write those can be a little awkward (there's nothing like Intellisense), so I tend to use Visual Studio with CodeRush. (It's a shame that CodeSmith's integration with Visual Studio doesn't extend to the editor: when you try and edit a CodeSmith template in Visual Studio, it launches CodeSmith Studio.)

So all in all, if you're into code generation (and for any large project, you should be), CodeSmith and CodeRush make a great pair.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.