What is the best way to use reusable code?

Most often then you most likely will not encounter a situation where you may need a function or procedure, most likely you have already written this code before.

My question is how you organize it, so you don’t need to rewrite everything, in other words, what is the best way to maintain the restored code?

I thought about putting all my procedures and functions in one unit and adding this unit to the Uses offer of the necessary forms, etc.

But then the block will become quite large, and therefore using one or two functions from one large block will add unnecessarily large size for the application.

What am I mostly asking, what is the best method for both performance and convenience, for managing, maintaining and using reusable code?

UPDATE

Following some informative messages, I decided to continue and use one single source containing all of my reusable routines. I have enabled the use of the Delphi Code Folding code editor code to create custom regions in order to divide different types into groups, area regions. All regions will be destroyed, as well as have significant regional names. I have added a unit of source code to the library path, which I can add to any sentence using forms and have access to all reusable procedures. See screenshot for a better idea:

Screen shot

You need to add some more routines. Regions help keep code organized and easier to read, I can expand it if necessary.

Thanks.

+6
source share
4 answers

Firstly, your assumption that large units lead to large executable files is incorrect. Only used parts are associated with the final executable.

Placing reusable routines in one or more separate devices is common practice. To which you normalize, it depends on your own production or on established rules (for example, your company).

Personally, I like to call such units as Delphi: AwControls, AwUtils, AwDB, AwForms, AwMenus for these routines and classes that extend VCL by default or contain new or derived components. But I also create separate blocks for fairly large components or for unusual routines or classes, such as AwPlanGrid, AwDxf, AwIpTypes. Just an offer.

One more tip: Add the folder containing these units to the default search path (to set in the project settings without loading the project into the IDE).

+6
source

Instead of a single device, use a folder with several units.

Arrange devices in accordance with the subject and strive to maintain high adhesion, low adhesion. In other words: units should contain functions and procedures that are closely related and use as much of your other units as possible (vcl / rtl can be used freely).

If you need inspiration about which units you could have: I would take a sheet from VCL and RTL. The way the units are organized and what things are collected in comparison with what gets your own unit can give you a pretty clear idea of ​​how to organize your own material.

+10
source

Today I tried to add a block to my code, and it used a different device. This block used three more. And three of them that used used a total of 27 other units.

Since these units were not in the same folder, I had to set up the search path or add 27 units to my demo project so that I could call one of my library functions.

If you think this is extreme, just try SOME of the JEDI JCL or JVCL without using much more.

This is an example of the type of connection that makes you feel like it’s not worth trying to reuse your code.

I think that for my next project, instead of the monolithic dUnit for unit tests, all tests for one reusable code fragment should be built into their own separate executable files, for each class they test, and set to explicitly include the units they need, without including any search folders / libraries that may allow new units to simply be added to the code discreetly. Then I, when someone collects things, they break the unit tests, and we find out that something bad happened.

In any case, it seems to me that this is one way to ensure that your code avoids binding, and that you could write a small or new application and draw into your code only the parts (components, if you want) that you need without a giant train "USE-CLASS-adeni".

Also to Marjan's statement that the One Unit is not the way; Dependencies in the huge Utility division are about to escape. To create any project that uses this Utility module, you will need to have all the blocks in the interface and implementation section.

Keep it simple. Keep it separate. The grip is bad. easy and fun Reuse is only possible if you avoid traction.

+1
source

I put all my shared code into a shared library (in fact, there are about 4 PAS files in one folder). This is really huuuge. But only the necessary code will be added to your exe, and not the entire library. The Delphi compiler is smart enough to know what code is needed.

0
source

Source: https://habr.com/ru/post/890302/


All Articles