微代码生成被认为是有害的吗?

时间:2022-10-20 19:27:44

I recently wrote a small tool to generate a class for each tier I hand write for the boring "forms over data" work where I spend almost 90% of my time (depressing I know) ... more on this as the economy improves ;)

我最近编写了一个小工具来为每个层级生成一个类,我为无聊的“数据形式”工作编写,我花费了近90%的时间(令人沮丧,我知道)...随着经济的改善,更多关于这一点; )

My question is this - will using this tool instead of hand typing all this code from day to day actually hurt me as a developer? I feel like I will always be making changes to this tool and thus I "should" stay on top of the patterns used/ choices made/ etc... but some small part of me feels like I might lose my edge ... am I wrong?

我的问题是 - 将使用这个工具而不是手工打字所有这些代码日复一日实际上伤害了我作为开发人员?我觉得我将永远改变这个工具,因此我“应该”保持在使用的模式/选择/等等...但我的一小部分感觉我可能会失去我的优势......我错了?

16 个解决方案

#1


If the tool can spit the code out without thought, then it probably saves you lots of thoughtless typing.

如果该工具可以不假思索地吐出代码,那么它可能会为您节省大量的轻率打字。

Writing the tool in the first place requires thinking, so I'd guess you'd be more "on the edge" maintaining and writing the tool.

首先编写工具需要思考,所以我猜你会更加“处于边缘”维护和编写工具。

#2


That's good! Of course writing a tool to do all the job for you is impossible and wrong.

非常好!当然,编写一个工具来为你完成所有工作是不可能的,也是错误的。

But automating repeatable tasks is always good - and sometimes writing specific types of code is repeatable.

但自动化可重复任务总是好的 - 有时编写特定类型的代码是可重复的。

It is even encouraged in the "Pragmatic Programmer" book.

“实用程序员”一书中甚至鼓励它。

Make sure that in the source control you have checked in a code generator and not its output (unless you have to modify the code later by hand)!

确保在源代码管理器中检查了代码生成器而不是其输出(除非您以后必须手动修改代码)!

#3


You are most definitely not wrong. I use code generators anywhere I can - I currently use CodeSmith to create my DAO's by looking at the database.

你绝对没错。我在任何地方使用代码生成器 - 我目前使用CodeSmith通过查看数据库来创建我的DAO。

What edge are you afraid of losing? In my mind going to code generation is actually giving you an edge.

你害怕失去什么优势?在我看来,代码生成实际上是给你一个优势。

#4


Larry Wall (of Perl fame) describes the three cardinal virtues of programming as Laziness, Impatience, and Hubris.

Larry Wall(Perl成名)描述了编程的三个主要优点:Laziness,Impatience和Hubris。

Congratulations! You have shown good laziness, in that you have identified some work you can pass off to an automated process and done so. (Bad laziness leads to cutting corners, procrastination, and generally postponing rather than eliminating work.) If you can successfully palm off some work onto another program, you are spending less time on annoying triviality and more on accomplishing things and learning.

恭喜!你已经表现出很好的懒惰,因为你已经确定了一些可以传递给自动化过程的工作并且这样做了。 (糟糕的懒惰导致偷工减料,拖延,并且通常推迟而不是消除工作。)如果你能成功地将一些工作转移到另一个项目上,你就会花更少的时间来讨厌烦人的事情,而更多的时间来完成事情和学习。

#5


Generate what you can. Code generation is one of the best tools I've picked up over the last 2 or 3 years. Typing the same code over and over (or copy and pasting it) is prone to error.

生成你能做的。代码生成是我在过去2或3年内学到的最好的工具之一。一遍又一遍地键入相同的代码(或复制并粘贴它)很容易出错。

#6


Spending less time doing something by having something/someone else do it, and more time researching better ways to do it will generally lead to doing it in a better way.

通过让别人/别人做某事来减少做某事的时间,而花更多时间研究更好的方法,通常会以更好的方式做到这一点。

This doesn't have to just apply to programming....

这不一定只适用于编程....

#7


Your code generator (at least in principle - I haven't looked at it myself) is The Right Thing, at least as far as it goes.

你的代码生成器(至少在原则上 - 我自己没有看过它)是正确的事,至少就它而言。

The next step would be to see whether you can, instead of generating all this redundant code, create a base class whose functionality matches the generated code and then derive your application code from it. Using inheritance rather than generation will allow you to benefit from improvements without needing to re-run the generator on all your projects. Perhaps more importantly, if you customize the generated code, the customizations would be lost if you re-run the generator, but customizations in a derived class will be preserved when the base class is changed.

下一步是查看是否可以创建一个基本类,而不是生成所有这些冗余代码,该基类的功能与生成的代码匹配,然后从中派生应用程序代码。使用继承而不是生成将允许您从改进中受益,而无需在所有项目上重新运行生成器。也许更重要的是,如果您自定义生成的代码,如果重新运行生成器,自定义将丢失,但在更改基类时将保留派生类中的自定义。

#8


No. Why do you think IDE's are so popular. Imagine if all the people who use Visual Studio had to programmatically create the GUI's without help from the IDE, it'd be terrible. I would be willing to bet most people who use VisualStudio won't know how to manualy create the forms they're creating in the IDE. But there's nothing wrong with that.

不。为什么你认为IDE如此受欢迎。想象一下,如果所有使用Visual Studio的人都必须在没有IDE帮助的情况下以编程方式创建GUI,那就太糟糕了。我愿意打赌大多数使用VisualStudio的人都不会知道如何手动创建他们在IDE中创建的表单。但这并没有错。

#9


I believe in code generation wherever possible to remove the rote tasks of programming. You will not lose your edge, you will probably become a better programmer because you will spend more time working on the important and interesting stuff.

我相信代码生成尽可能消除编程的死记硬背任务。你不会失去优势,你可能会成为一个更好的程序员,因为你会花更多的时间来处理重要而有趣的事情。

BTW, your tool sounds interesting. Have you released it anywhere?

顺便说一句,你的工具听起来很有趣你有没有发布它?

#10


Code generation is fine as long as you understand what you are generating. Physicists use calculators because they understand the formulas they are automating and realize that their precious time is better spent on important tasks.

只要您了解所生成的内容,代码生成就可以了。物理学家使用计算器,因为他们了解自动化的公式,并意识到他们的宝贵时间更好地花在重要任务上。

#11


Code generation is one of those invaluable DO:s that The Pragmatic Programmer advocates. I truly recommend that book. Here's a Pragmatic Programmer quick ref.

代码生成是实用程序员所倡导的那些宝贵的DO之一。我真的推荐那本书。这是一个实用的程序员快速参考。

#12


Its almost hypocritical not to code generate. Here we are automating all of these tasks that were traditionally done by hand... and yet many of us still hand crank all of our code, even if it can be easily generated.

它几乎虚伪不要代码生成。在这里,我们自动执行传统上手工完成的所有这些任务......但是我们中的许多人仍然会动摇我们所有的代码,即使它很容易生成。

#13


My only experience with code generation is the macros of Common Lisp. They are used all the time. Everything that automats repetitive tasks is beneficial; that is what programming is about.

我唯一的代码生成经验是Common Lisp的宏。它们一直在使用。使重复性任务自动化的一切都是有益的;这就是编程的意义所在。

Read the story of Mac.

阅读Mac的故事。

#14


Imagine that each time you made a change to the tool and regenerated your code, that you made that design change by hand on all of your modules.

想象一下,每次您对工具进行更改并重新生成代码时,您都会在所有模块上手动更改设计。

Since I've started generating code and gotten up to speed, I've found that I rarely get bugs in the generated code.

由于我已经开始生成代码并且加快了速度,我发现我很少在生成的代码中遇到错误。

#15


I find that writing code gen does help me learn the nuances of good architecture. You start seeing common patterns as opposed to a narrow view of your design. That said, don't use code gen as a substitute for good object-oriented code, and don't love your code gen so much you ignore new technologies. For example, if you're in .NET and are writing code-gen for data access, you'd better have a good excuse for not using Linq to SQL or NHibernate. Similarly, Dynamic Data can help in many forms-on-data scenarios. So, my advice: spike new stuff and code gen as needed.

我发现编写代码确实帮助我学习了良好架构的细微差别。您开始看到常见的图案,而不是狭隘的设计视图。也就是说,不要使用代码生成代替良好的面向对象的代码,并且不要太喜欢你的代码,否则你会忽略新技术。例如,如果你在.NET中并且正在编写用于数据访问的代码,那么你最好有一个不使用Linq to SQL或NHibernate的借口。同样,动态数据可以在许多数据形式场景中提供帮助。所以,我的建议是:根据需要加入新东西和代码。

#16


My 2cents on code gen is that it is also critical for use in refactoring. I have found that partial classes and a good file comparison utility (Araxis or BeyondCompare) are essential.

我对代码生成的看法是,它在重构中的使用也很重要。我发现部分类和良好的文件比较实用程序(Araxis或BeyondCompare)是必不可少的。

Keep your generated code in one file and the custom Tweaks you made for that class in another file.

将生成的代码保存在一个文件中,并将您为该类创建的自定义调整保存在另一个文件中。

This practice will allow you to make those comprehensive framework changes implemented quickly and will also help you move to a new paradigm while easily being able to save your custom logic.

这种做法将允许您快速实施这些全面的框架更改,并且还可以帮助您转换到新的范例,同时轻松地保存您的自定义逻辑。

CodeSmith FTW!

While build servers are great to make sure all your code compiles, it doesn't address the differences in signatures with your stored procs or the like. If you routinely run the code gen you can more easily identify when those changes occur. A unit test will tell you the SP is wrong, code gen will tell you how to make it right.

虽然构建服务器非常适合确保所有代码编译,但它并没有解决签名与存储过程等的差异。如果您经常运行代码生成,则可以更轻松地识别何时发生这些更改。单元测试会告诉你SP是错误的,代码将告诉你如何使它正确。

#1


If the tool can spit the code out without thought, then it probably saves you lots of thoughtless typing.

如果该工具可以不假思索地吐出代码,那么它可能会为您节省大量的轻率打字。

Writing the tool in the first place requires thinking, so I'd guess you'd be more "on the edge" maintaining and writing the tool.

首先编写工具需要思考,所以我猜你会更加“处于边缘”维护和编写工具。

#2


That's good! Of course writing a tool to do all the job for you is impossible and wrong.

非常好!当然,编写一个工具来为你完成所有工作是不可能的,也是错误的。

But automating repeatable tasks is always good - and sometimes writing specific types of code is repeatable.

但自动化可重复任务总是好的 - 有时编写特定类型的代码是可重复的。

It is even encouraged in the "Pragmatic Programmer" book.

“实用程序员”一书中甚至鼓励它。

Make sure that in the source control you have checked in a code generator and not its output (unless you have to modify the code later by hand)!

确保在源代码管理器中检查了代码生成器而不是其输出(除非您以后必须手动修改代码)!

#3


You are most definitely not wrong. I use code generators anywhere I can - I currently use CodeSmith to create my DAO's by looking at the database.

你绝对没错。我在任何地方使用代码生成器 - 我目前使用CodeSmith通过查看数据库来创建我的DAO。

What edge are you afraid of losing? In my mind going to code generation is actually giving you an edge.

你害怕失去什么优势?在我看来,代码生成实际上是给你一个优势。

#4


Larry Wall (of Perl fame) describes the three cardinal virtues of programming as Laziness, Impatience, and Hubris.

Larry Wall(Perl成名)描述了编程的三个主要优点:Laziness,Impatience和Hubris。

Congratulations! You have shown good laziness, in that you have identified some work you can pass off to an automated process and done so. (Bad laziness leads to cutting corners, procrastination, and generally postponing rather than eliminating work.) If you can successfully palm off some work onto another program, you are spending less time on annoying triviality and more on accomplishing things and learning.

恭喜!你已经表现出很好的懒惰,因为你已经确定了一些可以传递给自动化过程的工作并且这样做了。 (糟糕的懒惰导致偷工减料,拖延,并且通常推迟而不是消除工作。)如果你能成功地将一些工作转移到另一个项目上,你就会花更少的时间来讨厌烦人的事情,而更多的时间来完成事情和学习。

#5


Generate what you can. Code generation is one of the best tools I've picked up over the last 2 or 3 years. Typing the same code over and over (or copy and pasting it) is prone to error.

生成你能做的。代码生成是我在过去2或3年内学到的最好的工具之一。一遍又一遍地键入相同的代码(或复制并粘贴它)很容易出错。

#6


Spending less time doing something by having something/someone else do it, and more time researching better ways to do it will generally lead to doing it in a better way.

通过让别人/别人做某事来减少做某事的时间,而花更多时间研究更好的方法,通常会以更好的方式做到这一点。

This doesn't have to just apply to programming....

这不一定只适用于编程....

#7


Your code generator (at least in principle - I haven't looked at it myself) is The Right Thing, at least as far as it goes.

你的代码生成器(至少在原则上 - 我自己没有看过它)是正确的事,至少就它而言。

The next step would be to see whether you can, instead of generating all this redundant code, create a base class whose functionality matches the generated code and then derive your application code from it. Using inheritance rather than generation will allow you to benefit from improvements without needing to re-run the generator on all your projects. Perhaps more importantly, if you customize the generated code, the customizations would be lost if you re-run the generator, but customizations in a derived class will be preserved when the base class is changed.

下一步是查看是否可以创建一个基本类,而不是生成所有这些冗余代码,该基类的功能与生成的代码匹配,然后从中派生应用程序代码。使用继承而不是生成将允许您从改进中受益,而无需在所有项目上重新运行生成器。也许更重要的是,如果您自定义生成的代码,如果重新运行生成器,自定义将丢失,但在更改基类时将保留派生类中的自定义。

#8


No. Why do you think IDE's are so popular. Imagine if all the people who use Visual Studio had to programmatically create the GUI's without help from the IDE, it'd be terrible. I would be willing to bet most people who use VisualStudio won't know how to manualy create the forms they're creating in the IDE. But there's nothing wrong with that.

不。为什么你认为IDE如此受欢迎。想象一下,如果所有使用Visual Studio的人都必须在没有IDE帮助的情况下以编程方式创建GUI,那就太糟糕了。我愿意打赌大多数使用VisualStudio的人都不会知道如何手动创建他们在IDE中创建的表单。但这并没有错。

#9


I believe in code generation wherever possible to remove the rote tasks of programming. You will not lose your edge, you will probably become a better programmer because you will spend more time working on the important and interesting stuff.

我相信代码生成尽可能消除编程的死记硬背任务。你不会失去优势,你可能会成为一个更好的程序员,因为你会花更多的时间来处理重要而有趣的事情。

BTW, your tool sounds interesting. Have you released it anywhere?

顺便说一句,你的工具听起来很有趣你有没有发布它?

#10


Code generation is fine as long as you understand what you are generating. Physicists use calculators because they understand the formulas they are automating and realize that their precious time is better spent on important tasks.

只要您了解所生成的内容,代码生成就可以了。物理学家使用计算器,因为他们了解自动化的公式,并意识到他们的宝贵时间更好地花在重要任务上。

#11


Code generation is one of those invaluable DO:s that The Pragmatic Programmer advocates. I truly recommend that book. Here's a Pragmatic Programmer quick ref.

代码生成是实用程序员所倡导的那些宝贵的DO之一。我真的推荐那本书。这是一个实用的程序员快速参考。

#12


Its almost hypocritical not to code generate. Here we are automating all of these tasks that were traditionally done by hand... and yet many of us still hand crank all of our code, even if it can be easily generated.

它几乎虚伪不要代码生成。在这里,我们自动执行传统上手工完成的所有这些任务......但是我们中的许多人仍然会动摇我们所有的代码,即使它很容易生成。

#13


My only experience with code generation is the macros of Common Lisp. They are used all the time. Everything that automats repetitive tasks is beneficial; that is what programming is about.

我唯一的代码生成经验是Common Lisp的宏。它们一直在使用。使重复性任务自动化的一切都是有益的;这就是编程的意义所在。

Read the story of Mac.

阅读Mac的故事。

#14


Imagine that each time you made a change to the tool and regenerated your code, that you made that design change by hand on all of your modules.

想象一下,每次您对工具进行更改并重新生成代码时,您都会在所有模块上手动更改设计。

Since I've started generating code and gotten up to speed, I've found that I rarely get bugs in the generated code.

由于我已经开始生成代码并且加快了速度,我发现我很少在生成的代码中遇到错误。

#15


I find that writing code gen does help me learn the nuances of good architecture. You start seeing common patterns as opposed to a narrow view of your design. That said, don't use code gen as a substitute for good object-oriented code, and don't love your code gen so much you ignore new technologies. For example, if you're in .NET and are writing code-gen for data access, you'd better have a good excuse for not using Linq to SQL or NHibernate. Similarly, Dynamic Data can help in many forms-on-data scenarios. So, my advice: spike new stuff and code gen as needed.

我发现编写代码确实帮助我学习了良好架构的细微差别。您开始看到常见的图案,而不是狭隘的设计视图。也就是说,不要使用代码生成代替良好的面向对象的代码,并且不要太喜欢你的代码,否则你会忽略新技术。例如,如果你在.NET中并且正在编写用于数据访问的代码,那么你最好有一个不使用Linq to SQL或NHibernate的借口。同样,动态数据可以在许多数据形式场景中提供帮助。所以,我的建议是:根据需要加入新东西和代码。

#16


My 2cents on code gen is that it is also critical for use in refactoring. I have found that partial classes and a good file comparison utility (Araxis or BeyondCompare) are essential.

我对代码生成的看法是,它在重构中的使用也很重要。我发现部分类和良好的文件比较实用程序(Araxis或BeyondCompare)是必不可少的。

Keep your generated code in one file and the custom Tweaks you made for that class in another file.

将生成的代码保存在一个文件中,并将您为该类创建的自定义调整保存在另一个文件中。

This practice will allow you to make those comprehensive framework changes implemented quickly and will also help you move to a new paradigm while easily being able to save your custom logic.

这种做法将允许您快速实施这些全面的框架更改,并且还可以帮助您转换到新的范例,同时轻松地保存您的自定义逻辑。

CodeSmith FTW!

While build servers are great to make sure all your code compiles, it doesn't address the differences in signatures with your stored procs or the like. If you routinely run the code gen you can more easily identify when those changes occur. A unit test will tell you the SP is wrong, code gen will tell you how to make it right.

虽然构建服务器非常适合确保所有代码编译,但它并没有解决签名与存储过程等的差异。如果您经常运行代码生成,则可以更轻松地识别何时发生这些更改。单元测试会告诉你SP是错误的,代码将告诉你如何使它正确。