使用大型系统的编程模板

发布于 2024-12-28 21:36:50 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿咻 2025-01-04 21:36:50

答案也许是已经决定的。说实话,这取决于什么
模板正在被使用。模板的成本很高
增加了复杂性和耦合性。替代方案也可能有
然而,当替代品的成本为
高于模板的成本,那么就使用模板。 (这是
当然,无论应用程序的大小如何,都是如此。但事情
就像增加的复杂性和耦合往往会产生更大的影响
大型项目。)

在解决这个问题之前,您必须区分使用
模板和定义模板。确实没有什么可争论的
反对使用标准库中定义的模板,例如:
任何额外的费用均由图书馆的开发人员支付,而不是由
你,所以这都是好处。类似的论点也适用于第三方
图书馆。但是,对于您自己定义的模板,您必须
考虑模板定义的修改所产生的影响
关于构建时间。为几乎所有内容定义模板是
好主意。另一方面,您也许能够隔离某些
功能,并像对待第三方一样对待它们
图书馆,内部开发,但除极端情况外不进行修改。

有趣的是,模板不会引起问题(低级别,
非常稳定的元素)通常是它们最有用的地方。为了
“应用级”模块,处理实际的
应用程序特定的代码,模板通常不太有用;
继承更有用。对于低层组件,沿着
另一方面,std::vector 行非常有用;
但这些组件通常是独立于其他组件而开发的
无论如何,应用程序必须非常稳定。

The answer is a decided maybe. Seriously, it depend on what the
templates are being used for. Templates have a significant cost in
added complexity and coupling. The alternatives may also have
significant cost, however—when the cost of the alternatives is
higher than the cost of templates, then you use templates. (This is
true regardless of the size of the application, of course. But things
like added complexity and coupling tend to have a much greater impact in
large projects.)

Before addressing the question, you have to distinguish between using
templates, and defining templates. There is certainly no argument
against using templates defined in the standard library, for example:
any added costs have been paid by the developers of the library, not by
you, so it's all benefits. Similar arguments apply for third party
libraries. For templates you define yourself, however, you must
consider the impact that a modification in the template definition has
on build times. Defining templates for just about everything is not
a good idea. On the other hand, you might be able to isolate certain
functionalities, and treat them almost as you would a third party
library, developed in house, but not modified except in extreme cases.

Interestingly enough, where templates don't cause problems (low level,
very stable elements) is typically where they are most useful. For
“application level” modules, which deal with the actual
application specific code, templates typically aren't too useful anyway;
inheritance is far more useful. For low level components, along the
lines of std::vector, on the other hand, they're exceedingly useful;
but such components are generally developed in isolation from the rest
of the application, must be very stable anyway.

懒的傷心 2025-01-04 21:36:50

我不会告诉你答案,但我会给你这个提示。

问问自己:

使用模板的令人信服的原因是什么,以及不使用模板的令人信服的原因(例如编译时间)是什么?

然后问问自己,为什么这些原因在大型系统中是有用还是有害?

让我们看看大型系统。是什么让它们变大?可能有很多因素,例如:

  • 代码量

  • 用户数量

  • 部署规模

  • 地理

  • 开发成本

  • 维护成本

    等等

现在考虑一些模板有利于

  • 算法的可重用性和组件化

的事情,这两者如何帮助上述内容?

  • 代码量:良好的可重用性将有助于减少维护的代码量
  • 。 开发成本:可重用性也将有所帮助,因为需要开发的内容较少,但如果开发人员需要更高的技能,则可能会增加成本。所以维护成本是双刃剑
  • :可重复性将在这里有所帮助,因为测试和维护更少

如果您回答第一个,那么后者就会变得容易得多。

没有正确或错误的答案,而是每个系统的设计将根据相关系统的实际关注点进行分析和决定的论点。

I'm not telling you the answer but I'll give you this hint.

Ask your self this:

What are the compelling reasons for using templates, and the compelling reasons for not using templates (like compile time)?

Then ask yourself why if any of these reasons are useful or detrimental in large scale systems?

Lets look at the large scale systems. What makes them large? It could be many things such as:

  • Amount of code

  • Number users

  • scale of deployment

  • geography

  • cost of development

  • cost of maintennance

    etc.

Now consider some things that templates are good for

  • resuabilty and componentisation of algorithms

etc

How do these two aid the above?

  • Amount of code : well resuabilty will help to reduce the amount of code to maintain
  • Cost of development: resuabilty will help this too as there is less to develop, but it may increase cost if the developers need to be more skilled. So double edged sword there
  • Cost of maintenance : resuabilty will help here as there less to test and maintain

If you answer the first, the latter becomes much easier.

There is no right or wrong answer, rather it's an argument that each system's design will analyse and decide upon based on the actual concerns of the system in question.

夜访吸血鬼 2025-01-04 21:36:50

您的问题非常主观,我的印象是您不会得出令所有大型系统开发人员甚至所有老​​师满意的答案。就我个人而言,我追求更简单的做事方式,这有时意味着不使用模板,有时意味着让模板为您复制代码。编译时间始终可以通过 PIMPL 或显式实例化来限制,但避免在自然使用模板的情况下可能会导致更多的膨胀、模糊的运行时错误和性能损失。

我对你的作业的建议是:提出所有支持和反对使用模板的论点,不要以是或否的答案作为结论,而是以介于两者之间的东西作为结论;-)。并且不要让软件方法论的狂热分子夺走您编码的乐趣。

Your question is highly subjective, and I have the impression that you won't arrive to an answer that satisfies all large-scale system developers or even all your teachers. Personally, I go for the simpler way of doing something, and that sometimes means not-working-with-templates and sometimes means let-templates-replicate-the-code-for-you. Compilation times can always be firewalled with PIMPL or with explicit instantiations, but avoiding templates where their use would be only natural can incur in more bloating, obscure runtime errors and loss of performance.

My suggestion for your homework: present all the arguments favoring and countering the use of templates, and don't conclude with a yes-or-no answer, but rather with something in between ;-) . And don't let the zealots of software methodologies to take the joy of coding from you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文