简体   繁体   English

使用可重用代码的最佳方式是什么?

[英]What is the best way to use reusable code?

More often then not you will likely come across a situation where you may need a function or procedure, chances are you have already written this code before.通常情况下,您可能会遇到需要 function 或程序的情况,您可能之前已经编写过此代码。

My question is how do you organise it so you dont have to rewrite everything, in other words what is the best way to maintain resuable code?我的问题是您如何组织它,这样您就不必重写所有内容,换句话说,维护可重用代码的最佳方法是什么?

I was thinking of putting all my procedures and functions into one single unit, and adding this unit to the Uses clause of required forms etc.我正在考虑将我所有的程序和功能放在一个单元中,并将这个单元添加到所需的 forms 等的 Uses 子句中。

But then the unit would become rather large, and so to use maybe one or two functions from the one large unit will add unnecessarily bulk size to the Application.但是随后该单元会变得相当大,因此使用一个大单元中的一两个函数可能会给应用程序增加不必要的体积大小。

What I am basically asking is what is the best method for both performance and convenience wise, to manage, maintain and use reusable code?我基本上要问的是,在性能和便利方面,管理、维护和使用可重用代码的最佳方法是什么?

UPDATE更新

Following some informative posts, I decided to proceed and use one single source unit that contains all my reusable routines.在一些信息丰富的帖子之后,我决定继续并使用一个包含我所有可重用例程的单一源单元。 I have incorporated the use of the Delphi Code Editor's Code Folding to create custom regions, to seperate the different types into groups, well regions.我已经结合使用 Delphi 代码编辑器的代码折叠来创建自定义区域,将不同的类型分成组和井区域。 All regions will be collapsed as well as having meaningful region names.所有区域都将被折叠并具有有意义的区域名称。 I have added the single source unit to the Library path, which I can add to any forms uses clause and have access to all the reusable routines.我已将单个源单元添加到库路径中,我可以将其添加到任何 forms 使用子句并可以访问所有可重用例程。 See the screenshot for a better idea:请参阅屏幕截图以获得更好的想法:

截屏

A lot more routines need adding in there.更多的例程需要在那里添加。 The regions help keep the code organized and easier to read, I can expand any if needed.这些区域有助于保持代码井井有条并且更易于阅读,如果需要,我可以扩展任何内容。

Thanks.谢谢。

Instead of one unit, use a folder with multiple units.使用具有多个单元的文件夹,而不是一个单元。

Organise the units according to subject matter and strive to maintain high cohesion, low coupling.按主题组织单元,努力保持高内聚、低耦合。 In other words: the units should contain functions and procedures that are closely related and use as few of your other units as possible (vcl/rtl can be freely used of course).换句话说:单元应该包含密切相关的功能和过程,并尽可能少地使用其他单元(当然可以自由使用vcl/rtl)。

If you need inspiration on what kind of units you could have: I'd take a leaf out of the VCL and RTL.如果您需要有关可以拥有哪种单元的灵感:我会从 VCL 和 RTL 中吸取教训。 The way the units are organised and what stuff is put together versus what gets its own unit can give you a pretty clear understanding of how to organise your own stuff.单元的组织方式以及将哪些东西放在一起以及哪些东西有自己的单元可以让您非常清楚地了解如何组织自己的东西。

First, your assumption that large units result in large executables is wrong.首先,您认为大型单元会导致大型可执行文件的假设是错误的。 Only the used parts get linked into the final executable.只有使用过的部分才会链接到最终的可执行文件中。

Placing reusable routines in one or more separate units is common practice.将可重用的例程放在一个或多个单独的单元中是常见的做法。 To which extend you normalize this depends on your own proceedings or on imposed rules (eg by your company).您将其规范化的程度取决于您自己的程序或强加的规则(例如,您的公司)。

Personally, I like to name units like Delphi does: AwControls, AwUtils, AwDB, AwForms, AwMenus for those routines and classes that extend the default VCL or contain new or derived components.就个人而言,我喜欢将 Delphi 之类的单元命名为:AwControls、AwUtils、AwDB、AwForms、AwMenus,用于那些扩展默认 VCL 或包含新组件或派生组件的例程和类。 But I also create separate units for rather big components or for uncommon routines or classes like AwPlanGrid, AwDxf, AwIpTypes.但我也为相当大的组件或不常见的例程或类(如 AwPlanGrid、AwDxf、AwIpTypes)创建单独的单元。 Just a suggestion.只是一个建议。

Another tip: Add the folder containing these units to your default project search path (to set in project options while having no project loaded in the IDE).另一个提示:将包含这些单元的文件夹添加到您的默认项目搜索路径(在项目选项中设置,同时在 IDE 中没有加载项目)。

Today I tried to add a unit to my code, and it used another unit.今天我试图在我的代码中添加一个单元,它使用了另一个单元。 That unit used three more.那个单位又用了三个。 And the three that that one used, used a total of 27 other units.而那一个用的三个,一共用了27个其他单位。

Because those units weren't in a single folder, I had to set up a search path, or else add, a total of 27 units to my demo project, just so I could call one of my library functions.因为这些单元不在一个文件夹中,所以我必须设置一个搜索路径,或者将总共 27 个单元添加到我的演示项目中,这样我就可以调用我的库函数之一。

In case you think this is extreme, just try to use SOME of the JEDI JCL or JVCL without using a lot more of it.如果您认为这很极端,请尝试使用 JEDI JCL 或 JVCL 中的一些,而不要使用更多。

This is an example of the kind of coupling that makes it feel like it's Not Worth Trying to Reuse Your Code.这是一种耦合的例子,它让人觉得不值得尝试重用你的代码。

I am thinking that for my next project, instead of a monolithic dUnit for unit tests, that all tests for a single reusable chunk of the code should be built into their own separate executables, per class they test, and configured to explicitly include the units they need, without including any search folders/library folders that can allow new units to just silently be added to the code.我在想,对于我的下一个项目,而不是用于单元测试的单片 dUnit,应将针对单个可重用代码块的所有测试构建到它们自己单独的可执行文件中,根据他们测试的 class,并配置为显式包含单元他们需要,但不包括任何可以允许将新单元静默添加到代码中的搜索文件夹/库文件夹。 Then I any time someone couples stuff, they would break the unit tests, and we would know something bad happened.然后我任何时候有人把东西结合起来,他们会破坏单元测试,我们就会知道发生了一些不好的事情。

Anyways, that seems to me, to be one way to guarantee that your code avoids coupling, and that you could write a small or new application and pull into your code, just the pieces (components if you like) that you need, without a giant train of "USE-Clause-dependency hell".无论如何,在我看来,这是保证您的代码避免耦合的一种方法,并且您可以编写一个小型或新应用程序并拉入您的代码,只是您需要的部分(如果您喜欢的组件),而无需“USE-Clause-dependency hell”的巨型火车。

Ditto to marjan's statement that a Single Unit is not the way to go;同上 marjan 的声明,即单个单元不是通往 go 的方式; Dependencies in a huge Utility unit are going to go haywire.一个巨大的实用程序单元中的依赖项将被 go 乱码。 To build any project that uses that Utility unit, you will need to have available all the units in its interface and implementation section.要构建使用该实用程序单元的任何项目,您需要在其接口和实现部分中提供所有单元。

Keep it simple.把事情简单化。 Keep it separate.保持分开。 Coupling bad.耦合不好。 easy and fun Reusability is only possible if you avoid coupling.简单而有趣 只有避免耦合,才能实现可重用性。

I put all my common code in a common library (actually there are about 4 PAS files in the same folder).我把我所有的公共代码放在一个公共库中(实际上在同一个文件夹中大约有 4 个 PAS 文件)。 Which is indeed huuuge.这确实是huuuge。 But only the needed code will be added to your exe, not the entire library.但是只会将所需的代码添加到您的 exe 中,而不是整个库。 Delphi compiler is smart enough to know which code is needed. Delphi 编译器足够聪明,可以知道需要哪些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM