Whilst moving some applications from Delphi XE2 to XE5 I've noticed that there are a number of units listed which are no longer needed.
This is turn lead me to tidying up the uses section of the interface which leads to my question.
Is there a recommended order for units in the interface? I know they are loaded in reverse order, but should I be organising them by Usnit Scope.
For example should MyCompany.* be listed at the start or the end? Should I place the Vcl.* before the System.*
update
In general I will include my Units in the implementation rather than interface. I'm grasping more about whether there is a hierarchy to Delphi's own units...
For example I have encountered one issue where listing the Windows unit before System.SysUtils changes the version of DeleteFile that is used...
Explicit Question
Should the order of uses be
Winapi*, System.*, Vcl.*, or the reverse?
There are some risks associated with uses list refactoring:
Later used units will override symbols (methods
, classes
, vars
, consts
, ...) of units used earlier (scoping).
This can introduce (more or less) sublte bugs into your program.
Reordering units can change the order in which intialization
and finalization
sections are run.
There are also some advantages:
Faster compiling speed
Smaller executables (Only if any removed units had initialization
or finalization
)
Avoiding / Preventing uses circles (although uses circles are often a sign of a sub optimal OOP design)
Having said that and after your question update:
I tend to do the same thing that David already wrote: Order the units from low level to high level.
There are tools that can help you with finding unused units and also arranging the used ones in the best order. (Peganza Icarus / PascalAnalyzer and ModelMaker)
It's hard to say what the compiler really does (especially since there are now multiple compilers), but putting units with the least dependencies first should make it easier for the compiler to build the dependency tree.