开发者

Changing generated code or use inheritance?

开发者 https://www.devze.com 2023-01-06 18:01 出处:网络
I work on an EMF project. One of the design decisions was not to touch t开发者_运维问答he generated code and not to check it in. Instead, whenever something needs to be changed, a sub-class is created

I work on an EMF project. One of the design decisions was not to touch t开发者_运维问答he generated code and not to check it in. Instead, whenever something needs to be changed, a sub-class is created that contains the changes. The framework is flexible enough to deal with this. However, I experience some work overhead.

The design decision was made on the base of bad experiences with other code generation frameworks were re-generation let to problems.

Being new to the project I would like to challenge that design decision but would like to hear the general opinion first. I know that the EMF project team recommends the in-code changes. But what are your experiences? How well does EMF handle manual code changes in the generated code? Did you ever come to a point where you lost manual written code? Did the code ever get into a non-maintainable state?


But what are your experiences?

I've implemented two separate projects, both of which involved models with 50 or more model classes, and in both cases the models evolved throughout the lifetime of the project; i.e. lots of model changes. In both cases, I modified the generated code to typically to implement computed attributes, validation and to customize the editors in various ways.

How well does EMF handle manual code changes in the generated code?

It works well. Occasionally the generator produces code that doesn't compile due to some model change, but the fixes are typically simple; e.g. deleting Java classes/interfaces, dead imports, etc.

Did you ever come to a point where you lost manual written code?

Only very occasionally. Once in a while you forget to remove the "generated" marker comment and your method gets clobbered when you regenerate the model.

(I suppose if it was a major concern you could modify the EMF generator to always backup the source tree before merging in changes.)

I guess that the most irritating thing was that the generated code had to be formatted. Unfortunately, the Eclipse code formatter is pretty crass, but if you manually reformat, your formatting changes get clobbered next time you regenerate. But that's just irritating ... not something that is worth jumping through hoops to avoid.

Did the code ever get into a non-maintainable state?

Nope. Not ever.


Reading consta_a's answer reminds me that I always checked my generated EMF classes into version control. That's the best way to avoid losing hand edits in the long term.


Update in 2018: the 2 EMF projects I was talking about happened before 2008. Things may have changed in the EMF world since then. I have not been keeping track.


I confirm Stephen C answer: in our case we handle about 120 classes in our model, which means 120 interfaces + 120 implementation classes + countless edit classes and regeneration goes quite well (if only we could easily make the generated classes formatted as we want (^_^))

Tip: If you fear losing some hand crafted code, the best is to keep your code in a repository. Every time you do a change, you can easily compare with previous version and see what has changed.

0

精彩评论

暂无评论...
验证码 换一张
取 消