Wednesday, March 23, 2005

Code generation and partial classes

The world isn't a level playing field.

If you are a vendor providing tools with code generation in Java(and probably some other languages too) then the problem of regenerating code without overwriting the users changes is something you have to take into account. A lot of tools solve the problem by putting markers in comments around the parts of the code that they are interested in and allow changes only to parts outside the markers. If the user wants he can remove the markers and the tool will skip regeneration of those parts. Overall every cursed tool developer ends up writing his own thing and the markers make the code less readable. One way to work around is probably by inheriting the generated class and putting all user added code in the subclass. However this can have performance implications and can also lead to encapsulation breakage.

However, if you are Microsoft where you control both the programming languages and the tools you end up solving the problem by introducing partial types/classes in the language specification, thus taking away the need for the user to edit the generated file at all. With partial classes the tool is happy emitting code in one file and the user is happy coding away in another.

So ye SUN and JCP gods please shower us with your partial blessings :)

This post also has similar comments.

No comments: