Chris's Weblog
Chris's log about software development and other stuff
Navigation for Chris's Weblog - Is Code Generation a Code Smell?
Content
Sidebar
Footer
August 4, 2006
@ 06:05 AM
Comments [0]
Is Code Generation a Code Smell?
Last night I was working on something and suddenly found myself needing about 30 really simple ActiveRecord classes. I figured it was about time to stop writing these manually each time.
Looking at the
CastleProject.org
site, and seeing that the status of ActiveRecordGenerator was frozen, I briefly considered writing my own little tool (or just modifying ActiveRecordGenerator as needed) to do the job. After another 10 minutes, I finally broke down and made a CodeSmith template (we've got CodeSmith, but for some subconscious reason I keep avoiding using it.)
This was fairly straightforward, and after 20 minutes or so, I had the template I wanted, and started generating classes. After another 20 minutes I had added some things I missed the first time and everything was great.
After completing this, I looked at what I had done, and found myself thinking: this is alot of cruft.
Let me mention that all 30 classes had basically the same set of properties. I briefly considered writing a generic class, but didn't think there would be a way to get the name of the class into the '[ActiveRecord("MyTableNameIs")]' attribute. That, and a small handful of special rules, were the only things different about these classes.
I was thinking: A lisp-like macro would have been really useful. Why don't we have lisp macros in OO languages like C#? I've always fealt (but not agreed) that it's because OO leads you to composition instead of code generation. I could have written one base class with the properties I wanted, then 30 derived classes with the right names and attributes. That is still too much typing, though, and I would have used code generation anyway. I chose not to relate these classes, as they are not really related in this domain anyway, only functionally and structurally related.
I am not sure what the best answer to this type of design problem is, but I know I haven't found it. Code Generation is a jack-hammer in the face of the problem of code duplication caused by imperfect design. It gets me through the barriers, but the barriers were trying to tell me something.
Frameworks and tools (like ActiveRecordGenerator, or even better -
Streamlined
) that generate applications based on meta data are becoming very popular. Are these tools just reactions to the lack of good DSL tools or lisp-macro-like facilities - i.e., why am I writing my C# application in Code Smith templates and not in C#?
Categories:
design
|
architecture
|
refactoring
|
agile
Related posts:
How to Stop Taking Agile So Seriously and Have a Laugh
Error Handling, High Speed Car Chases, and Zen
'Sup
Getting rid of IoC
WCF dotnetusers.org
« Avalon is the J2EE of GUI APIs?
|
Home
|
Excited About SQL Server Everywhere Edit... »
Comments are closed.
RSS/Subscribe
Search
Links
friendfeed
my del.icio.us
What am I doing?
Categories
411 (2)
admin (2)
agile (3)
architecture (2)
article (1)
bdd (3)
bs (8)
career (2)
CodeCamp (7)
CodeRush (2)
community (2)
CrazyIdea (2)
database (8)
debugging (2)
design (4)
event (2)
humor (6)
i18n (1)
installer (1)
library (2)
lifehack (1)
links (4)
linq (1)
music (1)
nhibernate (3)
patterns (1)
poker (2)
politics (5)
PowerShell (3)
refactoring (2)
scripting (5)
sqle (5)
tdd (9)
tool (22)
ug (3)
wcf (1)
web (3)
wf (1)
wix (1)
wpf (2)
BlogRoll
Blogs
.NET Undocumented
Ayende @ Blog
brains-N-brawn.com
Chris Donnan : Programming - Brooklyn Style
Coding Horror
ComputerZen.com - Scott Hanselman
DevHawk
Eric.Weblog()
Geek Noise
hamilton *hammett* verissimo
It's the Tests
Jason Haley
Jean-Paul Boodhoo
Lambda the Ultimate - Programming Languages Weblog
Less is better
Mark Miller
Marquee de Sells: Chris's insight outlet
Martin Fowler's Bliki
Miguel de Icaza
Sam Gentile
Scott Bellware [MVP]
secretGeek
Tales from a Trading Desk
Wall Street Programmer
you've been HAACKED
Archives
All dates
All Posts
August, 2008 (1)
July, 2008 (5)
May, 2008 (2)
April, 2008 (5)
March, 2008 (3)
February, 2008 (9)
January, 2008 (1)
December, 2007 (4)
November, 2007 (16)
October, 2007 (9)
September, 2007 (6)
August, 2007 (8)
July, 2007 (5)
June, 2007 (13)
May, 2007 (11)
January, 2007 (2)
December, 2006 (2)
October, 2006 (8)
September, 2006 (29)
August, 2006 (10)
July, 2006 (5)
Contact the Author
Yahoo IM: booleanchoice
MSN: cbilson@pobox.com
Admin
Sign In