Code Generation

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.

7 comment(s)
Mike Partain
Code generator for a DAL? It's called XPO - duh

Now a code generator for the tedious CReate, Update, Delete aka CRUD UIs, both web and winform (and WinFX) would be something I would use.

Does anyone have  competing product to iron speed? Seems like a good market to me.
15 April, 2006
First let me say that I too use both tools and both tools are fine. However I wouldn't compare them at all since they are very different. Now, let me comment couple of your statements where I disagree:

"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."

I disagree here. While CodeRush has an advantage in simplicity and speed it shouldn't be used for complex structures such as typed collecitons. Why? The reason is very simple. What if you decide to modify your typed collection at certain point in time? CodeRush will give up while CodeSmith requires only modified template, few clicks and there you go.

About CodeSmith and code-behind files. This is only one of the way. The other two ways are either use C#/VB.NET code directly in template or (IMO the best if you are creating reusable code) create a normall class library assembly.

"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."

Very true. :-)
16 April, 2006
Gary Gibbons
Dear Mr. Bucknall; as CTO can you tell me why a customer who purchased a subscription (DXExperience Professional -- it lacks the "priority support" feature -- ) I can't at the least recieve some acknowledgement about the bug report I have been attempting to communicate to support staff?
I submitted it a week ago, recieved one response asking for more information; I uploaded 2 files, edited the report 3 different times to further explain the issue and have yet to receive a response beyond the first!

I know there is no priority support, but do you think as a paying customer I may deserve a bit more than nothing?
You all are busy?  I am too.  Costs you money to respond? It costs me money when you don't!

What's up with this stuff?  It isn't breaking me, but it certainly isn't making me into a loyal customer either.
17 April, 2006
Ben
What we really want is lisp like macros...

If you need code generation tools you have stumbled upon a limitation in the language. Code should be generated from function and macro calls I think.
2 May, 2006
James Dooley
Take a look at MyGeneration from MyGenerationSoftware.com, does everything that CodeSmith does and it is FREE!
14 May, 2006
Jimmy Glass
Interesting.. I've been researching .Net Application frameworks. I really liked what I read about .NetTiers, which seems to be the flagship framework for CodeSmith. How does it compare to eXpressApp Framework? Are there code gen templates with CodeSmith for eXpressApp? I like the SOA of .NetTiers, but.. It falls short at the UI Layer. It seems eXpressApp is rather completed, but.. I hate writing monotinous code for business objects.
18 December, 2006

What about the performance using nettiers assemblies with ASPxGRidView?. Some samples already exists?... One friend is using NetTiers with DevExpress but says that is bad idea combine both technologies. Problems with performance.

Please.. could someone give me one feedback or one link to one web site that uses devexpress?... I need to show him that DevExpress is the best choice.

Thank you in advance.

25 August, 2011

Please login or register to post comments.