代码生成器不好吗? [英] Are code generators bad?

查看:167
本文介绍了代码生成器不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MyGeneration与nHibernate一起使用来创建基本的POCO对象和XML映射文件.我听到有人说他们认为代码生成器不是一个好主意.目前最好的想法是什么?当生成数千行无法理解的代码时,仅仅是代码生成不好吗?

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. What is the current best thinking? Is it just that code generation is bad when it generates thousands of lines of not understandable code?

推荐答案

由代码生成器生成的代码(作为概括)不应在随后由人为干预进行编辑的情况下使用.诸如Visual C ++的各种化身上的向导之类的某些系统生成了代码,然后要求程序员手动对其进行编辑.这并不流行,因为它要求开发人员将生成的代码分解,理解并进行修改.这也意味着生成过程只是一枪而已.

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.

生成的代码应与系统中的其他代码存放在单独的文件中,并且只能从生成器生成.生成的代码应该清楚地标出,以表示人们不应该对其进行修改.我曾经有机会做过很多不同种类的代码生成系统,并且这样生成的代码 All 在前言中都有这样的内容:

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.

这篇关于代码生成器不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆