Ayende @ Rahien

Refunds available at head office

Code Generation and the Open Close Principal

Vladan Strigo made a really inspiring comment about code generation in the NH Users list.

I didn't want to use codesmith for that because it would de-OCP-fy me in my future efforts :)

That makes a lot of sense, and manages to touch on what bothers me the most with code gen as an architectural approach. It gives up a very important concept, and that affect a lot of the stuff that is dependant on that.

Comments

Mark Lindell
03/29/2008 04:37 PM by
Mark Lindell

I agree that Passive code gen can be abused.

I particularly don't like passive code gen other than a template starting point to save you some typing. Come on, snippets are passive code gen and no one would deny the benefit of those, right?

On the other hand, I do believe that active codegen can be used appropriately to solve the right problems. Infrastructure that are driven by metadata can benefit from active code generation. (Proxy generation?)

After all, isn't Reflection.Emit a form of active codegen?

I understand your dislike of code gen as an architectural solution when good design would solve the same problem without violating OCP. (yeah you know me -- sorry music joke)

Alon Fliess
03/30/2008 07:43 AM by
Alon Fliess

So JIT is a violation of OCP, it does code generation from IL to assembler. Also any runtime implementation of proxy, interception layer, data accessors, etc. are bad.

I don't think that code generation has anything to do with OCP, its the design of your system that has. Code Generation may be based on the same good OO patterns. I have implemented a system were all my code generators are discovered in runtime. They registered themselves to a pub/sub mechanisms that calls them whenever they need to emit their code. I can remove or add a generator without touching any other generator...

Any generator can derived from existing one and change the code it emits.

This is an OO & OCP based design...

Alon.

Ayende Rahien
03/30/2008 12:35 PM by
Ayende Rahien

You missed this part: "code gen as an architectural approach"

I wasn't talking about emitting IL to do something at runtime. I was talking about code gen as an architectural approach, that is, we will define the application using (DB, XML, pretty pictures) and generate the code from there

Lars Wilhelmsen
04/23/2008 12:10 PM by
Lars Wilhelmsen

Not to be annoying or anything - but isn't there a slight difference between "Principal" & "Principle"?

--larsw

Comments have been closed on this topic.