Here is an interesting article by Martin Fowler on the subject of reflection vs. code generation.
Here is a nice quote to sum it up (with which I couldn’t agree more).
Active code generation gives us all the benefits of reflection, but none of its limitations. Reflection will still be used, but only during the code generation process, and not during runtime.
Here is another endorsement he gives after many examples and justifications.
The benefits of reflection are obvious. When coupled with code generation it becomes an invaluable and, more importantly, a safe tool. There is often no other way to escape many seemingly redundant tasks. As for code generation: the more I work with it, the more I like it. With every refactoring and increase in functionality, the code becomes clearer and more understandable. However, runtime reflection has the opposite effect. The more I increase its functionality, the more it increases in complexity. So, in the future, if you feel you need to conquer a complicated problem using reflection, just remember one rule: don’t do it at runtime.
I couldn’t agree more, except I would go one step further and consider “code generation” just another form of transformation at compile time. At least that is what I’m hoping to accomplish with MetaSharp.
You must log in to post a comment.