Are code generators bad?

后端 未结 26 1704
耶瑟儿~
耶瑟儿~ 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:23

    Code generated by a code-generator should not (as a generalisation) be used in a situation where it is subsequently edited by human intervention. Some systems such the wizards on various incarnations of Visual C++ generated code that the programmer was then expected to edit by hand. This was not popular as it required developers to pick apart the generated code, understand it and make modifications. It also meant that the generation process was one shot.

    Generated code should live in separate files from other code in the system and only be generated from the generator. The generated code code should be clearly marked as such to indicate that people shouldn't modify it. I have had occasion to do quite a few code-generation systems of one sort or another and All of the code so generated has something like this in the preamble:

    -- =============================================================
    -- === Foobar Module ===========================================
    -- =============================================================
    --
    --         === THIS IS GENERATED CODE.  DO NOT EDIT. ===
    --
    -- =============================================================
    

    Code Generation in Action is quite a good book on the subject.

提交回复
热议问题