7

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

uses
  ShellApi,
  Windows,
  SHFolder,
  SysUtils;

uses
  Winapi.ShellApi,
  Winapi.Windows,
  Winapi.SHFolder,
  System.SysUtils;

2 Answers2

8

It really depends on what you're building. If it's a simple VCL application as Delphi is most known for, then you usually don't need to worry about it. However, if you're building a package, with components for example, you need to be sure to clearly define which environment you intend to use: VCL or FMX. Embarcadero added namespace prefixes to be able to differentiate different possible solutions.

However, in most scenarios, the . only serves as a visual representation. It helps you, the coder, be able to identify which libraries you're using.

Take this other question for example. The Delphi IDE/Compiler would not accept one very common unit without either adding the namespace prefix or the namespace in the project options. The standard Graphics unit needed to be explicitly defined as Vcl.Graphics, as opposed to FMX.Graphics.

On a side-note, using the full namespace is comfortable for many coders who come from other languages where it was strictly enforced, and not only that, but allows you to see the nature of everything in a single glance, without having to look elsewhere for more information about what you're actually using.

EDIT

In addition, I just recently saw that using fully qualified namespaces also helps speed up compile-time, because the compiler doesn't have to try to resolve all the namespaces.

Community
  • 1
  • 1
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • Just to give heads up how using Fully Qualified Namespaces helps to speed up building process. So, on 7gen i7 320 000 lines of code compiles on average in 6 seconds. Using fully qualified namespaces code compiles on average in 6 seconds. So, if there is an advantage, it is just in ms. On an older computer same project of 320 000 lines of code compiles on average in 14 seconds. Using fully qualified unit names code compiles in 14 seconds. – Edijs Kolesnikovičs Nov 19 '18 at 17:12
  • @EdijsKolesnikovičs Thanks for testing that. It's just something I saw a couple devs mention, and it made sense, so added it. Sure, it won't affect too many seconds on a single unit. And the LOC in that unit doesn't matter. Imagine many units with many things in their `uses` clauses. I'm not saying it will speed it up by seconds, but altogether, a half a second faster for an overall project is still somewhat notable, considering Delphi's best known for compilation speed. It's the filesystem that matters. PS - I work with 10+ million LOC. – Jerry Dodge Nov 20 '18 at 01:30
3

The main benefit of fully qualifying names is that your code can be compiled successfully into projects, irrespective of the namespace prefix settings of those projects.

Why is this useful? Well, if you are writing application code then you likely know and control the namespace prefix settings of the project(s) that contain a particular unit. However, if you are writing library code, code that is intended to be included in projects outside of your control, then you cannot predict what the namespace prefixes will be in those projects. If you do not fully qualify unit names, then you place a constraint on the projects settings of any project that consumes your library code. And library code is expected not to do that.

So, in summary, for application code, namespace qualifiers can reasonably be omitted, if you prefer less verbose names. For library code, fully qualified names should be used to allow the library code to stand alone.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490