The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,967 other subscribers

Archive for November 11th, 2014

Delphi: removing “unused” units from uses lists cannot be fully automated (via: SO)

Posted by jpluimers on 2014/11/11

One of the things a lot of Delphi users want is to be able to automagically remove unused units from their uses lists and projects.

The short answer is: you can’t.

The long answer starts with: you can’t fore a number of reasons.

Similar reasonings hold for many other development environments. Plain Windows EXEs and DLL dependencies. .NET projects and assembly dependencies, etc.

Initialization/Finalization dependency

The first reason is that each unit (module, assembly, or other dependency) can contain global code to be executed at unit start/load or finish/unload.

So even though you do not reference anything inside that unit, the initialization and finalization sections can be run.

Removing the dependency from your units and project, kills that functionality. And might break all sorts of things.

Load order dependency

Sometimes you have subtle load order dependencies of units. Those should be rare, and if they are there, should be enforced by the affected units themselves. But everyone knows those subtle dependencies are more often a by product not enforced by anything than coincidence.

So if you start removing references, the load order might change, and subtle bugs may occur.

In other words: test, test, test and test your codebase before and after removing unit references from uses lists.


If you understands the dependencies of initializtion/finalization or load order, you will get interested to know what units are actually being used.

The ultimate source for this would be the Delphi compiler. Bad luck here: you cannot use it as the IDE and command-line interfaces don’t offer a hook to it to do just this.

So you need alternative parsers that can help out. The answers to How to remove unused units from all source files on Delphi XE2 describe a few and they all have the same drawback: they are not the Delphi compiler, so they are a rough approximation of what the compiler would do.

And even if the approximation would be perfect, they all suffer from the same thing the compiler suffers from: you can only have one set of conditional defines, platforms, etc at the same time.

There is lots of code for which the usage is conditional, but where the uses list does not reflect this.


Optimizing uses lists to eliminate unused units seems a simple thing at start, but isn’t.

The best way to keep those optimized is to prune them while developing. So if you remove code, try to remember cutting down the uses lists by hand.

And then test, test, test and test your codebase.


via: ide – How to remove unused units from all source files on Delphi XE2? – Stack Overflow.

Posted in Delphi, Delphi 1, Delphi 2, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Development, Software Development | 8 Comments »

%d bloggers like this: