Are code generators bad?

后端 未结 26 1669
耶瑟儿~
耶瑟儿~ 2021-02-07 07:44

I use MyGeneration along with nHibernate to create the basic POCO objects and XML mapping files. I have heard some people say they think code generators are not a good idea. Wha

相关标签:
26条回答
  • 2021-02-07 08:36

    Code generation might cause you some grief if you like to mix behaviour into your classes. An equally productive alternative might be attributes/annotations and runtime reflection.

    0 讨论(0)
  • 2021-02-07 08:36

    This is one of those highly contentious issues. Personally, I think code generators are really bad due to the unoptimized crap code most of them put out.

    However, the question is really one that only you can answer. In a lot of organizations, development time is more important than project execution speed or even maintainability.

    0 讨论(0)
  • 2021-02-07 08:37

    In certain (not many) cases they are useful. Such as if you want to generate classes based on lookup-type data in the database tables.

    0 讨论(0)
  • 2021-02-07 08:39

    In a recent project we built our own code generator. We generated all the data base stuff, and all the base code for our view and view controller classes. Although the generator took several months to build (mostly because this was the first time we had done this, and we had a couple of false starts) it paid for itself the first time we ran it and generated the basic framework for the whole app in about ten minutes. This was all in Java, but Ruby makes an excellent code-writing language particularly for small, one-off type projects. The best thing was the consistency of the code and the project organization. In addition you kind of have to think the basic framework out ahead of time, which is always good.

    0 讨论(0)
  • 2021-02-07 08:41

    They are like any other tool. Some give beter results than others, but it is up to the user to know when to use them or not. A hammer is a terrible tool if you are trying to screw in a screw.

    0 讨论(0)
  • 2021-02-07 08:42

    The mistake many people make when using code generation is to edit the generated code. If you keep in mind that if you feel like you need to edit the code, you actually need to be editing the code generation tool it's a boon to productivity. If you are constantly fighting the code that gets generated it's going to end up costing productivity.

    The best code generators I've found are those that allow you to edit the templates that generate the code. I really like Codesmith for this reason, because it's template-based and the templates are easily editable. When you find there is a deficiency in the code that gets generated, you just edit the template and regenerate your code and you are forever good after that.

    The other thing that I've found is that a lot of code generators aren't super easy to use with a source control system. The way we've gotten around this is to check in the templates rather than the code and the only thing we check into source control that is generated is a compiled version of the generated code (DLL files, mostly). This saves you a lot of grief because you only have to check in a few DLLs rather than possibly hundreds of generated files.

    0 讨论(0)
提交回复
热议问题