Are code generators bad?

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

    Code generators can be a boon for productivity, but there are a few things to look for:

    Let you work the way you want to work.

    If you have to bend your non-generated code to fit around the generated code, then you should probably choose a different approach.

    Run as part of your regular build.

    The output should be generated to an intermediates directory, and not be checked in to source control. The input must be checked in to source control, however.

    No install

    Ideally, you check the tool in to source control, too. Making people install things when preparing a new build machine is bad news. For example, if you branch, you want to be able to version the tools with the code.

    If you must, make a single script that will take a clean machine with a copy of the source tree, and configure the machine as required. Fully automated, please.

    No editing output

    You shouldn't have to edit the output. If the output isn't useful enough as-is, then the tool isn't working for you.

    Also, the output should clearly state that it is a generated file & should not be edited.

    Readable output

    The output should be written & formatted well. You want to be able to open the output & read it without a lot of trouble.

    #line

    Many languages support something like a #line directive, which lets you map the contents of the output back to the input, for example when producing compiler error messages or when stepping in the debugger. This can be useful, but it can also be annoying unless done really well, so it's not a requirement.

提交回复
热议问题