Are there any advantages to using fully qualified namespaces in the Uses section?

Is it better to fully qualify namespaces in a uses clause? For example, is one of these ads better than the other?

uses ShellApi, Windows, SHFolder, SysUtils; uses Winapi.ShellApi, Winapi.Windows, Winapi.SHFolder, System.SysUtils; 
+6
source share
2 answers

It really depends on what you are building. If this is a simple VCL application, since Delphi is the most famous, then you usually don't need to worry about it. However, if you are creating a package, for example, with components, you need to clearly define which environment you are going to use: VCL or FMX. Embarcadero added namespace prefixes to be able to distinguish between different possible solutions.

However, in most scenarios . serves only as a visual representation. This will help you, the encoder, find out which libraries you use.

Take this other question , for example. The Delphi IDE / Compiler will not accept one very generic device without adding a namespace prefix or namespace in the project settings. A standard Graphics device should be explicitly defined as Vcl.Graphics , not FMX.Graphics .

On the side, the note, using the full namespace, is convenient for many coders who come from other languages, where it was strictly followed, and not only that, but allows you to see nature with just one glance, without having to look elsewhere for more information about what you are actually using.

EDIT

In addition, I recently noticed that using fully qualified namespaces also speeds up compilation because the compiler does not need to try to resolve all namespaces.

+7
source

The main advantage of fully qualified names is that your code can be successfully compiled into projects, regardless of the namespace prefix settings for these projects.

Why is this useful? Well, if you write application code, you probably know and control the prefix settings for the namespace of the project (s) that contain a specific block. However, if you write library code, code that should be included in projects outside your control, then you cannot predict what namespace prefixes will be in these projects. If you do not fully qualify the module names, you set a limit on the project parameters of any project that consumes your library code. And the library code is not expected to do this.

So overall, for application code, namespace qualifiers can be reasonably omitted if you prefer less verbose names. Fully qualified names should be used for library code to allow library code to stand alone.

+3
source

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


All Articles