Quite a few years ago back in the days of DOS programming, I came across a tool that did some rudimentary code generation. I was very, very excited about it! To tell you the truth, I never ended up using it very much. It really was a case of codus interruptus. I was excited at first only to be disappointed at how little it did. This is not to impugn the author of the tool. Quite the opposite. Here was some daring individual that had the audacity to write a program to generate dialog boxes, complete with buttons. Remember DOS buttons? Hah! They were text buttons made to look like a button. Anyway, I've never forgotten that first case of code generation. Back in those days, it was not called code generation but CASE, an acronymn that stands for computer-aided software development.
One of the exciting things about Delphi for me was it really did do some code generation. It setup a project and if you put a button on a form and clicked on it, it put an empty event-handler on the form for you. It put the method declaration in the class so all you had to do was write the code for the event handler. This was very pivotal time because the IDEs became part of the development process.
Fastf forward a few years (perhaps 10 or so). I read a book awhile back called Coder to Developer by Mike Gunderloy. He mentions code generation as something we should be doi'ng. He described a tool that enabled you to do, among other things, generate stored procedures for MS SQL tables. All that CRUD stuff that is not hard, is boring, takes time, and is prone to error. The technique I always used prior to reading this book was what I call cut-and-paste code generation. Still way faster than writing it out completely by hand but any time you are using cut-and-paste, you will make errors and that's just a fact of life. So add time to debug it all. I had just read this chapter and a month or so prior to this, I had done a project that had legions of tables and I spent a day or so doing cut-and-paste code generation to write these stored procs. So I did a simple test. I took a tool I downloaded and generated these same stored procs in about 5 minutes - that included 4:30 to learn how to use the tool to do this. I became a convert right then and there.
Since then, I have played with various tools and have found a few that I use quite a bit for .net programming - Code Smith and MyGeneration. Both are excellent and have their strengths and weaknesses.
I've been eagerly learning more and more about code generation and I started reading Code Generation in Microsoft.NET by Kathleen Dollard. This book is another pivotal moment for me as a developer. The method Kathleen advocates is using XML and XSLT to generate code. Say what? yes, using XML and XSLT to generate code.
I am going to leave it there because if I open up this can of worms, I'll be writing a book I'm sure and I have code to generate. So stay tuned!
Comments