What is the recommended naming for scoped units in

2019-09-08 06:12发布

With the advent of Delphi XE2 scoped units like Xml.Internal.AdomCore_4_3 or System.StrUtils came in fashion.

I like the ability to use such descriptive names but I am puzzled what are the naming conventions and preffered directory structure.

  1. Should it be

    • com.company.project.Security.Compression.ZLib.pas like in Java

    • System.Security.Compression.ZLib.pas like in .NET

    • or something else?

  2. Should I place my files in a directory strcuture like this

    • System\Security\Compression\System.Security.Compression.ZLib.pas

    • or just System.Security.Compression.ZLib.pas in the root folder?

Looking at the way Embarcadero organized their units I am left with the impression that they simply kept the directory structure as in Delphi 5/6/7/.../XE

Please advise.

2条回答
Root(大扎)
2楼-- · 2019-09-08 06:36

I believe that everyone should be scoping their units. (You don't need to do this only if you are a 3rd party component vendor - as you want to reduce name collision on any external units that you utilize.) By scoping your units you are going a very long way to prevent unit naming issues.

I'd strongly suggest a global prefix that you will use for all internally-created units. You can use an abbreviated form of your company name, or your full name as its totally your preference. I would recommend simply something readable and easy to type.

If you work in a handful of major projects, then project-specific names would follow such as Acme.Widgets.SlicerUtils.pas for the Widgets product/project and Acme.Wonkers.WippleFactory.pas for the Wonkers product.

Regardless of the naming, this should strongly correlate to how you versionize your projects and manage your source code. You want to be able to easily setup a build for version 1.2.1.0 of the Widgets project to include all the units related to that specific build. (1.2.1.0 Widgets may include an explicit revision 1.5.3.0 of the System library) This needs to be easily understood by all developers and seemlessly managed. You should desire one-click operations to begin work on 1.2.1.1 of the Widgets project.

The subdirectory question is answered by the general rule to separate version-controlled projects into their own sub directories. So if your Acme.System.Security.Compression library is version controlled separately than Acme.System.Security.Auth, then you'll have a root\System\Compression subdirectory structure...but more likely you'll simply have Acme.Widgets.System)

Hope this helps a little.

查看更多
叼着烟拽天下
3楼-- · 2019-09-08 06:52

Unlike Java and .NET the "." in the new scoped filenames provides no functionality at run-time or design-time. System.StrUtils.pas could just as easily have been called System_StrUtils.pas or SystemXStrUtils.pas and we'd have exactly the same situation as we have now.

Because of that I don't recommend you adopt the new naming conventions unless perhaps you are a component developer who distributes your components publicly.

Even then I wouldn't name your units System.MyUnit.pas, as I believe that prefix should be limited to the official Delphi System units. If you really have to adopt this new convention (personally I don't think I will be) I'd name your units something along the lines of

MyCompany.UnitName.pas

or

MyComponentLib.UnitName.pas

I don't see any need to put VCL or FMX in the names unless you're releasing a dual framework library.

查看更多
登录 后发表回答